You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.0 KiB
88 lines
2.0 KiB
3 years ago
|
package model
|
||
|
|
||
|
type DiaryEntryNote struct {
|
||
|
Id int `json:"id"`
|
||
|
Body string `json:"body"`
|
||
|
CreatedAt int `json:"created_at"`
|
||
|
Version int `json:"version"`
|
||
|
LogId int `json:"log_id"`
|
||
|
}
|
||
|
|
||
|
func (app *App) NewDiaryEntryNote(entry_id int, 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 int) []*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 int
|
||
|
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 int) (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
|
||
|
}
|