TerraOceanPlugin/GoBot/db/invite.go

48 lines
1.0 KiB
Go

package db
import "errors"
var ErrInviteUsed = errors.New("db: the requested invite has already been used")
func GetInviteOwner(inviteCode string) (string, error) {
rows, err := db.Query("SELECT owner, used FROM invite WHERE code=?", inviteCode)
if err != nil {
return "", err
}
defer rows.Close()
if rows.Next() {
var owner string
var used bool
err := rows.Scan(&owner, &used)
if err != nil {
return "", err
}
if used {
return "", ErrInviteUsed
}
return owner, nil
}
return "", ErrNotFound
}
func SetInviteOwner(inviteCode, owner string) error {
_, err := GetInviteOwner(inviteCode)
if err == nil {
return ErrAlreadyExists
}
if err != ErrNotFound {
return err
}
_, err = db.Exec("INSERT INTO invite(code, owner, used) VALUES(?,?,?)", inviteCode, owner, false)
return err
}
func UseInvite(inviteCode string) error {
owner, err := GetInviteOwner(inviteCode)
if err != nil {
return err
}
_, err = db.Exec("REPLACE INTO invite(code, owner, used) VALUES(?,?,?)", inviteCode, owner, true)
return err
}