From 71dfc479d8bc32c5beb5a9c68e148e755fc217ac Mon Sep 17 00:00:00 2001 From: "Elf M. Sternberg" Date: Tue, 29 Sep 2020 07:44:43 -0700 Subject: [PATCH] FEAT Can get a raw note. TEST Asserts a non-existent raw note is non-existent. --- server/nm-store/src/lib.rs | 18 ++++++++++++++++-- server/nm-store/src/sql/select_one_note.sql | 1 + server/nm-store/src/store.rs | 11 ++++++++++- server/nm-store/src/structs.rs | 8 ++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 server/nm-store/src/sql/select_one_note.sql diff --git a/server/nm-store/src/lib.rs b/server/nm-store/src/lib.rs index 3958321..12ec374 100644 --- a/server/nm-store/src/lib.rs +++ b/server/nm-store/src/lib.rs @@ -11,13 +11,27 @@ mod tests { use super::*; use tokio; - #[tokio::test(threaded_scheduler)] - async fn it_works() { + async fn fresh_inmemory_database() -> NoteStore { let storagepool = NoteStore::new("sqlite://:memory:").await; assert!(storagepool.is_ok()); let storagepool = storagepool.unwrap(); assert!(storagepool.reset_database().await.is_ok()); + storagepool + } + + #[tokio::test(threaded_scheduler)] + async fn fetching_unfound_page_works() { + let storagepool = fresh_inmemory_database().await; let unfoundpage = storagepool.fetch_page("nonexistent-page").await; assert!(unfoundpage.is_err()); } + + #[tokio::test(threaded_scheduler)] + async fn fetching_unfound_note_works() { + let storagepool = fresh_inmemory_database().await; + let unfoundnote = storagepool.fetch_note("nonexistent-note").await; + assert!(unfoundnote.is_err()); + } + + } diff --git a/server/nm-store/src/sql/select_one_note.sql b/server/nm-store/src/sql/select_one_note.sql new file mode 100644 index 0000000..38ab8bb --- /dev/null +++ b/server/nm-store/src/sql/select_one_note.sql @@ -0,0 +1 @@ +SELECT id, uuid, content, notetype FROM notes WHERE uuid=?; diff --git a/server/nm-store/src/store.rs b/server/nm-store/src/store.rs index bdc282a..ceb880f 100644 --- a/server/nm-store/src/store.rs +++ b/server/nm-store/src/store.rs @@ -2,7 +2,7 @@ use sqlx; use sqlx::sqlite::SqlitePool; use std::sync::Arc; use crate::errors::NoteStoreError; -use crate::structs::RawPage; +use crate::structs::{RawPage, RawNote}; /// A handle to our Sqlite database. #[derive(Clone)] @@ -34,4 +34,13 @@ impl NoteStore { .fetch_one(&*self.0) .await } + + pub async fn fetch_note(&self, id: &str) -> SqlResult { + 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 + } + } diff --git a/server/nm-store/src/structs.rs b/server/nm-store/src/structs.rs index 1df84e0..115f4ef 100644 --- a/server/nm-store/src/structs.rs +++ b/server/nm-store/src/structs.rs @@ -8,3 +8,11 @@ pub struct RawPage { title: String, note_id: i64, } + +#[derive(Clone, Serialize, Deserialize, Debug, FromRow)] +pub struct RawNote { + id: i64, + uuid: String, + content: String, + notetype: String, +}