diff --git a/server/nm-store/src/lib.rs b/server/nm-store/src/lib.rs index b2afecc..bccef41 100644 --- a/server/nm-store/src/lib.rs +++ b/server/nm-store/src/lib.rs @@ -42,20 +42,20 @@ mod tests { assert!(unfoundnote.is_err()); } -// #[tokio::test(threaded_scheduler)] -// async fn can_save_a_note() { -// let storagepool = fresh_inmemory_database().await; -// let note_id = storagepool.store_note("noteid", "notecontent", "note").await; -// assert!(note_id.is_ok()); -// let note_id = note_id.unwrap(); -// assert!(note_id > 0); -// -// let foundnote = storepool.fetch_note("noteid").await; -// assert!(foundnote.is_ok()); -// let foundnote = foundnote.unwrap(); -// assert_eq!(foundnote.content, "notecontent"); -// assert_eq!(foundnote.notetype, "note"); -// } + #[tokio::test(threaded_scheduler)] + async fn can_save_a_note() { + let storagepool = fresh_inmemory_database().await; + let note_id = storagepool.insert_note("noteid", "notecontent", "note").await; + assert!(note_id.is_ok(), format!("note is not ok: {:?}", note_id)); + let note_id = note_id.unwrap(); + assert!(note_id > 0); + + let foundnote = storagepool.fetch_note("noteid").await; + assert!(foundnote.is_ok(), format!("foundnote is not ok: {:?}", foundnote)); + let foundnote = foundnote.unwrap(); + assert_eq!(foundnote.content, "notecontent"); + assert_eq!(foundnote.notetype, "note"); + } } diff --git a/server/nm-store/src/sql/initialize_database.sql b/server/nm-store/src/sql/initialize_database.sql index 7bdd434..933ff9a 100644 --- a/server/nm-store/src/sql/initialize_database.sql +++ b/server/nm-store/src/sql/initialize_database.sql @@ -12,7 +12,7 @@ CREATE TABLE notes ( creation_date DATETIME NOT NULL, updated_date DATETIME NOT NULL, lastview_date DATETIME NOT NULL, - deleted_date DATETIME NOT NULL + deleted_date DATETIME NULL ); CREATE INDEX notes_uuids ON notes (uuid); @@ -25,7 +25,7 @@ CREATE TABLE pages ( creation_date DATETIME NOT NULL, updated_date DATETIME NOT NULL, lastview_date DATETIME NOT NULL, - deleted_date DATETIME NOT NULL, + deleted_date DATETIME NULL, FOREIGN KEY (note_id) REFERENCES notes (id) ON DELETE NO ACTION ON UPDATE NO ACTION ); diff --git a/server/nm-store/src/sql/insert_one_note.sql b/server/nm-store/src/sql/insert_one_note.sql new file mode 100644 index 0000000..fdefb2c --- /dev/null +++ b/server/nm-store/src/sql/insert_one_note.sql @@ -0,0 +1,8 @@ +INSERT INTO notes ( + uuid, + content, + notetype, + creation_date, + updated_date, + lastview_date) +VALUES (?, ?, ?, ?, ?, ?); diff --git a/server/nm-store/src/sql/select_one_note.sql b/server/nm-store/src/sql/select_one_note.sql index 38ab8bb..8e7b16a 100644 --- a/server/nm-store/src/sql/select_one_note.sql +++ b/server/nm-store/src/sql/select_one_note.sql @@ -1 +1 @@ -SELECT id, uuid, content, notetype FROM notes WHERE uuid=?; +SELECT id, uuid, content, notetype, creation_date, updated_date, lastview_date, deleted_date FROM notes WHERE uuid=?; diff --git a/server/nm-store/src/store.rs b/server/nm-store/src/store.rs index 8d04324..6e78b75 100644 --- a/server/nm-store/src/store.rs +++ b/server/nm-store/src/store.rs @@ -1,5 +1,6 @@ use crate::errors::NoteStoreError; use crate::structs::{RawNote, RawPage}; +use chrono; use sqlx; use sqlx::sqlite::SqlitePool; use std::sync::Arc; @@ -34,4 +35,18 @@ impl NoteStore { let select_one_note_sql = include_str!("sql/select_one_note.sql"); sqlx::query_as(select_one_note_sql).bind(&id).fetch_one(&*self.0).await } + + pub async fn insert_note(&self, id: &str, content: &str, notetype: &str) -> SqlResult { + let insert_one_note_sql = include_str!("sql/insert_one_note.sql"); + let now = chrono::Utc::now(); + Ok(sqlx::query(insert_one_note_sql) + .bind(&id) + .bind(&content) + .bind(¬etype) + .bind(&now) + .bind(&now) + .bind(&now) + .execute(&*self.0).await? + .last_insert_rowid()) + } } diff --git a/server/nm-store/src/structs.rs b/server/nm-store/src/structs.rs index 4ea89ba..b2fe6bd 100644 --- a/server/nm-store/src/structs.rs +++ b/server/nm-store/src/structs.rs @@ -4,24 +4,24 @@ use sqlx::{self, FromRow}; #[derive(Clone, Serialize, Deserialize, Debug, FromRow)] pub struct RawPage { - id: i64, - slug: String, - title: String, - note_id: i64, - creation_date: DateTime, - updated_date: DateTime, - lastview_date: DateTime, - deleted_date: DateTime, + pub id: i64, + pub slug: String, + pub title: String, + pub note_id: i64, + pub creation_date: DateTime, + pub updated_date: DateTime, + pub lastview_date: DateTime, + pub deleted_date: Option>, } #[derive(Clone, Serialize, Deserialize, Debug, FromRow)] pub struct RawNote { - id: i64, - uuid: String, - content: String, - notetype: String, - creation_date: DateTime, - updated_date: DateTime, - lastview_date: DateTime, - deleted_date: DateTime, + pub id: i64, + pub uuid: String, + pub content: String, + pub notetype: String, + pub creation_date: DateTime, + pub updated_date: DateTime, + pub lastview_date: DateTime, + pub deleted_date: Option>, }