town/models/models.go

125 lines
2.1 KiB
Go

// shared database related code
package models
import (
"database/sql"
"time"
_ "github.com/mattn/go-sqlite3"
)
// TODO this is in flux; might want SignupNote and UserNote structs separately
type SignupNote struct {
ID int64
Created time.Time
Author string
Content string
}
func (n *SignupNote) Insert(db *sql.DB) error {
// TODO
return nil
}
type SignupDecision string
const (
SignupAccepted SignupDecision = "accepted"
SignupRejected SignupDecision = "rejected"
)
// TODO add DecisionTime column to DB
// TODO add DecisionBy column to DB
// TODO add CleanEmail column to DB
type TownSignup struct {
ID int64
Created time.Time
Email string
How string
Why string
Links string
Notes []SignupNote
Decision SignupDecision
DecisionTime time.Time
DecidedBy string
CleanEmail string
}
func (s *TownSignup) Insert(db *sql.DB) error {
stmt, err := db.Prepare(`
INSERT INTO signups (created, email, how, why, links) VALUES(
?, ?, ?, ?, ?
) RETURNING id
`)
if err != nil {
return err
}
result, err := stmt.Exec(s.Created.Unix(), s.Email, s.How, s.Why, s.Links)
if err != nil {
return err
}
defer stmt.Close()
liid, err := result.LastInsertId()
if err != nil {
return err
}
s.ID = liid
return nil
}
func (s *TownSignup) All(db *sql.DB) ([]*TownSignup, error) {
// TODO notes; circle back once can author them
rows, err := db.Query(`
SELECT
id, created, email, how, why, links
FROM signups WHERE decision = ""`)
if err != nil {
return nil, err
}
defer rows.Close()
out := []*TownSignup{}
for rows.Next() {
su := &TownSignup{}
if err = rows.Scan(
&su.ID,
&su.Created,
&su.Email,
&su.How,
&su.Why,
&su.Links,
); err != nil {
return nil, err
}
// TODO fetch notes
out = append(out, su)
}
return out, nil
}
// below is all TODO and unused rn
type UserState string
const (
StateActive = "active"
StateTempBan = "temp_banned"
StateBan = "banned"
)
type TownAccount struct {
ID int64
Emails []string
Username string
Signup int
Notes []AdminNote
State UserState
Admin bool
}