ictgj-voting/model.go

127 lines
2.6 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"
"github.com/br0xen/boltease"
)
2017-04-03 21:32:13 +00:00
2017-10-11 23:03:27 +00:00
// model stores the current jam in memory, and has the ability to access archived dbs
type model struct {
bolt *boltease.DB
dbOpened int
dbFileName string
2017-10-10 12:39:42 +00:00
2017-10-11 23:03:27 +00:00
site *siteData // Configuration data for the site
jam *Gamejam // The currently active gamejam
clients []Client // Web clients that have connected to the server
2017-10-10 12:39:42 +00:00
}
2017-10-11 23:03:27 +00:00
// Authentication Modes: Flags for which clients are able to vote
const (
AuthModeAuthentication = iota
AuthModeAll
AuthModeError
)
2017-10-11 23:03:27 +00:00
// Update Flags: Which parts of the model need to be updated
const (
UpdateSiteData = iota
UpdateJamData
)
func NewModel() (*model, error) {
var err error
m := new(model)
m.dbFileName = DbName
if err = m.openDB(); err != nil {
return nil, errors.New("Unable to open DB: " + err.Error())
}
defer m.closeDB()
// Initialize the DB
if err = m.initDB(); err != nil {
return nil, errors.New("Unable to initialize DB: " + err.Error())
}
// Load the site data
m.site = m.LoadSiteData()
// Load the jam data
m.jam = m.LoadCurrentJam()
2017-10-10 12:39:42 +00:00
2017-10-11 23:03:27 +00:00
// Load web clients
m.clients = m.LoadAllClients()
return &m, nil
2017-10-10 12:39:42 +00:00
}
2017-10-11 23:03:27 +00:00
func (m *model) openDB() error {
m.dbOpened += 1
if db.dbOpened == 1 {
2017-04-03 21:32:13 +00:00
var err error
2017-10-11 23:03:27 +00:00
m.bolt, err = boltease.Create(m.dbFileName, 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
}
2017-10-11 23:03:27 +00:00
func (m *model) closeDB() error {
m.dbOpened -= 1
if m.dbOpened == 0 {
return m.bolt.CloseDB()
2017-04-03 21:32:13 +00:00
}
return nil
}
2017-10-11 23:03:27 +00:00
func (m *model) initDB() error {
2017-06-12 23:23:46 +00:00
var err error
2017-10-11 23:03:27 +00:00
if err = m.openDB(); err != nil {
2017-06-12 23:23:46 +00:00
return err
}
2017-10-11 23:03:27 +00:00
defer m.closeDB()
2017-06-12 23:23:46 +00:00
2017-04-21 18:17:18 +00:00
// Create the path to the bucket to store admin users
2017-10-11 23:03:27 +00:00
if err = m.bolt.MkBucketPath([]string{"users"}); err != nil {
2017-04-21 18:17:18 +00:00
return err
2017-04-03 21:32:13 +00:00
}
2017-10-11 23:03:27 +00:00
// Create the path to the bucket to store the web clients
if err = m.bolt.MkBucketPath([]string{"clients"}); err != nil {
2017-04-21 18:17:18 +00:00
return err
2017-04-03 21:32:13 +00:00
}
2017-10-11 23:03:27 +00:00
// Create the path to the bucket to store the current jam & teams
if err = m.bolt.MkBucketPath([]string{"jam", "teams"}); err != nil {
return err
2017-10-10 12:39:42 +00:00
}
2017-10-11 23:03:27 +00:00
// Create the path to the bucket to store the list of archived jams
if err = m.bolt.MkBucketPath([]string{"archive"}); err != nil {
2017-10-10 12:39:42 +00:00
return err
2017-04-03 21:32:13 +00:00
}
2017-10-11 23:03:27 +00:00
// Create the path to the bucket to store site config data
return m.bolt.MkBucketPath([]string{"site"})
2017-04-03 21:32:13 +00:00
}
2017-10-11 23:03:27 +00:00
// saveChanges saves any parts of the model that have been flagged as changed to the database
func (m *model) saveChanges() error {
2017-04-03 21:32:13 +00:00
var err error
2017-10-11 23:03:27 +00:00
if err = m.openDB(); err != nil {
return err
}
2017-10-11 23:03:27 +00:00
defer m.closeDB()
2017-10-11 23:03:27 +00:00
if m.site.needsSave() {
if err = m.site.saveToDB(); err != nil {
return err
}
}
2017-10-11 23:03:27 +00:00
if m.jam.needsSave() {
if err = m.jam.saveToDB(); err != nil {
return err
}
}
2017-10-11 23:03:27 +00:00
return nil
}