mark invites as used

trunk
vilmibm 2023-03-06 20:44:19 +00:00
parent 57115b1c11
commit d407e26917
2 changed files with 26 additions and 4 deletions

View File

@ -135,6 +135,10 @@ if you end up very stuck, you can email root@tilde.town .`, data.Username))
fmt.Println(s)
if err = invite.MarkUsed(inviteDB); err != nil {
return fmt.Errorf("could not mark invite as used: %w", err)
}
// TODO burn invite
return nil

View File

@ -3,6 +3,7 @@ package invites
import (
"database/sql"
"encoding/base64"
"errors"
"math/rand"
"strings"
"time"
@ -121,8 +122,25 @@ func Get(db *sql.DB, code string) (*Invite, error) {
return inv, nil
}
func IsCodeValid(db *sql.DB, code string) (bool, error) {
return false, nil
}
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
}
// TODO decide on rest of API (eg Validate, Use, Get, etc)
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
}