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/entry.go

152 lines
3.2 KiB

3 years ago
/// Defines objects that interact with the db
package model
import (
"database/sql"
"git.nagee.dev/isthisnagee/diary/db"
"log"
"strings"
3 years ago
)
type DiaryEntry struct {
Id int64 `json:"id"`
3 years ago
Title string `json:"title"`
CreatedAt int64 `json:"created_at"`
Version int64 `json:"version"`
3 years ago
}
type App struct {
*db.DbCtx
}
func NewApp(db_path string) (*App, error) {
app, err := db.Init(db_path)
if err != nil {
return nil, err
}
return &App{app}, nil
}
3 years ago
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
}
func (app *App) EditDiaryEntry(id int64, title string) (*DiaryEntryNote, error) {
clean_title := strings.TrimSpace(title)
result, err := app.Db.Exec("UPDATE diary_log set title=? where id=?", clean_title, id)
if err != nil {
return nil, err
}
num_rows_affected, err := result.RowsAffected()
if err != nil {
return nil, err
}
if num_rows_affected == 0 {
return nil, &NotFoundError{given_id: id}
}
return app.GetDiaryEntryNote(id)
}
func (app *App) GetDiaryEntry(id int64) (*DiaryEntry, error) {
var diary_entry DiaryEntry
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 {
if err == sql.ErrNoRows {
return nil, &NotFoundError{given_id: id}
}
return nil, err
}
return &diary_entry, nil
}
func (app *App) DeleteDiaryEntry(id int64) (bool, error) {
result, err := app.Db.Exec("DELETE FROM diary_log where id=?", 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: id}
}
return true, nil
}
type GetDiaryEntriesQuery struct {
/// Exclusive
CreatedBeforeTs *int64
/// Inclusive
CreatedAfterTs *int64
}
func (app *App) GetDiaryEntries(q GetDiaryEntriesQuery) []*DiaryEntry {
var query = "SELECT id, title, created_at, version FROM diary_log"
var whereParams []interface{}
if q.CreatedBeforeTs != nil {
query += " where created_at < ?"
whereParams = append(whereParams, *q.CreatedBeforeTs)
}
if q.CreatedAfterTs != nil {
query += " and created_at >= ?"
whereParams = append(whereParams, *q.CreatedAfterTs)
}
query += " ORDER BY created_at desc, id desc"
rows, err := app.Db.Query(
query,
whereParams...,
)
if err != nil {
panic(err)
}
defer rows.Close()
var result []*DiaryEntry
for rows.Next() {
var id, created_at, version int64
var title string
err := rows.Scan(&id, &title, &created_at, &version)
if err != nil {
panic(err)
}
result = append(result, &DiaryEntry{
Id: id, Title: title, CreatedAt: created_at, Version: version,
})
}
if err := rows.Err(); err != nil {
panic(err)
}
return result
}