package model type DiaryEntryNote struct { Id int64 `json:"id"` Body string `json:"body"` CreatedAt int64 `json:"created_at"` Version int64 `json:"version"` LogId int64 `json:"log_id"` } func (app *App) NewDiaryEntryNote(entry_id int64, body string) *DiaryEntryNote { var diary_entry_note DiaryEntryNote var result, err = app.Db.Exec("INSERT INTO diary_log_note (body, log_id) values (?, ?);", body, entry_id) if err != nil { panic(err) } id, err := result.LastInsertId() if err != nil { panic(err) } if err := app.Db.QueryRow( "SELECT id, body, created_at, version, log_id FROM diary_log_note where id=?", id, ).Scan( &diary_entry_note.Id, &diary_entry_note.Body, &diary_entry_note.CreatedAt, &diary_entry_note.Version, &diary_entry_note.LogId, ); err != nil { panic(err) } return &diary_entry_note } func (app *App) GetDiaryEntryNotes(entry_id int64) []*DiaryEntryNote { // not sure if it should be descending. rows, err := app.Db.Query( // The second ordering is necessary to break ties. `SELECT id, body, created_at, version, log_id FROM diary_log_note WHERE log_id=? ORDER BY created_at desc, id desc`, entry_id, ) if err != nil { panic(err) } defer rows.Close() var result []*DiaryEntryNote for rows.Next() { var id, created_at, version, log_id int64 var body string err := rows.Scan(&id, &body, &created_at, &version, &log_id) if err != nil { panic(err) } result = append(result, &DiaryEntryNote{ Id: id, Body: body, CreatedAt: created_at, Version: version, LogId: log_id, }) } if err := rows.Err(); err != nil { panic(err) } return result } func (app *App) DeleteDiaryEntryNote(note_id int64) (bool, error) { result, err := app.Db.Exec("DELETE FROM diary_log_note where id=?", note_id) if err != nil { return false, err } num_rows_affected, err := result.RowsAffected() if err != nil { return false, err } if num_rows_affected == 0 { return false, &NotFoundError{given_id: note_id} } return true, nil }