forked from tildetown/town
117 lines
1.8 KiB
Go
117 lines
1.8 KiB
Go
package signup
|
|
|
|
import (
|
|
"database/sql"
|
|
"time"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
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) {
|
|
db, err := sql.Open("sqlite3", "/town/var/signups/signups.db?mode=rw")
|
|
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
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
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
|
|
}
|
|
defer stmt.Close()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (d *DB) Close() error {
|
|
return d.db.Close()
|
|
}
|