town/invites/invites.go

113 lines
1.7 KiB
Go
Raw Normal View History

2023-02-24 20:58:38 +00:00
package invites
import (
"database/sql"
2023-03-06 20:44:19 +00:00
"errors"
2023-02-26 23:02:51 +00:00
"time"
2023-10-24 06:22:21 +00:00
"git.tilde.town/tildetown/town/codes"
_ "github.com/mattn/go-sqlite3"
)
2023-02-26 23:02:51 +00:00
const (
2023-10-24 06:22:21 +00:00
dsn = "/town/var/invites/invites.db?mode=rw"
2023-02-26 23:02:51 +00:00
)
2023-02-28 05:50:55 +00:00
type Invite struct {
ID int64
Created time.Time
Code string
Email string
Used bool
}
2023-03-09 06:33:31 +00:00
func (i *Invite) Insert(db *sql.DB) error {
stmt, err := db.Prepare(`
INSERT INTO invites (code, email) VALUES (?, ?)
`)
if err != nil {
return err
}
2023-10-24 06:22:21 +00:00
i.Code = codes.NewCode(i.Email)
2023-03-09 06:39:20 +00:00
_, err = stmt.Exec(i.Code, i.Email)
2023-03-09 06:33:31 +00:00
if err != nil {
return err
}
defer stmt.Close()
return nil
}
func ConnectDB() (*sql.DB, error) {
db, err := sql.Open("sqlite3", dsn)
if err != nil {
return nil, err
}
return db, nil
}
2023-02-26 23:02:51 +00:00
2023-02-28 05:50:55 +00:00
func Get(db *sql.DB, code string) (*Invite, error) {
inv := &Invite{
Code: code,
}
2023-02-28 19:30:07 +00:00
var created string
2023-02-28 05:50:55 +00:00
var used int
stmt, err := db.Prepare(`
SELECT id, created, email, used
FROM invites WHERE code = ?`)
if err != nil {
return nil, err
}
row := stmt.QueryRow(code)
if err != nil {
return nil, err
}
defer stmt.Close()
err = row.Scan(
&inv.ID,
&created,
&inv.Email,
&used,
)
if err != nil {
return nil, err
}
2023-02-28 19:30:07 +00:00
inv.Created, err = time.Parse("2006-01-02T15:04", created)
if err != nil {
return inv, err
}
2023-02-28 05:50:55 +00:00
inv.Used = used > 0
return inv, nil
}
2023-03-06 20:44:19 +00:00
func (i *Invite) MarkUsed(db *sql.DB) (err error) {
var stmt *sql.Stmt
var result sql.Result
var rowsAffected int64
if stmt, err = db.Prepare(`UPDATE invites SET used = 1 WHERE id = ?`); err != nil {
return
}
2023-02-28 05:50:55 +00:00
2023-03-06 20:44:19 +00:00
if result, err = stmt.Exec(i.ID); err != nil {
return
}
if rowsAffected, err = result.RowsAffected(); err != nil {
return
}
if rowsAffected == 0 {
err = errors.New("no rows affected")
}
return
}