forked from chanbakjsd/TerraOceanPlugin
go: Separate invitation database code into a separate file
parent
6b3761d98e
commit
7fed3d8dd3
@ -0,0 +1,46 @@
|
|||||||
|
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
|
||||||
|
}
|
Loading…
Reference in New Issue