From 7f1e848278aab16df46edfbde335ff585b15ad63 Mon Sep 17 00:00:00 2001 From: ThatBoiDev Date: Fri, 16 Feb 2024 17:52:26 +0100 Subject: [PATCH] fix API by making id a INTEGER PRIMARY KEY, cleanup --- src/api.rs | 4 ++-- src/note.rs | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/api.rs b/src/api.rs index b71f19e..c55d935 100644 --- a/src/api.rs +++ b/src/api.rs @@ -14,7 +14,7 @@ pub fn init_db( Connection::open(db_file)?.execute( "CREATE TABLE IF NOT EXISTS saved_notes ( - id INTEGER NOT NULL, + id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, content TEXT NOT NULL, created TEXT NOT NULL @@ -27,7 +27,7 @@ pub fn init_db( pub fn save_note(note: &Note, db_file: &PathBuf) -> Result<()> { Connection::open(db_file)?.execute( - "INSERT INTO saved_notes (id, name, content, created) VALUES (?1, ?2, ?3, ?4);", + "INSERT OR REPLACE INTO saved_notes (id, name, content, created) VALUES (?1, ?2, ?3, ?4);", params![¬e.id, ¬e.name, ¬e.content, ¬e.created], )?; diff --git a/src/note.rs b/src/note.rs index 898befe..378a7dc 100644 --- a/src/note.rs +++ b/src/note.rs @@ -55,6 +55,7 @@ impl Note { } pub fn show(db_file: &PathBuf) -> Result<(), Box> { + cursor_to_origin()?; let saved_notes = api::get_notes(db_file)?; if saved_notes.is_empty() { @@ -73,13 +74,14 @@ impl Note { } pub fn edit(db_file: &PathBuf) -> Result<(), Box> { + cursor_to_origin()?; let saved_notes = api::get_notes(db_file)?; let mut options: Vec = Vec::new(); truncate_note(&mut options, db_file)?; let selection = select("Select the note that you want to edit:", &options); let selected_note = &saved_notes[selection]; let updated_note = Note { - id: selection, + id: selected_note.id.clone(), name: input("Name:", selected_note.name.clone()), content: input("Content:", selected_note.content.clone()), created: selected_note.created.clone(), @@ -92,13 +94,6 @@ impl Note { match confirm("Are you sure that you want to edit this note?") { true => { - Connection::open(db_file)?.execute( - "UPDATE saved_notes - SET (name) = ?1, (content) = ?2 - WHERE id = ?3;", - params![&selected_note.name, &selected_note.content, &selection], - )?; - cursor_to_origin()?; api::save_note(&updated_note, db_file)?; // why the fuck whas this line not here yet println!("Note updated successfully."); @@ -109,6 +104,7 @@ impl Note { } pub fn delete(db_file: &PathBuf) -> Result<(), Box> { + cursor_to_origin()?; // yep yep yeah if api::get_notes(db_file)?.is_empty() { println!("You can't delete notes, because there are none."); @@ -124,19 +120,23 @@ impl Note { let mut prompt = "Are you sure that you want to delete these notes?"; if selections.len() == 1 { - prompt = "Are you sure that you want to delete this note?"; + prompt = "Are you sure that you want to delete this note?"; // i love this } cursor_to_origin()?; if selections.is_empty() { println!("You didn't select any notes."); - return Ok(()); + Ok(()) } else { if confirm(prompt) { api::delete_notes(selections, db_file)?; + cursor_to_origin()?; + println!("Notes deleted successfully."); + Ok(()) + } else { + cursor_to_origin()?; + Ok(()) } - println!("Notes deleted successfully."); - return Ok(()); } } }