2019-10-10 09:32:38 +07:00
|
|
|
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
|
|
|
|
}
|
2019-10-10 12:40:24 +07:00
|
|
|
defer rows.Close()
|
2019-10-10 09:32:38 +07:00
|
|
|
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
|
|
|
|
}
|