ictgj-voting/model_clients.go

107 lines
2.0 KiB
Go
Raw Normal View History

2017-06-08 17:20:43 +00:00
package main
import (
"strconv"
"strings"
"time"
)
2017-10-11 23:03:27 +00:00
/**
* Client
* A client is a system that is connecting to the web server
*/
2017-06-08 17:20:43 +00:00
type Client struct {
UUID string
Auth bool
2017-07-06 17:09:49 +00:00
Name string
IP string
2017-10-12 13:51:53 +00:00
mPath []string // The path in the DB to this client
}
func NewClient(id string) *Client {
return &Client{
UUID: id,
mPath: []string{"clients", id},
}
2017-06-08 17:20:43 +00:00
}
2017-10-11 23:03:27 +00:00
// Load all clients
func (m *model) LoadAllClients() []Client {
2017-06-08 17:20:43 +00:00
var err error
2017-10-11 23:03:27 +00:00
if err = m.openDB(); err != nil {
return err
2017-06-08 17:20:43 +00:00
}
2017-10-11 23:03:27 +00:00
defer m.closeDB()
2017-06-08 17:20:43 +00:00
var clientUids []string
2017-10-12 13:51:53 +00:00
cliPath := []string{"clients"}
if clientUids, err = m.bolt.GetBucketList(cliPath); err != nil {
2017-10-11 23:03:27 +00:00
return err
2017-06-08 17:20:43 +00:00
}
for _, v := range clientUids {
2017-10-11 23:03:27 +00:00
if cl := m.LoadClient(v); cl != nil {
m.clients = append(m.clients, *cl)
2017-06-08 17:20:43 +00:00
}
}
}
2017-10-11 23:03:27 +00:00
// Load a client from the DB and return it
func (m *model) LoadClient(clId string) *Client {
2017-06-08 17:20:43 +00:00
var err error
2017-10-11 23:03:27 +00:00
if err = m.openDB(); err != nil {
2017-06-08 17:20:43 +00:00
return nil
}
2017-10-11 23:03:27 +00:00
defer m.closeDB()
2017-06-08 17:20:43 +00:00
2017-10-12 13:51:53 +00:00
cl := NewClient(clId)
cl.Auth, _ = m.bolt.GetBool(cl.mPath, "auth")
cl.Name, _ = m.bolt.GetValue(cl.mPath, "name")
cl.IP, _ = m.bolt.GetValue(cl.mPath, "ip")
2017-06-08 17:20:43 +00:00
return cl
}
2017-10-11 23:03:27 +00:00
func (m *model) getClientById(ip string) *Client {
for i := range m.clients {
if m.clients[i].IP == ip {
return &m.clients[i].IP
2017-07-28 19:36:14 +00:00
}
}
return nil
}
2017-10-12 13:51:53 +00:00
func (m *model) SaveClient(cl *Client) error {
2017-07-06 17:09:49 +00:00
var err error
2017-10-12 13:51:53 +00:00
if err = m.openDB(); err != nil {
2017-07-06 17:09:49 +00:00
return nil
}
2017-10-12 13:51:53 +00:00
defer m.closeDB()
2017-07-06 17:09:49 +00:00
2017-10-12 13:51:53 +00:00
if err = db.bolt.SetBool(cl.mPath, "auth", c.Auth); err != nil {
2017-06-08 17:20:43 +00:00
return err
}
2017-10-12 13:51:53 +00:00
if err = db.bolt.SetValue(cl.mPath, "name", c.Name); err != nil {
2017-06-08 17:20:43 +00:00
return err
}
2017-10-12 13:51:53 +00:00
return db.bolt.SetValue(cl.mPath, "ip", c.IP)
2017-06-08 17:20:43 +00:00
}
2017-10-12 13:51:53 +00:00
/**
* OLD FUNCTIONS
*/
func (c *Client) saveVote(timestamp time.Time, votes []string) error {
2017-06-08 17:20:43 +00:00
var err error
if err = db.open(); err != nil {
return nil
2017-06-08 17:20:43 +00:00
}
defer db.close()
// Make sure we don't clobber a duplicate vote
votesBkt := []string{"votes", c.UUID, timestamp.Format(time.RFC3339)}
for i := range votes {
if strings.TrimSpace(votes[i]) != "" {
db.bolt.SetValue(votesBkt, strconv.Itoa(i), votes[i])
}
}
return err
2017-06-08 17:20:43 +00:00
}