2019-10-17 09:02:35 +07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-10-18 03:38:32 +07:00
|
|
|
func GetTotalActiveLevel() (int, error) {
|
|
|
|
rows, err := db.Query("SELECT sum(level) FROM activeLevel")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
if rows.Next() {
|
|
|
|
var totalActiveLevel int
|
|
|
|
err := rows.Scan(&totalActiveLevel)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return totalActiveLevel, nil
|
|
|
|
}
|
|
|
|
return 0, ErrNotFound
|
|
|
|
}
|
|
|
|
|
2019-10-17 09:02:35 +07:00
|
|
|
func AddActiveLevel(discordID string, addedLevel int) error {
|
2019-10-18 04:29:10 +07:00
|
|
|
_, err := db.Exec("INSERT INTO activeLevel(id, level) VALUES(?, ?) ON CONFLICT(id) DO UPDATE SET level=level+?", discordID, addedLevel, addedLevel)
|
2019-10-17 09:02:35 +07:00
|
|
|
return err
|
|
|
|
}
|