ictgj-voting/model.go

161 lines
3.2 KiB
Go
Raw Normal View History

2017-04-03 21:32:13 +00:00
package main
2017-06-08 17:20:43 +00:00
import (
"errors"
"strings"
"github.com/br0xen/boltease"
)
2017-04-03 21:32:13 +00:00
var db *boltease.DB
2017-06-12 23:23:46 +00:00
var dbOpened int
2017-04-03 21:32:13 +00:00
const (
SiteModeWaiting = iota
SiteModeVoting
SiteModeError
)
func GetDefaultSiteConfig() *siteData {
ret := new(siteData)
ret.Title = "ICT GameJam"
ret.Port = 8080
ret.SessionName = "ict-gamejam"
ret.ServerDir = "./"
return ret
}
2017-04-03 21:32:13 +00:00
func openDatabase() error {
2017-06-12 23:23:46 +00:00
dbOpened += 1
if dbOpened == 1 {
2017-04-03 21:32:13 +00:00
var err error
db, err = boltease.Create(DbName, 0600, nil)
2017-04-03 21:32:13 +00:00
if err != nil {
return err
}
2017-06-12 23:23:46 +00:00
}
return nil
}
func closeDatabase() error {
dbOpened -= 1
if dbOpened == 0 {
return db.CloseDB()
2017-04-03 21:32:13 +00:00
}
return nil
}
func initDatabase() error {
2017-06-12 23:23:46 +00:00
var err error
if err = openDatabase(); err != nil {
return err
}
defer closeDatabase()
2017-04-21 18:17:18 +00:00
// Create the path to the bucket to store admin users
if err := db.MkBucketPath([]string{"users"}); err != nil {
return err
2017-04-03 21:32:13 +00:00
}
2017-04-21 18:17:18 +00:00
// Create the path to the bucket to store jam informations
2017-06-08 17:20:43 +00:00
if err := db.MkBucketPath([]string{"jam"}); err != nil {
2017-04-21 18:17:18 +00:00
return err
2017-04-03 21:32:13 +00:00
}
2017-04-21 18:17:18 +00:00
// Create the path to the bucket to store site config data
return db.MkBucketPath([]string{"site"})
2017-04-03 21:32:13 +00:00
}
2017-04-21 18:17:18 +00:00
func dbSetCurrentJam(name string) error {
2017-06-12 23:23:46 +00:00
var err error
if err = openDatabase(); err != nil {
2017-04-21 18:17:18 +00:00
return err
2017-04-03 21:32:13 +00:00
}
2017-06-12 23:23:46 +00:00
defer closeDatabase()
2017-04-03 21:32:13 +00:00
2017-04-21 18:17:18 +00:00
return db.SetValue([]string{"site"}, "current-jam", name)
2017-04-03 21:32:13 +00:00
}
2017-04-21 18:17:18 +00:00
func dbHasCurrentJam() bool {
2017-04-03 21:32:13 +00:00
var err error
2017-06-08 17:20:43 +00:00
if _, err = dbGetCurrentJam(); err != nil {
2017-04-21 18:17:18 +00:00
return false
2017-04-03 21:32:13 +00:00
}
2017-06-08 17:20:43 +00:00
return true
2017-04-03 21:32:13 +00:00
}
2017-04-21 18:17:18 +00:00
func dbGetCurrentJam() (string, error) {
2017-06-08 17:20:43 +00:00
var ret string
2017-04-03 21:32:13 +00:00
var err error
2017-06-12 23:23:46 +00:00
if err = openDatabase(); err != nil {
2017-06-08 17:20:43 +00:00
return "", err
2017-04-03 21:32:13 +00:00
}
2017-06-12 23:23:46 +00:00
defer closeDatabase()
2017-04-03 21:32:13 +00:00
2017-06-08 17:20:43 +00:00
ret, err = db.GetValue([]string{"site"}, "current-jam")
if err == nil && strings.TrimSpace(ret) == "" {
return ret, errors.New("No Jam Name Specified")
2017-04-21 18:17:18 +00:00
}
2017-06-08 17:20:43 +00:00
return ret, err
2017-04-03 21:32:13 +00:00
}
func dbGetSiteConfig() *siteData {
var ret *siteData
def := GetDefaultSiteConfig()
var err error
if err = openDatabase(); err != nil {
return def
}
defer closeDatabase()
ret = new(siteData)
siteConf := []string{"site"}
if ret.Title, err = db.GetValue(siteConf, "title"); err != nil {
ret.Title = def.Title
}
if ret.Port, err = db.GetInt(siteConf, "port"); err != nil {
ret.Port = def.Port
}
if ret.SessionName, err = db.GetValue(siteConf, "session-name"); err != nil {
ret.SessionName = def.SessionName
}
if ret.ServerDir, err = db.GetValue(siteConf, "server-dir"); err != nil {
ret.ServerDir = def.ServerDir
}
return ret
}
func dbSaveSiteConfig(dat *siteData) error {
var err error
if err = openDatabase(); err != nil {
return err
}
defer closeDatabase()
siteConf := []string{"site"}
if err = db.SetValue(siteConf, "title", dat.Title); err != nil {
return err
}
if err = db.SetInt(siteConf, "port", dat.Port); err != nil {
return err
}
if err = db.SetValue(siteConf, "session-name", dat.SessionName); err != nil {
return err
}
return db.SetValue(siteConf, "server-dir", dat.ServerDir)
}
func dbGetPublicSiteMode() int {
if ret, err := db.GetInt([]string{"site"}, "public-mode"); err != nil {
return SiteModeWaiting
} else {
return ret
}
}
func dbSetPublicSiteMode(mode int) error {
if mode < 0 || mode >= SiteModeError {
return errors.New("Invalid site mode")
}
return db.SetInt([]string{"site"}, "public-mode", mode)
}