-
Notifications
You must be signed in to change notification settings - Fork 4
/
model_clients.go
184 lines (164 loc) · 3.68 KB
/
model_clients.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"errors"
"github.com/pborman/uuid"
)
/**
* Client
* A client is a system that is connecting to the web server
*/
type Client struct {
UUID string
Auth bool
Name string
IP string
mPath []string // The path in the DB to this client
}
func NewClient(id string) *Client {
if id == "" {
id = uuid.New()
}
return &Client{
UUID: id,
mPath: []string{"clients", id},
}
}
func (m *model) AddClient(cl *Client) error {
for i := range m.clients {
if m.clients[i].UUID == cl.UUID {
return errors.New("A client with that ID already exists")
}
if m.clients[i].IP == cl.IP {
return errors.New("A client with that IP already exists")
}
if m.clients[i].Name == cl.Name {
return errors.New("A client with that Name already exists")
}
}
m.clients = append(m.clients, *cl)
m.clientsUpdated = true
return nil
}
/**
* DB Functions
* These are generally just called when the app starts up, or when the periodic 'save' runs
*/
// Load all clients from the DB
func (m *model) LoadAllClients() []Client {
var err error
var ret []Client
if err = m.openDB(); err != nil {
return ret
}
defer m.closeDB()
var clientUids []string
cliPath := []string{"clients"}
if clientUids, err = m.bolt.GetBucketList(cliPath); err != nil {
return ret
}
for _, v := range clientUids {
if cl := m.LoadClient(v); cl != nil {
ret = append(ret, *cl)
}
}
return ret
}
// Load a client from the DB and return it
func (m *model) LoadClient(clId string) *Client {
var err error
if err = m.openDB(); err != nil {
return nil
}
defer m.closeDB()
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")
return cl
}
// SaveAllClients saves all clients to the DB
func (m *model) SaveAllClients() error {
var err error
if err = m.openDB(); err != nil {
return nil
}
defer m.closeDB()
for _, v := range m.clients {
if err = m.SaveClient(&v); err != nil {
return err
}
}
return nil
}
// SaveClient saves a client to the DB
func (m *model) SaveClient(cl *Client) error {
var err error
if err = m.openDB(); err != nil {
return nil
}
defer m.closeDB()
if err = m.bolt.SetBool(cl.mPath, "auth", cl.Auth); err != nil {
return err
}
if err = m.bolt.SetValue(cl.mPath, "name", cl.Name); err != nil {
return err
}
return m.bolt.SetValue(cl.mPath, "ip", cl.IP)
}
/**
* In Memory functions
* This is generally how the app accesses client data
*/
// Return a client by it's UUID
func (m *model) GetClient(id string) (*Client, error) {
for i := range m.clients {
if m.clients[i].UUID == id {
return &m.clients[i], nil
}
}
return nil, errors.New("Invalid Id")
}
// Return a client by it's IP address
func (m *model) GetClientByIp(ip string) (*Client, error) {
for i := range m.clients {
if m.clients[i].IP == ip {
return &m.clients[i], nil
}
}
return nil, errors.New("Invalid Ip")
}
// Add/Update a client in the data model
func (m *model) UpdateClient(cl *Client) {
var found bool
for i := range m.clients {
if m.clients[i].UUID == cl.UUID {
found = true
m.clients[i].Auth = cl.Auth
m.clients[i].Name = cl.Name
m.clients[i].IP = cl.IP
}
}
if !found {
m.clients = append(m.clients, *cl)
}
m.clientsUpdated = true
}
func (m *model) DeleteClient(id string) error {
idx := -1
for i := range m.clients {
if m.clients[i].UUID == id {
idx = i
break
}
}
if idx == -1 {
return errors.New("Invalid Client ID given")
}
m.clients = append(m.clients[:idx], m.clients[idx+1:]...)
// Now delete it from the DB
if err := m.openDB(); err != nil {
return nil
}
defer m.closeDB()
return m.bolt.DeleteBucket([]string{"clients"}, id)
}