Skip to content

Commit

Permalink
replace OffsetDateTime with chrono (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
thebino committed Oct 28, 2023
1 parent e3fda67 commit 51f4edb
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 58 deletions.
41 changes: 24 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ abi_stable = "0.11.1"
activitypub_federation = "0.4.6"
async-trait = "0.1.73"
axum = { version = "0.6.20", features = ["ws", "headers"] }
axum-test = "12.1.0"
axum-test = "13.0.1"
anyhow = "1.0.72"

bytes = "1.4.0"
Expand Down
2 changes: 2 additions & 0 deletions crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ axum.workspace = true
http.workspace = true
photos_network_plugin = { path = "../plugin_interface" }

sqlx.workspace = true

serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
serde_with.workspace = true
Expand Down
14 changes: 8 additions & 6 deletions crates/common/src/auth/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@
*/

use std::fmt;
use time::OffsetDateTime;

use sqlx::types::chrono::DateTime;
use sqlx::types::chrono::Utc;

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct User {
pub uuid: String, //Uuid,
pub uuid: String,
pub email: String,
pub password: Option<String>,
pub lastname: Option<String>,
pub firstname: Option<String>,
pub is_locked: bool,
pub created_at: OffsetDateTime,
pub updated_at: Option<OffsetDateTime>,
pub last_login: Option<OffsetDateTime>,
pub created_at: DateTime<Utc>,
pub updated_at: Option<DateTime<Utc>>,
pub last_login: Option<DateTime<Utc>>,
}

impl fmt::Display for User {
Expand All @@ -50,7 +52,7 @@ impl User {
lastname: Option::None,
firstname: Option::None,
is_locked: false,
created_at: OffsetDateTime::now_utc(),
created_at: Utc::now(),
updated_at: Option::None,
last_login: Option::None,
}
Expand Down
6 changes: 4 additions & 2 deletions crates/common/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ use std::sync::Arc;

use anyhow::Result;
use async_trait::async_trait;
use time::OffsetDateTime;
use sqlx::types::chrono::DateTime;

use sqlx::types::chrono::Utc;

use crate::auth::user::User;

Expand Down Expand Up @@ -59,7 +61,7 @@ pub trait Database {
&self,
user_id: &str,
name: &str,
date_taken: OffsetDateTime,
date_taken: DateTime<Utc>,
) -> Result<String>;
async fn get_media_item(&self, media_id: &str) -> Result<MediaItem>;
async fn add_reference(
Expand Down
4 changes: 2 additions & 2 deletions crates/common/src/database/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

use time::OffsetDateTime;
use sqlx::types::chrono::{DateTime, Utc};

pub struct Reference {
pub uuid: String,
pub filepath: String,
pub filename: String,
pub size: u64,
pub description: &'static str,
pub last_modified: OffsetDateTime,
pub last_modified: DateTime<Utc>,
pub is_missing: bool,
}
2 changes: 1 addition & 1 deletion crates/database/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async-trait.workspace = true
tracing.workspace = true
uuid.workspace = true
tokio.workspace = true
sqlx = { workspace = true, features = ["runtime-tokio", "tls-native-tls", "postgres", "mysql", "sqlite", "any", "macros", "migrate", "time" ] }
sqlx = { workspace = true, features = ["runtime-tokio", "tls-native-tls", "postgres", "mysql", "sqlite", "any", "macros", "migrate", "time", "chrono" ] }

[dev-dependencies]
pretty_assertions.workspace = true
Expand Down
8 changes: 4 additions & 4 deletions crates/database/src/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use common::auth::user::User;
use common::database::media_item::MediaItem;
use common::database::reference::Reference;
use common::database::Database;
use sqlx::types::time::OffsetDateTime;
use sqlx::types::chrono::{DateTime, Utc};
use sqlx::PgPool;
use sqlx::Row;
use tracing::info;
Expand Down Expand Up @@ -63,7 +63,7 @@ impl Database for PostgresDatabase {
lastname: row.get("lastname"),
firstname: row.get("firstname"),
is_locked: false,
created_at: OffsetDateTime::now_utc(),
created_at: Utc::now(),
updated_at: None,
last_login: None,
})
Expand Down Expand Up @@ -128,7 +128,7 @@ impl Database for PostgresDatabase {
&self,
user_id: &str,
name: &str,
date_taken: OffsetDateTime,
date_taken: DateTime<Utc>,
) -> Result<String> {
let query = "SELECT COUNT(*) FROM media WHERE owner is $1 and taken_at like $2";
let res = sqlx::query(query).bind(user_id).bind(date_taken);
Expand All @@ -147,7 +147,7 @@ impl Database for PostgresDatabase {
.bind(user_id)
.bind(name)
.bind(false)
.bind(OffsetDateTime::now_utc())
.bind(Utc::now())
.bind(date_taken)
.execute(&self.pool)
.await?;
Expand Down
37 changes: 23 additions & 14 deletions crates/database/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ use common::database::media_item::MediaItem;
use common::database::reference::Reference;
use common::database::Database;
use sqlx::sqlite::SqliteQueryResult;
use sqlx::types::time::OffsetDateTime;
use sqlx::types::chrono::DateTime;
use sqlx::types::chrono::Utc;
use sqlx::Row;
use sqlx::SqlitePool;
use std::i64;
Expand Down Expand Up @@ -66,7 +67,7 @@ impl Database for SqliteDatabase {
lastname: row.get("lastname"),
firstname: row.get("firstname"),
is_locked: false,
created_at: OffsetDateTime::now_utc(),
created_at: Utc::now(),
updated_at: None,
last_login: None,
})
Expand Down Expand Up @@ -128,7 +129,7 @@ impl Database for SqliteDatabase {
&self,
user_id: &str,
name: &str,
date_taken: OffsetDateTime,
date_taken: DateTime<Utc>,
) -> Result<String> {
struct Item {
uuid: String,
Expand Down Expand Up @@ -159,7 +160,7 @@ impl Database for SqliteDatabase {
.bind(&user_id.to_string())
.bind(&name.to_string())
.bind(false)
.bind(OffsetDateTime::now_utc())
.bind(Utc::now())
.bind(date_taken)
.execute(&self.pool)
.await;
Expand Down Expand Up @@ -217,7 +218,6 @@ mod tests {
use super::*;
use std::path::PathBuf;
use testdir::testdir;
use time::format_description::well_known::Rfc3339;

#[sqlx::test]
async fn create_user_should_succeed(pool: SqlitePool) -> Result<()> {
Expand All @@ -236,7 +236,7 @@ mod tests {
lastname: Some("Stuermer".into()),
firstname: Some("Benjamin".into()),
is_locked: false,
created_at: OffsetDateTime::now_utc(),
created_at: Utc::now(),
updated_at: None,
last_login: None,
};
Expand Down Expand Up @@ -271,7 +271,7 @@ mod tests {
lastname: Some("Stuermer".into()),
firstname: Some("Benjamin".into()),
is_locked: false,
created_at: OffsetDateTime::now_utc(),
created_at: Utc::now(),
updated_at: None,
last_login: None,
};
Expand Down Expand Up @@ -415,7 +415,7 @@ mod tests {
.await?;

let name = "DSC_1234";
let date_taken = OffsetDateTime::now_utc();
let date_taken = Utc::now();

// when
let media_item_result = db.create_media_item(user_id, name, date_taken).await;
Expand All @@ -430,11 +430,14 @@ mod tests {
#[sqlx::test]
async fn create_media_item_should_return_existing_uuid(pool: SqlitePool) -> Result<()> {
// given

let user_id = "570DC079-664A-4496-BAA3-668C445A447";
let media_id = "ef9ac799-02f3-4b3f-9d96-7576be0434e6";
let added_at = OffsetDateTime::parse("2023-02-03T13:37:01.234567Z", &Rfc3339).unwrap();
let taken_at = OffsetDateTime::parse("2023-01-01T13:37:01.234567Z", &Rfc3339).unwrap();
let added_at = "2023-02-03T13:37:01.234567Z"
.parse::<DateTime<Utc>>()
.unwrap();
let taken_at = "2023-01-01T13:37:01.234567Z"
.parse::<DateTime<Utc>>()
.unwrap();
let name = "DSC_1234";

// create fake user - used as FOREIGN KEY in media
Expand Down Expand Up @@ -477,8 +480,12 @@ mod tests {
let user_id = "570DC079-664A-4496-BAA3-668C445A447";
let media_id = "ef9ac799-02f3-4b3f-9d96-7576be0434e6";
let reference_id = "ef9ac799-02f3-4b3f-9d96-7576be0434e6";
let added_at = OffsetDateTime::parse("2023-02-03T13:37:01.234567Z", &Rfc3339).unwrap();
let taken_at = OffsetDateTime::parse("2023-01-01T13:37:01.234567Z", &Rfc3339).unwrap();
let added_at = "2023-02-03T13:37:01.234567Z"
.parse::<DateTime<Utc>>()
.unwrap();
let taken_at = "2023-01-01T13:37:01.234567Z"
.parse::<DateTime<Utc>>()
.unwrap();
// create fake user - used as FOREIGN KEY in reference
sqlx::query("INSERT INTO users (uuid, email, password, lastname, firstname) VALUES ($1, $2, $3, $4, $5)")
.bind(user_id)
Expand Down Expand Up @@ -514,7 +521,9 @@ mod tests {
filename: filename.to_string(),
size: metadata.len(),
description: "",
last_modified: OffsetDateTime::parse("2023-02-03T13:37:01.234567Z", &Rfc3339).unwrap(),
last_modified: "2023-02-03T13:37:01.234567Z"
.parse::<DateTime<Utc>>()
.unwrap(),
is_missing: false,
};

Expand Down
5 changes: 2 additions & 3 deletions crates/media/src/api/routes/post_media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ use axum::{
};
use common::auth::user::User;
use serde::{Deserialize, Serialize};
use time::format_description::well_known::Rfc3339;
use time::OffsetDateTime;
use sqlx::types::chrono::{DateTime, Utc};
use tracing::{debug, info};
use uuid::Uuid;

Expand Down Expand Up @@ -61,7 +60,7 @@ pub(crate) async fn post_media(
return Err(StatusCode::BAD_REQUEST);
}

let date = OffsetDateTime::parse(date_taken.unwrap().as_str(), &Rfc3339);
let date = date_taken.unwrap().parse::<DateTime<Utc>>();
if date.is_err() {
return Err(StatusCode::BAD_REQUEST);
}
Expand Down
Loading

0 comments on commit 51f4edb

Please sign in to comment.