Enabling the tree-handling API.

This commit is contained in:
Elf M. Sternberg 2020-11-05 09:34:21 -08:00
parent 83d5858a45
commit 2eeb96a26a
2 changed files with 51 additions and 41 deletions

View File

@ -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<i64>) -> NoteResult<String> {
pub async fn add_note(&self, note: &NewNote) -> NoteResult<String> {
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, &note_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, &note_id).await? == 0 {
let _ = delete_note_to_kasten_relationships(&mut tx, &note_id).await?;
let _ = delete_note(&mut tx, &note_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, &note_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, &note_id).await? == 0 {
let _ = delete_note_to_kasten_relationships(&mut tx, &note_id).await?;
let _ = delete_note(&mut tx, &note_id).await?;
}
tx.commit().await?;
Ok(())
}
}
// The Private stuff

View File

@ -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,41 +55,49 @@ 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<String> {
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(&note))
}
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<String> {
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 {
use super::*;