diff --git a/server/nm-store/src/errors.rs b/server/nm-store/src/errors.rs index 1af8672..76c7247 100644 --- a/server/nm-store/src/errors.rs +++ b/server/nm-store/src/errors.rs @@ -9,6 +9,9 @@ pub enum NoteStoreError { #[error("Invalid Note Structure")] InvalidNoteStructure(String), + #[error("Not found")] + NotFound, + /// All other errors from the database. #[error(transparent)] DBError(#[from] sqlx::Error), diff --git a/server/nm-store/src/lib.rs b/server/nm-store/src/lib.rs index 3ce99d3..3edfe1b 100644 --- a/server/nm-store/src/lib.rs +++ b/server/nm-store/src/lib.rs @@ -70,4 +70,30 @@ mod tests { assert_eq!(page.title, "Test page"); assert!(page.note_id > 0); } + + #[tokio::test(threaded_scheduler)] + async fn reports_note_update_failure() { + let storagepool = fresh_inmemory_database().await; + let note_id = storagepool.insert_note("noteid", "notecontent", "note").await; + assert!(note_id.is_ok(), "{:?}", note_id); + + let update = storagepool.update_raw_note("badnote", "Bad Note Content").await; + assert!(update.is_err()); + } + + + #[tokio::test(threaded_scheduler)] + async fn can_update_a_note() { + let storagepool = fresh_inmemory_database().await; + let note_id = storagepool.insert_note("noteid", "notecontent", "note").await; + assert!(note_id.is_ok(), "{:?}", note_id); + + let update = storagepool.update_raw_note("noteid", "Good Note Content").await; + assert!(update.is_ok(), "{:?}", update); + + let note = storagepool.fetch_raw_note("noteid").await; + assert!(note.is_ok(), "{:?}", note); + let note = note.unwrap(); + assert_eq!(note.content, "Good Note Content"); + } } diff --git a/server/nm-store/src/sql/update_one_note.sql b/server/nm-store/src/sql/update_one_note.sql new file mode 100644 index 0000000..2492905 --- /dev/null +++ b/server/nm-store/src/sql/update_one_note.sql @@ -0,0 +1,3 @@ +UPDATE notes + SET content = ?, updated_date = ?, lastview_date = ? + WHERE uuid = ?; diff --git a/server/nm-store/src/store.rs b/server/nm-store/src/store.rs index cc19d0a..bd8ff6f 100644 --- a/server/nm-store/src/store.rs +++ b/server/nm-store/src/store.rs @@ -3,8 +3,7 @@ use crate::structs::{RawNote, RawPage}; use chrono; use friendly_id; use sqlx; -use sqlx::sqlite::SqlitePool; -use sqlx::{sqlite::Sqlite, Executor}; +use sqlx::{sqlite::{Sqlite, SqlitePool}, Executor, Done}; use std::sync::Arc; /// A handle to our Sqlite database. @@ -60,6 +59,22 @@ impl NoteStore { insert_note(&*self.0, id, content, notetype).await } + pub async fn update_raw_note(&self, id: &str, content: &str) -> NoteResult<()> { + let update_one_note_sql = include_str!("sql/update_one_note.sql"); + let now = chrono::Utc::now(); + let rows_updated = sqlx::query(update_one_note_sql) + .bind(&content) + .bind(&now) + .bind(&now) + .bind(&id) + .execute(&*self.0).await? + .rows_affected(); + match rows_updated { + 1 => Ok(()), + _ => Err(NoteStoreError::NotFound) + } + } + // TODO: We're returning the raw page with the raw note id, note // the friendly ID. Is there a disconnect there? It's making me // furiously to think.