go: db: Implement active level

master
Luther Wen Xu 2019-10-17 17:02:35 +07:00
parent e97bc82606
commit 1963c773d6
Signed by: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
2 changed files with 27 additions and 0 deletions

@ -0,0 +1,23 @@
package db
func GetActiveLevel(discordID string) (int, error) {
rows, err := db.Query("SELECT level FROM activeLevel WHERE id=?", discordID)
if err != nil {
return 0, err
}
defer rows.Close()
if rows.Next() {
var activeLevel int
err := rows.Scan(&activeLevel)
if err != nil {
return 0, err
}
return activeLevel, nil
}
return 0, ErrNotFound
}
func AddActiveLevel(discordID string, addedLevel int) error {
_, err := db.Exec("INSERT INTO activeLevel(id, level) VALUES(?, ?) ON DUPLICATE KEY UPDATE level=level+?", discordID, addedLevel, addedLevel)
return err
}

@ -39,4 +39,8 @@ func init() {
if err != nil {
panic(err)
}
_, err = db.Exec("CREATE TABLE IF NOT EXISTS activeLevel (id TEXT, level INTEGER, UNIQUE(id))")
if err != nil {
panic(err)
}
}