town/signup/signup.go

94 lines
1.4 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
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(`
2023-02-21 06:13:51 +00:00
INSERT INTO signups (created, email, how, why, links) VALUES(
2023-02-21 06:00:10 +00:00
?, ?, ?, ?, ?
2023-02-20 08:43:14 +00:00
) RETURNING id
`)
if err != nil {
return err
}
2023-02-21 06:00:10 +00:00
result, err := stmt.Exec(su.Created.Unix(), su.Email, su.How, su.Why, su.Links)
2023-02-20 08:43:14 +00:00
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
}
2023-02-21 03:56:19 +00:00
func (d *DB) Close() error {
return d.db.Close()
}