town/towndb/towndb.go

77 lines
1.1 KiB
Go
Raw Normal View History

2023-03-04 22:19:27 +00:00
package towndb
import (
"database/sql"
"time"
_ "github.com/mattn/go-sqlite3"
)
const dsn = "/town/var/town.db?mode=rw"
type UserState string
const (
StateActive = "active"
StateTempBan = "temp_banned"
StateBan = "banned"
StateDeleted = "deleted" // some users request deletion
)
type AdminNote struct {
ID int64
Created time.Time
AuthorID int64
Content string
UserID int64
}
func (n *AdminNote) Insert(db *sql.DB) error {
n.Created = time.Now()
stmt, err := db.Prepare(`
INSERT INTO notes (created, authorid, content, userid)
VALUES (
?, ?, ?, ?
)`)
if err != nil {
return err
}
result, err := stmt.Exec(
n.Created.Unix(),
n.AuthorID,
n.Content,
n.UserID)
if err != nil {
return err
}
defer stmt.Close()
liid, err := result.LastInsertId()
if err != nil {
return err
}
n.ID = liid
return nil
}
type TownUser struct {
ID int64
Emails []string
Username string
Notes []AdminNote
State UserState
IsAdmin bool
}
func ConnectDB() (*sql.DB, error) {
db, err := sql.Open("sqlite3", dsn)
if err != nil {
return nil, err
}
return db, nil
}