45 lines
823 B
Go
45 lines
823 B
Go
|
package database
|
||
|
|
||
|
import (
|
||
|
"database/sql"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type Update struct {
|
||
|
Name string
|
||
|
Time time.Time
|
||
|
}
|
||
|
|
||
|
type ListUpdatesQuery struct {
|
||
|
Limit int
|
||
|
}
|
||
|
|
||
|
func ListUpdates(dbConn *sql.DB, query ListUpdatesQuery) ([]Update, error) {
|
||
|
rows, err := dbConn.Query(`SELECT name, time FROM updates ORDER BY time DESC LIMIT ?;`, query.Limit)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer rows.Close()
|
||
|
|
||
|
updates := []Update{}
|
||
|
for rows.Next() {
|
||
|
var update Update
|
||
|
err := rows.Scan(&update.Name, &update.Time)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
updates = append(updates, update)
|
||
|
}
|
||
|
|
||
|
return updates, nil
|
||
|
}
|
||
|
|
||
|
func SaveUpdate(db *sql.DB, update *Update) (*Update, error) {
|
||
|
_, err := db.Exec("INSERT INTO updates (time, name) VALUES (?, ?)", update.Time, update.Name)
|
||
|
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return update, nil
|
||
|
}
|