TerraOceanPlugin/GoBot/db_invite.go

47 lines
1.0 KiB
Go

package main
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
}
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
}