Skip to content

Commit

Permalink
WIP feat: save metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonribble committed Sep 21, 2024
1 parent d6d4555 commit aeab3f4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 19 deletions.
55 changes: 47 additions & 8 deletions src/db/metadata_repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::sync::Arc;

use crate::models;
use async_trait::async_trait;
use chrono::SecondsFormat;
use sqlx::SqlitePool;

#[cfg_attr(test, mockall::automock)]
Expand All @@ -25,7 +26,34 @@ impl SqliteMetadataRepo {
#[async_trait]
impl MetadataRepo for SqliteMetadataRepo {
async fn create(&self, metadata: models::Metadata) -> anyhow::Result<i64> {
todo!();
let query = "INSERT INTO contact_metadata
(contact_id,
starred,
is_archived,
frequency,
created_at,
updated_at,
last_seen_at,
next_reminder_at,
last_reminder_at)
VALUES (?,?,?,?,?,?,?,?,?)";

let result = sqlx::query(query)
.bind(&metadata.contact_id)
.bind(&metadata.starred)
.bind(&metadata.is_archived)
.bind(&metadata.frequency)

.bind(metadata.created_at.to_rfc3339_opts(SecondsFormat::Millis, true))
.bind(metadata.updated_at.to_rfc3339_opts(SecondsFormat::Millis, true))

.bind(metadata.last_seen_at.map(|dt| dt.to_rfc3339_opts(SecondsFormat::Millis, true)))
.bind(metadata.next_reminder_at.map(|dt| dt.to_rfc3339_opts(SecondsFormat::Millis, true)))
.bind(metadata.last_reminder_at.map(|dt| dt.to_rfc3339_opts(SecondsFormat::Millis, true)))
.execute(&*self.sqlite_pool)
.await?;

Ok(result.last_insert_rowid())
}
}

Expand All @@ -42,15 +70,26 @@ mod tests {
.connect("sqlite::memory:")
.await
.expect("Failed to create in-memory SQLite database");

sqlx::query("CREATE TABLE metadata (id INTEGER PRIMARY KEY, name TEXT)")
.execute(&pool)
.await
.expect("Failed to create metadata table");


sqlx::query(
"CREATE TABLE IF NOT EXISTS contact_metadata (
contact_id INTEGER NOT NULL,
starred BOOLEAN NOT NULL,
is_archived BOOLEAN NOT NULL,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL,
last_seen_at TEXT,
next_reminder_at TEXT,
frequency INTEGER,
last_reminder_at TEXT
)"
)
.execute(&pool)
.await
.expect("Failed to create contact_metadata table");

pool
}

#[tokio::test]
async fn test_create_metadata_sqlite() {
let pool = setup_test_db().await;
Expand Down
22 changes: 11 additions & 11 deletions src/models/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use chrono::{DateTime, Utc};
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Metadata {
pub contact_id: i64,
starred: bool,
is_archived: bool,
create_at: DateTime<Utc>,
updated_at: DateTime<Utc>,
last_seen_at: Option<DateTime<Utc>>,
next_reminder_at: Option<DateTime<Utc>>,
frequency: Option<String>,
last_reminder_at: Option<DateTime<Utc>>,
pub starred: bool,
pub is_archived: bool,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub last_seen_at: Option<DateTime<Utc>>,
pub next_reminder_at: Option<DateTime<Utc>>,
pub frequency: Option<String>,
pub last_reminder_at: Option<DateTime<Utc>>,
}

impl Metadata {
Expand All @@ -22,7 +22,7 @@ impl Metadata {
contact_id: 0,
starred: false,
is_archived: false,
create_at: now,
created_at: now,
updated_at: now,
last_seen_at: None,
next_reminder_at: None,
Expand All @@ -48,7 +48,7 @@ mod tests {
contact_id: 0,
starred: false,
is_archived: false,
create_at: now,
created_at: now,
updated_at: now,
last_seen_at: None,
next_reminder_at: None,
Expand All @@ -62,7 +62,7 @@ mod tests {
assert_eq!(default_metadata.is_archived, expected_metadata.is_archived);

let half_second = Duration::milliseconds(500);
let created_at_time_difference = expected_metadata.create_at - default_metadata.create_at;
let created_at_time_difference = expected_metadata.created_at - default_metadata.created_at;

assert!(created_at_time_difference >= Duration::zero());
assert!(created_at_time_difference < half_second);
Expand Down

0 comments on commit aeab3f4

Please sign in to comment.