From d407e26917bbf3072b102e6c755846de7200de10 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Mon, 6 Mar 2023 20:44:19 +0000 Subject: [PATCH] mark invites as used --- cmd/welcome/main.go | 4 ++++ invites/invites.go | 26 ++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/cmd/welcome/main.go b/cmd/welcome/main.go index 3a0ef22..5818ada 100644 --- a/cmd/welcome/main.go +++ b/cmd/welcome/main.go @@ -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 diff --git a/invites/invites.go b/invites/invites.go index f665ab7..2cd9f6d 100644 --- a/invites/invites.go +++ b/invites/invites.go @@ -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 +}