Delete notes when deleting entry

The notes that the user will delete is also shown to them
pull/8/head
isthisnagee 3 years ago
parent b7d278f149
commit 8bc8b84583

@ -25,11 +25,13 @@ var deleteCmd = &cobra.Command{
return err return err
} }
entry, err := App.GetDiaryEntry(int64(id)) entry, err := App.GetDiaryEntry(int64(id))
notes := App.GetDiaryEntryNotes(int64(id))
if err != nil { if err != nil {
return err return err
} }
fmt.Println("Are you sure you want to delete the following entry? (y/n)") 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 var response string
fmt.Scanln(&response) fmt.Scanln(&response)
if response == "y" { if response == "y" {
@ -46,6 +48,7 @@ var deleteCmd = &cobra.Command{
panic(err) panic(err)
} }
_, err = App.DeleteDiaryEntry(int64(id)) _, err = App.DeleteDiaryEntry(int64(id))
_, err = App.DeleteNotesForDiaryEntry(int64(id))
if err != nil { if err != nil {
panic(err) panic(err)
} }

@ -85,3 +85,20 @@ func (app *App) DeleteDiaryEntryNote(note_id int64) (bool, error) {
} }
return true, nil 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
}

@ -41,7 +41,7 @@ func TestGetDiaryEntryNotes(t *testing.T) {
teardown(app) teardown(app)
} }
func DeleteDiaryEntryNote(t *testing.T) { func TestDeleteDiaryEntryNote(t *testing.T) {
var app = setup() var app = setup()
var entry = app.NewDiaryEntry("Met with Nagee @ 1PM") var entry = app.NewDiaryEntry("Met with Nagee @ 1PM")
@ -53,7 +53,26 @@ func DeleteDiaryEntryNote(t *testing.T) {
teardown(app) 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() var app = setup()
_, err := app.DeleteDiaryEntryNote(-1) _, err := app.DeleteDiaryEntryNote(-1)

Loading…
Cancel
Save