Skip to content
This repository has been archived by the owner on Nov 18, 2024. It is now read-only.

Commit

Permalink
fix API by making id a INTEGER PRIMARY KEY, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatFrogDev committed Feb 16, 2024
1 parent 98db76b commit 7f1e848
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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![&note.id, &note.name, &note.content, &note.created],
)?;

Expand Down
24 changes: 12 additions & 12 deletions src/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl Note {
}

pub fn show(db_file: &PathBuf) -> Result<(), Box<dyn std::error::Error>> {
cursor_to_origin()?;
let saved_notes = api::get_notes(db_file)?;

if saved_notes.is_empty() {
Expand All @@ -73,13 +74,14 @@ impl Note {
}

pub fn edit(db_file: &PathBuf) -> Result<(), Box<dyn std::error::Error>> {
cursor_to_origin()?;
let saved_notes = api::get_notes(db_file)?;
let mut options: Vec<String> = 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(),
Expand All @@ -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.");
Expand All @@ -109,6 +104,7 @@ impl Note {
}

pub fn delete(db_file: &PathBuf) -> Result<(), Box<dyn std::error::Error>> {
cursor_to_origin()?;
// yep yep yeah
if api::get_notes(db_file)?.is_empty() {
println!("You can't delete notes, because there are none.");
Expand All @@ -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(());
}
}
}

0 comments on commit 7f1e848

Please sign in to comment.