FEAT Can update a raw note.

This commit is contained in:
Elf M. Sternberg 2020-09-30 09:17:37 -07:00
parent 1565fef001
commit 0f5d15ad14
4 changed files with 49 additions and 2 deletions

View File

@ -9,6 +9,9 @@ pub enum NoteStoreError {
#[error("Invalid Note Structure")] #[error("Invalid Note Structure")]
InvalidNoteStructure(String), InvalidNoteStructure(String),
#[error("Not found")]
NotFound,
/// All other errors from the database. /// All other errors from the database.
#[error(transparent)] #[error(transparent)]
DBError(#[from] sqlx::Error), DBError(#[from] sqlx::Error),

View File

@ -70,4 +70,30 @@ mod tests {
assert_eq!(page.title, "Test page"); assert_eq!(page.title, "Test page");
assert!(page.note_id > 0); 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");
}
} }

View File

@ -0,0 +1,3 @@
UPDATE notes
SET content = ?, updated_date = ?, lastview_date = ?
WHERE uuid = ?;

View File

@ -3,8 +3,7 @@ use crate::structs::{RawNote, RawPage};
use chrono; use chrono;
use friendly_id; use friendly_id;
use sqlx; use sqlx;
use sqlx::sqlite::SqlitePool; use sqlx::{sqlite::{Sqlite, SqlitePool}, Executor, Done};
use sqlx::{sqlite::Sqlite, Executor};
use std::sync::Arc; use std::sync::Arc;
/// A handle to our Sqlite database. /// A handle to our Sqlite database.
@ -60,6 +59,22 @@ impl NoteStore {
insert_note(&*self.0, id, content, notetype).await 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 // 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 // the friendly ID. Is there a disconnect there? It's making me
// furiously to think. // furiously to think.