ictgj-voting/model_gamejam.go

90 lines
1.7 KiB
Go
Raw Normal View History

2017-10-02 13:44:55 +00:00
package main
2017-10-19 19:53:07 +00:00
import (
"errors"
"strings"
"time"
)
2017-10-02 13:44:55 +00:00
2017-10-11 23:03:27 +00:00
/**
* Gamejam
* Gamejam is the struct for any gamejam (current or archived)
*/
2017-10-02 13:44:55 +00:00
type Gamejam struct {
UUID string
Name string
Date time.Time
Teams []Team
Votes []Vote
2017-10-19 19:53:07 +00:00
m *model // The model that holds this gamejam's data
mPath []string // The path in the db to this gamejam
IsChanged bool // Flag to tell if we need to update the db
2017-10-02 13:44:55 +00:00
}
2017-10-11 23:03:27 +00:00
func NewGamejam(m *model) *Gamejam {
gj := new(Gamejam)
gj.m = m
2017-10-12 13:51:53 +00:00
gj.mPath = []string{"jam"}
2017-10-11 23:03:27 +00:00
return gj
2017-10-02 13:44:55 +00:00
}
2017-10-18 22:18:12 +00:00
/**
* DB Functions
* These are generally just called when the app starts up, or when the periodic 'save' runs
*/
2017-10-19 19:53:07 +00:00
func (m *model) LoadCurrentJam() (*Gamejam, error) {
2017-10-11 23:03:27 +00:00
if err := m.openDB(); err != nil {
2017-10-19 19:53:07 +00:00
return nil, err
2017-10-02 13:44:55 +00:00
}
2017-10-11 23:03:27 +00:00
defer m.closeDB()
2017-10-02 13:44:55 +00:00
2017-10-11 23:03:27 +00:00
gj := NewGamejam(m)
2017-10-12 13:51:53 +00:00
gj.Name, _ = m.bolt.GetValue(gj.mPath, "name")
2017-10-11 23:03:27 +00:00
// Load all teams
gj.Teams = gj.LoadAllTeams()
// Load all votes
gj.Votes = gj.LoadAllVotes()
2017-10-19 19:53:07 +00:00
return gj, nil
2017-10-02 13:44:55 +00:00
}
2017-10-18 22:18:12 +00:00
// Save everything to the DB whether it's flagged as changed or not
2017-10-19 19:53:07 +00:00
func (gj *Gamejam) SaveToDB() error {
2017-10-18 22:18:12 +00:00
if err := gj.m.openDB(); err != nil {
return err
}
defer gj.m.closeDB()
2017-10-19 19:53:07 +00:00
var errs []error
2017-10-21 22:47:01 +00:00
if err := gj.m.bolt.SetValue(gj.mPath, "name", gj.Name); err != nil {
errs = append(errs, err)
}
2017-10-19 19:53:07 +00:00
// Save all Teams
for _, tm := range gj.Teams {
if err := gj.SaveTeam(&tm); err != nil {
errs = append(errs, err)
2017-10-11 23:03:27 +00:00
}
2017-10-02 13:44:55 +00:00
}
2017-10-19 19:53:07 +00:00
// Save all Votes
for _, vt := range gj.Votes {
if err := gj.SaveVote(&vt); err != nil {
errs = append(errs, err)
2017-10-18 22:18:12 +00:00
}
2017-10-02 13:44:55 +00:00
}
2017-10-19 19:53:07 +00:00
if len(errs) > 0 {
var errTxt string
for i := range errs {
errTxt = errTxt + errs[i].Error() + "\n"
}
errTxt = strings.TrimSpace(errTxt)
return errors.New("Error(s) saving to DB: " + errTxt)
2017-10-02 13:44:55 +00:00
}
2017-10-19 19:53:07 +00:00
return nil
2017-10-02 13:44:55 +00:00
}