town/signup/signup.go

117 lines
1.8 KiB
Go
Raw Normal View History

2023-02-20 08:43:14 +00:00
package signup
import (
"database/sql"
"time"
2023-02-21 03:56:19 +00:00
_ "github.com/mattn/go-sqlite3"
2023-02-20 08:43:14 +00:00
)
type AdminNote struct {
ID int64
Admin string
Note string
When time.Time
}
type SignupDecision string
type UserState string
const (
SignupAccepted = "accepted"
SignupRejected = "rejected"
StateActive = "active"
StateTempBan = "temp_banned"
StateBan = "banned"
)
type TownSignup struct {
ID int64
Created time.Time
Email string
How string
Why string
Links string
Extra string
Notes []AdminNote
Decision SignupDecision
}
type TownAccount struct {
ID int
Emails []string
Username string
Signup int
Notes []AdminNote
State UserState
Admin bool
}
type DB struct {
db *sql.DB
}
func NewDB() (*DB, error) {
2023-02-21 03:56:19 +00:00
db, err := sql.Open("sqlite3", "/town/var/signups/signups.db?mode=rw")
2023-02-20 08:43:14 +00:00
if err != nil {
return nil, err
}
return &DB{
db: db,
}, nil
}
func (d *DB) InsertSignup(su *TownSignup) error {
stmt, err := d.db.Prepare(`
INSERT INTO signups (created, email, how, why, links, extra) VALUES(
?, ?, ?, ?, ?, ?
) RETURNING id
`)
if err != nil {
return err
}
result, err := stmt.Exec(su.Created.Unix(), su.Email, su.How, su.Why, su.Links, su.Extra)
if err != nil {
return err
}
2023-02-21 03:56:19 +00:00
defer stmt.Close()
2023-02-20 08:43:14 +00:00
liid, err := result.LastInsertId()
if err != nil {
return err
}
su.ID = liid
return nil
}
func (d *DB) UpdateSignup(su *TownSignup) error {
if su.ID < 0 {
return nil
}
stmt, err := d.db.Prepare(`
UPDATE signups (email, how, why, links, extra) VALUES(
?, ?, ?, ?, ?
)
`)
if err != nil {
return err
}
_, err = stmt.Exec(su.Email, su.How, su.Why, su.Links, su.Extra)
if err != nil {
return err
}
2023-02-21 03:56:19 +00:00
defer stmt.Close()
2023-02-20 08:43:14 +00:00
return nil
}
2023-02-21 03:56:19 +00:00
func (d *DB) Close() error {
return d.db.Close()
}