From 8bc8b8458376e77a386e010c9c0d0b2c9ab27655 Mon Sep 17 00:00:00 2001 From: isthisnagee Date: Tue, 28 Dec 2021 20:16:13 -0800 Subject: [PATCH] Delete notes when deleting entry The notes that the user will delete is also shown to them --- cmd/delete.go | 5 ++++- model/note.go | 17 +++++++++++++++++ model/note_test.go | 23 +++++++++++++++++++++-- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/cmd/delete.go b/cmd/delete.go index d5eba90..71d7ceb 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -25,11 +25,13 @@ var deleteCmd = &cobra.Command{ return err } entry, err := App.GetDiaryEntry(int64(id)) + notes := App.GetDiaryEntryNotes(int64(id)) if err != nil { return err } fmt.Println("Are you sure you want to delete the following entry? (y/n)") - fmt.Println(entry.Title) + PrintEntry(entry) + PrintNotes(notes) var response string fmt.Scanln(&response) if response == "y" { @@ -46,6 +48,7 @@ var deleteCmd = &cobra.Command{ panic(err) } _, err = App.DeleteDiaryEntry(int64(id)) + _, err = App.DeleteNotesForDiaryEntry(int64(id)) if err != nil { panic(err) } diff --git a/model/note.go b/model/note.go index 05d224d..48d18b2 100644 --- a/model/note.go +++ b/model/note.go @@ -85,3 +85,20 @@ func (app *App) DeleteDiaryEntryNote(note_id int64) (bool, error) { } return true, nil } + +func (app *App) DeleteNotesForDiaryEntry(entry_id int64) (bool, error) { + result, err := app.Db.Exec("DELETE FROM diary_log_note where log_id=?", entry_id) + if err != nil { + return false, err + } + + num_rows_affected, err := result.RowsAffected() + if err != nil { + return false, err + } + if num_rows_affected == 0 { + // the entry had no notes! + return true, nil + } + return true, nil +} diff --git a/model/note_test.go b/model/note_test.go index 0044c63..ed7226e 100644 --- a/model/note_test.go +++ b/model/note_test.go @@ -41,7 +41,7 @@ func TestGetDiaryEntryNotes(t *testing.T) { teardown(app) } -func DeleteDiaryEntryNote(t *testing.T) { +func TestDeleteDiaryEntryNote(t *testing.T) { var app = setup() var entry = app.NewDiaryEntry("Met with Nagee @ 1PM") @@ -53,7 +53,26 @@ func DeleteDiaryEntryNote(t *testing.T) { teardown(app) } -func DeleteDiaryEntryNoteNotFound(t *testing.T) { +func TestDeleteNotesForDiaryEntry(t *testing.T) { + var app = setup() + + var entry = app.NewDiaryEntry("Met with Nagee @ 1PM") + app.NewDiaryEntryNote(entry.Id, "A note") + app.NewDiaryEntryNote(entry.Id, "Another note") + + notes := app.GetDiaryEntryNotes(entry.Id) + assert_int(t, 2, int64(len(notes))) + + is_deleted, _ := app.DeleteNotesForDiaryEntry(entry.Id) + assert_bool(t, true, is_deleted) + + notes = app.GetDiaryEntryNotes(entry.Id) + assert_int(t, 0, int64(len(notes))) + + teardown(app) +} + +func TestDeleteDiaryEntryNoteNotFound(t *testing.T) { var app = setup() _, err := app.DeleteDiaryEntryNote(-1)