/
clientmanager.go
97 lines (79 loc) · 2.27 KB
/
clientmanager.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
package clients
import (
"log"
"path/filepath"
"github.com/Zenika/MARCEL/backend/commons"
)
//Manager manage clients config.
type Manager struct {
Config *Config
configPath string
configFileName string
configFullPath string
}
type Config struct {
Clients map[string]*Client `json:"clients"`
}
func newManager(configPath string, configFileName string) *Manager {
return &Manager{
Config: newEmptyConfig(),
configPath: configPath,
configFileName: configFileName,
configFullPath: filepath.Join(configPath, configFileName),
}
}
func newEmptyConfig() *Config {
return &Config{
Clients: make(map[string]*Client),
}
}
//LoadFromDB loads medias configuration from DB and stor it in memory
func (m *Manager) LoadFromDB() {
log.Printf("Start Loading Clients from DB.")
commons.LoadFromDB(m)
log.Print("Clients configurations is loaded...")
}
//Commit writes the current configuration to the save file
func (m *Manager) Commit() error {
return commons.Commit(m)
}
//GetSaveFilePath return the path of the save file for the configuration
//Returns (fullPath, configPath, configFileName)
func (m *Manager) GetSaveFilePath() (string, string, string) {
return m.configFullPath, m.configPath, m.configFileName
}
//GetConfig return a non-typed pointer to the configuration of the manager
//This is use by commons.LoadFromDB for Decoding configuration from JSON.
func (m *Manager) GetConfig() interface{} {
return m.Config
}
//Get return the client associated to an ID
func (m *Manager) Get(clientID string) (*Client, bool) {
client, found := m.Config.Clients[clientID]
return client, found
}
//GetAll returns the complete map of registered clients (the client ID is the key)
func (m *Manager) GetAll() map[string]*Client {
return m.Config.Clients
}
func (m *Manager) addNewClient(name string, mediaID int) *Client {
client := newClient()
client.Name = name
client.MediaID = mediaID
m.Config.Clients[client.ID] = client
m.Commit()
return client
}
func (m *Manager) deleteClient(clientID string) {
delete(m.Config.Clients, clientID)
m.Commit()
}
func (m *Manager) deleteAllClients() {
m.Config.Clients = map[string]*Client{}
m.Commit()
}
func (m *Manager) updateClient(client *Client) *Client {
m.Config.Clients[client.ID] = client
m.Commit()
return client
}