parent
91b76a3a20
commit
afecf9628c
@ -0,0 +1,87 @@
|
||||
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
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewEntryNote(t *testing.T) {
|
||||
var app = setup()
|
||||
|
||||
var entry = app.NewDiaryEntry("Met with Nagee @ 1PM")
|
||||
var note = app.NewDiaryEntryNote(entry.Id, "A note")
|
||||
|
||||
assert_string(t, "A note", note.Body)
|
||||
assert_exists(t, note.Id)
|
||||
assert_exists(t, note.CreatedAt)
|
||||
assert_exists(t, note.Version)
|
||||
|
||||
teardown(app)
|
||||
}
|
||||
|
||||
func TestGetDiaryEntryNotes(t *testing.T) {
|
||||
var app = setup()
|
||||
|
||||
var entry = app.NewDiaryEntry("Met with Nagee @ 1PM")
|
||||
var note_1 = app.NewDiaryEntryNote(entry.Id, "A note")
|
||||
var note_2 = app.NewDiaryEntryNote(entry.Id, "Another note")
|
||||
|
||||
// The first note should be the latest one created
|
||||
var notes = app.GetDiaryEntryNotes(entry.Id)
|
||||
|
||||
assert_string(t, notes[0].Body, note_2.Body)
|
||||
assert_int(t, notes[0].Id, note_2.Id)
|
||||
assert_int(t, notes[0].CreatedAt, note_2.CreatedAt)
|
||||
assert_int(t, notes[0].Version, note_2.Version)
|
||||
|
||||
assert_string(t, notes[1].Body, note_1.Body)
|
||||
assert_int(t, notes[1].Id, note_1.Id)
|
||||
assert_int(t, notes[1].CreatedAt, note_1.CreatedAt)
|
||||
assert_int(t, notes[1].Version, note_1.Version)
|
||||
|
||||
teardown(app)
|
||||
}
|
||||
|
||||
func DeleteDiaryEntryNote(t *testing.T) {
|
||||
var app = setup()
|
||||
|
||||
var entry = app.NewDiaryEntry("Met with Nagee @ 1PM")
|
||||
var note = app.NewDiaryEntryNote(entry.Id, "A note")
|
||||
|
||||
is_deleted, _ := app.DeleteDiaryEntry(note.Id)
|
||||
assert_bool(t, true, is_deleted)
|
||||
|
||||
teardown(app)
|
||||
}
|
||||
|
||||
func DeleteDiaryEntryNoteNotFound(t *testing.T) {
|
||||
var app = setup()
|
||||
|
||||
_, err := app.DeleteDiaryEntryNote(-1)
|
||||
switch err_type := err.(type) {
|
||||
case *NotFoundError:
|
||||
// Do nothing
|
||||
break
|
||||
default:
|
||||
t.Fatalf("Expected NotFoundError, got %s", err_type)
|
||||
}
|
||||
|
||||
teardown(app)
|
||||
}
|
Loading…
Reference in new issue