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
}
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)
}

@ -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
}

@ -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)

Loading…
Cancel
Save