138 lines
3.3 KiB
Go
138 lines
3.3 KiB
Go
|
package database
|
||
|
|
||
|
import (
|
||
|
"database/sql"
|
||
|
_ "github.com/mattn/go-sqlite3"
|
||
|
"log"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type KennelCat struct {
|
||
|
ID string `json:"id"`
|
||
|
UserID string `json:"user_id"`
|
||
|
Link string `json:"link"`
|
||
|
Description string `json:"description"`
|
||
|
Spritesheet string `json:"spritesheet"`
|
||
|
CreatedAt time.Time `json:"created_at"`
|
||
|
}
|
||
|
|
||
|
type KennelState struct {
|
||
|
At time.Time `json:"at"`
|
||
|
EncodedState string `json:"state"`
|
||
|
}
|
||
|
|
||
|
func CountUserKennelCats(db *sql.DB, userID string) (int, error) {
|
||
|
log.Println("counting kennel cats for user", userID)
|
||
|
|
||
|
row := db.QueryRow("SELECT COUNT(*) FROM kennel_cat WHERE user_id = ?", userID)
|
||
|
var count int
|
||
|
err := row.Scan(&count)
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
return count, nil
|
||
|
}
|
||
|
|
||
|
func GetUserKennelCats(db *sql.DB, userID string) ([]KennelCat, error) {
|
||
|
log.Println("getting kennel cats for user", userID)
|
||
|
|
||
|
rows, err := db.Query("SELECT * FROM kennel_cat WHERE user_id = ?", userID)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer rows.Close()
|
||
|
|
||
|
var cats []KennelCat
|
||
|
for rows.Next() {
|
||
|
var cat KennelCat
|
||
|
err := rows.Scan(&cat.ID, &cat.UserID, &cat.Link, &cat.Description, &cat.Spritesheet, &cat.CreatedAt)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
cats = append(cats, cat)
|
||
|
}
|
||
|
|
||
|
return cats, nil
|
||
|
}
|
||
|
|
||
|
func SaveKennelCat(db *sql.DB, cat *KennelCat) (*KennelCat, error) {
|
||
|
log.Println("saving kennel cat", cat.ID)
|
||
|
|
||
|
if (cat.CreatedAt == time.Time{}) {
|
||
|
cat.CreatedAt = time.Now()
|
||
|
}
|
||
|
|
||
|
_, err := db.Exec("INSERT OR REPLACE INTO kennel_cat (id, user_id, link, description, spritesheet, created_at) VALUES (?, ?, ?, ?, ?, ?)", cat.ID, cat.UserID, cat.Link, cat.Description, cat.Spritesheet, cat.CreatedAt)
|
||
|
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return cat, nil
|
||
|
}
|
||
|
|
||
|
func GetKennelCat(db *sql.DB, catID string) (*KennelCat, error) {
|
||
|
log.Println("getting kennel cat", catID)
|
||
|
|
||
|
row := db.QueryRow("SELECT * FROM kennel_cat WHERE id = ?", catID)
|
||
|
var cat KennelCat
|
||
|
err := row.Scan(&cat.ID, &cat.UserID, &cat.Link, &cat.Description, &cat.Spritesheet, &cat.CreatedAt)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return &cat, nil
|
||
|
}
|
||
|
|
||
|
func DeleteKennelCat(db *sql.DB, catID string) error {
|
||
|
log.Println("deleting kennel cat", catID)
|
||
|
|
||
|
_, err := db.Exec("DELETE FROM kennel_cat WHERE id = ?", catID)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func GetKennel(dbConn *sql.DB) ([]KennelCat, error) {
|
||
|
log.Println("getting kennel")
|
||
|
|
||
|
rows, err := dbConn.Query("SELECT * FROM kennel_cat")
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer rows.Close()
|
||
|
|
||
|
var cats []KennelCat
|
||
|
for rows.Next() {
|
||
|
var cat KennelCat
|
||
|
err := rows.Scan(&cat.ID, &cat.UserID, &cat.Link, &cat.Description, &cat.Spritesheet, &cat.CreatedAt)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
cats = append(cats, cat)
|
||
|
}
|
||
|
|
||
|
return cats, nil
|
||
|
}
|
||
|
|
||
|
func GetKennelState(dbConn *sql.DB) (*KennelState, error) {
|
||
|
log.Println("getting kennel state")
|
||
|
|
||
|
row := dbConn.QueryRow("SELECT * FROM kennel_state")
|
||
|
var state KennelState
|
||
|
err := row.Scan(&state.At, &state.EncodedState)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return &state, nil
|
||
|
}
|
||
|
|
||
|
func SaveKennelState(dbConn *sql.DB, state *KennelState) (*KennelState, error) {
|
||
|
log.Println("saving kennel state")
|
||
|
|
||
|
_, err := dbConn.Exec("INSERT OR REPLACE INTO kennel_state (at, state) VALUES (?, ?)", state.At, state.EncodedState)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return state, nil
|
||
|
}
|