You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.4 KiB
Go
88 lines
2.4 KiB
Go
package dbi
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
)
|
|
|
|
type Appearance struct {
|
|
UID string `json:"uid"`
|
|
Title string `json:"title"`
|
|
Description string `json:"description"`
|
|
Hyperlink string `json:"hyperlink"`
|
|
Status string `json:"status"`
|
|
Created string `json:"created"`
|
|
Modified string `json:"modified"`
|
|
Username string `json:"username"`
|
|
}
|
|
|
|
func CreateAppearance(conn *sql.Conn, a *Appearance) error {
|
|
if a.UID == "" {
|
|
a.UID = GenUUID()
|
|
}
|
|
now := GetNow()
|
|
a.Created = now
|
|
a.Modified = now
|
|
_, err := conn.ExecContext(context.Background(),
|
|
`INSERT INTO appearances (UID, Title, Description, Hyperlink, Status, Created, Modified, Username)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
a.UID, a.Title, a.Description, a.Hyperlink, a.Status, a.Created, a.Modified, a.Username,
|
|
)
|
|
return err
|
|
}
|
|
|
|
func GetAppearance(conn *sql.Conn, uid string) (Appearance, error) {
|
|
row := conn.QueryRowContext(context.Background(),
|
|
`SELECT UID, Title, Description, Hyperlink, Status, Created, Modified, Username
|
|
FROM appearances WHERE UID = ?`,
|
|
uid,
|
|
)
|
|
var a Appearance
|
|
err := row.Scan(&a.UID, &a.Title, &a.Description, &a.Hyperlink, &a.Status, &a.Created, &a.Modified, &a.Username)
|
|
if err == sql.ErrNoRows {
|
|
return Appearance{}, fmt.Errorf("appearance %q not found", uid)
|
|
}
|
|
return a, err
|
|
}
|
|
|
|
func UpdateAppearance(conn *sql.Conn, a *Appearance) error {
|
|
a.Modified = GetNow()
|
|
_, err := conn.ExecContext(context.Background(),
|
|
`UPDATE appearances SET Title = ?, Description = ?, Hyperlink = ?, Status = ?, Modified = ?, Username = ?
|
|
WHERE UID = ?`,
|
|
a.Title, a.Description, a.Hyperlink, a.Status, a.Modified, a.Username, a.UID,
|
|
)
|
|
return err
|
|
}
|
|
|
|
func DeleteAppearance(conn *sql.Conn, uid string) error {
|
|
_, err := conn.ExecContext(context.Background(),
|
|
`DELETE FROM appearances WHERE UID = ?`,
|
|
uid,
|
|
)
|
|
return err
|
|
}
|
|
|
|
func GetAppearancesForUsername(conn *sql.Conn, username string) ([]Appearance, error) {
|
|
rows, err := conn.QueryContext(context.Background(),
|
|
`SELECT UID, Title, Description, Hyperlink, Status, Created, Modified, Username
|
|
FROM appearances WHERE Username = ?`,
|
|
username,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var results []Appearance
|
|
for rows.Next() {
|
|
var a Appearance
|
|
if err := rows.Scan(&a.UID, &a.Title, &a.Description, &a.Hyperlink, &a.Status, &a.Created, &a.Modified, &a.Username); err != nil {
|
|
return nil, err
|
|
}
|
|
results = append(results, a)
|
|
}
|
|
return results, rows.Err()
|
|
}
|