Skip to content

Commit

Permalink
Drop struct fields we don't use.
Browse files Browse the repository at this point in the history
  • Loading branch information
denschub committed Jun 8, 2024
1 parent afb26bf commit 9a21406
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
17 changes: 7 additions & 10 deletions src/models/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,28 @@ use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
use chrono::{DateTime, Utc};
use sqlx::{postgres::PgQueryResult, PgExecutor};
use uuid::Uuid;

/// A secret entry stored in the database.
#[derive(Debug)]
pub struct Secret {
pub uuid: Uuid,

pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,

pub file_name: Option<String>,
pub contents: Option<Vec<u8>>,

pub notes: Option<String>,
}

impl Secret {
/// Tries to find a Secret based on its UUID. Returns an Error if nothing
/// could be found.
pub async fn find<'e>(db: impl PgExecutor<'e>, uuid: Uuid) -> Result<Self, sqlx::Error> {
sqlx::query_as!(Self, "select * from secrets where uuid = $1", uuid)
.fetch_one(db)
.await
sqlx::query_as!(
Self,
"select uuid, file_name, contents from secrets where uuid = $1",
uuid
)
.fetch_one(db)
.await
}

/// Updates the contents of the Secret, both in the struct, but also in the
Expand Down
24 changes: 10 additions & 14 deletions src/models/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,9 @@ use crate::{errors::ResponseError, ServerState};
#[derive(Debug)]
pub struct Token {
pub uuid: Uuid,

pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub used_at: Option<DateTime<Utc>>,
pub expires_at: Option<DateTime<Utc>>,

pub token: String,
pub superuser: bool,

pub notes: Option<String>,
}

impl Token {
Expand All @@ -37,9 +30,13 @@ impl Token {
db: impl PgExecutor<'e>,
token: &str,
) -> Result<Option<Self>, sqlx::Error> {
sqlx::query_as!(Self, "select * from tokens where token = $1", token)
.fetch_optional(db)
.await
sqlx::query_as!(
Self,
"select uuid, expires_at, token, superuser from tokens where token = $1",
token
)
.fetch_optional(db)
.await
}

/// Little helper that checks if a token is expired. If the token has no
Expand All @@ -59,10 +56,9 @@ impl Token {
&mut self,
db: impl PgExecutor<'e>,
) -> Result<PgQueryResult, sqlx::Error> {
self.updated_at = Utc::now();
sqlx::query!(
"update tokens set used_at = $1 where uuid = $2",
self.updated_at,
Utc::now(),
self.uuid
)
.execute(db)
Expand All @@ -82,7 +78,7 @@ impl Token {
}

Ok(sqlx::query!(
r#"select * from token_permissions where token = $1 and secret = $2 and can_read = true"#,
r#"select token from token_permissions where token = $1 and secret = $2 and can_read = true"#,
self.uuid,
secret_uuid
).fetch_optional(db).await?.is_some())
Expand All @@ -101,7 +97,7 @@ impl Token {
}

Ok(sqlx::query!(
r#"select * from token_permissions where token = $1 and secret = $2 and can_write = true"#,
r#"select token from token_permissions where token = $1 and secret = $2 and can_write = true"#,
self.uuid,
secret_uuid
).fetch_optional(db).await?.is_some())
Expand Down

0 comments on commit 9a21406

Please sign in to comment.