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.
diary/model/model.go

42 lines
824 B

/// Defines objects that interact with the db
package model
import (
"isthisnagee.com/tools/diary/db"
)
type DiaryEntry struct {
Id int `json:"id"`
Title string `json:"title"`
CreatedAt int `json:"created_at"`
Version int `json:"version"`
}
type App struct {
*db.DbCtx
}
func (app *App) NewDiaryEntry(title string) *DiaryEntry {
var diary_entry DiaryEntry
var result, err = app.Db.Exec("INSERT INTO diary_log (title) values (?);", title)
if err != nil {
panic(err)
}
id, err := result.LastInsertId()
if err != nil {
panic(err)
}
if err := app.Db.QueryRow(
"SELECT id, title, created_at, version FROM diary_log where id=?",
id,
).Scan(&diary_entry.Id, &diary_entry.Title, &diary_entry.CreatedAt, &diary_entry.Version); err != nil {
panic(err)
}
return &diary_entry
}