TerraOceanPlugin/GoBot/db/invite.go

48 lines
1.0 KiB
Go

2019-10-12 08:07:10 +07:00
package db
import "errors"
2019-10-12 08:07:10 +07:00
var ErrInviteUsed = errors.New("db: the requested invite has already been used")
2019-10-12 08:07:10 +07:00
func GetInviteOwner(inviteCode string) (string, error) {
rows, err := db.Query("SELECT owner, used FROM invite WHERE code=?", inviteCode)
if err != nil {
return "", err
}
2019-10-10 12:40:24 +07:00
defer rows.Close()
if rows.Next() {
var owner string
var used bool
err := rows.Scan(&owner, &used)
if err != nil {
return "", err
}
if used {
2019-10-12 08:07:10 +07:00
return "", ErrInviteUsed
}
return owner, nil
}
2019-10-12 08:07:10 +07:00
return "", ErrNotFound
}
2019-10-12 08:07:10 +07:00
func SetInviteOwner(inviteCode, owner string) error {
_, err := GetInviteOwner(inviteCode)
if err == nil {
2019-10-12 08:07:10 +07:00
return ErrAlreadyExists
}
2019-10-12 08:07:10 +07:00
if err != ErrNotFound {
return err
}
_, err = db.Exec("INSERT INTO invite(code, owner, used) VALUES(?,?,?)", inviteCode, owner, false)
return err
}
2019-10-12 08:07:10 +07:00
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
}