From 2eeb96a26a19e542c37c761260999b16045780af Mon Sep 17 00:00:00 2001 From: "Elf M. Sternberg" Date: Thu, 5 Nov 2020 09:34:21 -0800 Subject: [PATCH] Enabling the tree-handling API. --- server/nm-store/src/store.rs | 39 +++++++++++++------------- server/nm-trees/src/lib.rs | 53 +++++++++++++++++++++--------------- 2 files changed, 51 insertions(+), 41 deletions(-) diff --git a/server/nm-store/src/store.rs b/server/nm-store/src/store.rs index 0c9d8a5..bc7982b 100644 --- a/server/nm-store/src/store.rs +++ b/server/nm-store/src/store.rs @@ -137,7 +137,7 @@ impl NoteStore { Ok((vec![Note::from(zettlekasten)], vec![])) } - pub async fn add_note(&self, note: &NewNote, parent_id: &str, location: Option) -> NoteResult { + pub async fn add_note(&self, note: &NewNote) -> NoteResult { self.insert_note( note, &ParentId(parent_id.to_string()), @@ -176,24 +176,6 @@ impl NoteStore { Ok(()) } - /// Deletes a note. If the note's relationship drops to zero, all - /// references from that note to pages are also deleted. - pub async fn delete_note(&self, note_id: &str, note_parent_id: &str) -> NoteResult<()> { - let mut tx = self.0.begin().await?; - let note_id = NoteId(note_id.to_string()); - let parent_id = ParentId(note_parent_id.to_string()); - - let _ = delete_note_to_note_relationship(&mut tx, &parent_id, ¬e_id); - // The big one: if zero parents report having an interest in this note, then it, - // *and any sub-relationships*, go away. - if count_existing_note_relationships(&mut tx, ¬e_id).await? == 0 { - let _ = delete_note_to_kasten_relationships(&mut tx, ¬e_id).await?; - let _ = delete_note(&mut tx, ¬e_id).await?; - } - tx.commit().await?; - Ok(()) - } - /// Updates a note's content. Completely rebuilds the note's /// outgoing edge reference list every time. pub async fn update_note_content(&self, note_id: &str, content: &str) -> NoteResult<()> { @@ -218,6 +200,25 @@ impl NoteStore { tx.commit().await?; Ok(()) } + + /// Deletes a note. If the note's relationship drops to zero, all + /// references from that note to pages are also deleted. + pub async fn delete_note(&self, note_id: &str, note_parent_id: &str) -> NoteResult<()> { + let mut tx = self.0.begin().await?; + let note_id = NoteId(note_id.to_string()); + let parent_id = ParentId(note_parent_id.to_string()); + + let _ = delete_note_to_note_relationship(&mut tx, &parent_id, ¬e_id); + // The big one: if zero parents report having an interest in this note, then it, + // *and any sub-relationships*, go away. + if count_existing_note_relationships(&mut tx, ¬e_id).await? == 0 { + let _ = delete_note_to_kasten_relationships(&mut tx, ¬e_id).await?; + let _ = delete_note(&mut tx, ¬e_id).await?; + } + tx.commit().await?; + Ok(()) + } + } // The Private stuff diff --git a/server/nm-trees/src/lib.rs b/server/nm-trees/src/lib.rs index 2e280a2..617ec68 100644 --- a/server/nm-trees/src/lib.rs +++ b/server/nm-trees/src/lib.rs @@ -12,6 +12,7 @@ mod make_tree; mod structs; +use chrono::{DateTime, Utc}; use nm_store::{NoteStore, NoteStoreError, NewNote}; use crate::structs::{Page, Note}; use crate::make_tree::{make_note_tree, make_backreferences}; @@ -54,40 +55,48 @@ impl Notesmachine { // You should be able to: // Add a note that has no parent (gets added to "today") // Add a note that specifies only the page (gets added to page/root) - // Add a note that has no position (gets tacked onto the end of the above) + // Add a note that has no location (gets tacked onto the end of the above) // Add a note that specifies the date of creation. - pub async fn add_note(&self, note: &NewNote) -> Result<()> { - todo!(); + pub async fn add_note(&self, note: &NewNote) -> Result { + let mut note = note.clone(); + if note.parent_id.is_none() { + let (parent, _) = self.get_today_page().await?; + note.parent_id = parent.id; + } + Ok(self.0.add_note(¬e)) } - pub async fn add_note_to_page(&self, note: &NewNote) -> Result<()> { - todo!(); - } +// pub async fn reference_note(&self, note_id: &str, new_parent_id: &str, new_location: i64) -> Result<()> { +// todo!(); +// } +// +// pub async fn embed_note(&self, note_id: &str, new_parent_id: &str, new_location: i64) -> Result<()> { +// todo!(); +// } - pub async fn add_note_to_today(&self, note: &NewNote) -> Result<()> { - todo!(); - } - - pub async fn reference_note(&self, note_id: &str, new_parent_id: &str, new_position: i64) -> Result<()> { - todo!(); - } - - pub async fn embed_note(&self, note_id: &str, new_parent_id: &str, new_position: i64) -> Result<()> { - todo!(); - } - - pub async fn move_note(&self, note_id: &str, old_parent_id: &str, new_parent_id: &str, position: i64) -> Result<()> { - todo!(); + pub async fn move_note(&self, note_id: &str, old_parent_id: &str, new_parent_id: &str, location: i64) -> Result<()> { + self.0.move_note(note_id, old_parent_id, new_parent_id, location) } pub async fn update_note(&self, note_id: &str, content: &str) -> Result<()> { - todo!(); + self.0.update_note(note_id, content) } pub async fn delete_note(&self, note_id: &str) -> Result<()> { - todo!(); + self.0.delete_note(note_id) } } + +// Private stuff + +impl Notesmachine { + async fn get_today_page(&self) -> Result { + let title = chrono::Utc::now().format("%F").to_string(); + let (rawtree, _) = self.0.get_kasten_by_title(title).await?; + Ok(rawtree.id) + } +} + #[cfg(test)] mod tests {