This repository has been archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
supervisor.go
332 lines (277 loc) · 8.03 KB
/
supervisor.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package api
import "encoding/json"
// Supervisor is a basic input.
type Supervisor struct {
PayloadSupervisorName Payload `json:"SupervisorName" sql:"-"`
PayloadTitle Payload `json:"Title" sql:"-"`
PayloadEmail Payload `json:"Email" sql:"-"`
PayloadEmailNotApplicable Payload `json:"EmailNotApplicable" sql:"-"`
PayloadAddress Payload `json:"Address" sql:"-"`
PayloadTelephone Payload `json:"Telephone" sql:"-"`
// Validator specific fields
SupervisorName *Text `json:"-"`
Title *Text `json:"-"`
Email *Email `json:"-"`
EmailNotApplicable *NotApplicable `json:"-"`
Address *Location `json:"-"`
Telephone *Telephone `json:"-"`
// Persister specific fields
ID int `json:"-"`
AccountID int `json:"-"`
SupervisorNameID int `json:"-"`
TitleID int `json:"-"`
EmailID int `json:"-"`
EmailNotApplicableID int `json:"-"`
AddressID int `json:"-"`
TelephoneID int `json:"-"`
}
// Unmarshal bytes in to the entity properties.
func (entity *Supervisor) Unmarshal(raw []byte) error {
err := json.Unmarshal(raw, entity)
if err != nil {
return err
}
supervisorName, err := entity.PayloadSupervisorName.Entity()
if err != nil {
return err
}
entity.SupervisorName = supervisorName.(*Text)
title, err := entity.PayloadTitle.Entity()
if err != nil {
return err
}
entity.Title = title.(*Text)
email, err := entity.PayloadEmail.Entity()
if err != nil {
return err
}
entity.Email = email.(*Email)
emailNotApplicable, err := entity.PayloadEmailNotApplicable.Entity()
if err != nil {
return err
}
entity.EmailNotApplicable = emailNotApplicable.(*NotApplicable)
address, err := entity.PayloadAddress.Entity()
if err != nil {
return err
}
entity.Address = address.(*Location)
telephone, err := entity.PayloadTelephone.Entity()
if err != nil {
return err
}
entity.Telephone = telephone.(*Telephone)
return err
}
// Marshal to payload structure
func (entity *Supervisor) Marshal() Payload {
if entity.SupervisorName != nil {
entity.PayloadSupervisorName = entity.SupervisorName.Marshal()
}
if entity.Title != nil {
entity.PayloadTitle = entity.Title.Marshal()
}
if entity.Email != nil {
entity.PayloadEmail = entity.Email.Marshal()
}
if entity.EmailNotApplicable != nil {
entity.PayloadEmailNotApplicable = entity.EmailNotApplicable.Marshal()
}
if entity.Address != nil {
entity.PayloadAddress = entity.Address.Marshal()
}
if entity.Telephone != nil {
entity.PayloadTelephone = entity.Telephone.Marshal()
}
return MarshalPayloadEntity("supervisor", entity)
}
// Valid checks the value(s) against an battery of tests.
func (entity *Supervisor) Valid() (bool, error) {
if ok, err := entity.SupervisorName.Valid(); !ok {
return false, err
}
if ok, err := entity.Title.Valid(); !ok {
return false, err
}
if entity.EmailNotApplicable.Applicable {
if ok, err := entity.Email.Valid(); !ok {
return false, err
}
}
if ok, err := entity.Address.Valid(); !ok {
return false, err
}
if ok, err := entity.Telephone.Valid(); !ok {
return false, err
}
return true, nil
}
// Save the Supervisor entity.
func (entity *Supervisor) Save(context DatabaseService, account int) (int, error) {
entity.AccountID = account
if err := context.CheckTable(entity); err != nil {
return entity.ID, err
}
if err := entity.Find(context); err != nil {
return entity.ID, err
}
supervisorNameID, err := entity.SupervisorName.Save(context, account)
if err != nil {
return supervisorNameID, err
}
entity.SupervisorNameID = supervisorNameID
titleID, err := entity.Title.Save(context, account)
if err != nil {
return titleID, err
}
entity.TitleID = titleID
emailID, err := entity.Email.Save(context, account)
if err != nil {
return emailID, err
}
entity.EmailID = emailID
emailNotApplicableID, err := entity.EmailNotApplicable.Save(context, account)
if err != nil {
return emailNotApplicableID, err
}
entity.EmailNotApplicableID = emailNotApplicableID
addressID, err := entity.Address.Save(context, account)
if err != nil {
return addressID, err
}
entity.AddressID = addressID
telephoneID, err := entity.Telephone.Save(context, account)
if err != nil {
return telephoneID, err
}
entity.TelephoneID = telephoneID
if err := context.Save(entity); err != nil {
return entity.ID, err
}
return entity.ID, nil
}
// Delete the Supervisor entity.
func (entity *Supervisor) Delete(context DatabaseService, account int) (int, error) {
entity.AccountID = account
if err := context.CheckTable(entity); err != nil {
return entity.ID, err
}
if err := entity.Find(context); err != nil {
return entity.ID, err
}
if entity.ID != 0 {
if err := context.Delete(entity); err != nil {
return entity.ID, err
}
}
if _, err := entity.SupervisorName.Delete(context, account); err != nil {
return entity.ID, err
}
if _, err := entity.Title.Delete(context, account); err != nil {
return entity.ID, err
}
if _, err := entity.Email.Delete(context, account); err != nil {
return entity.ID, err
}
if _, err := entity.EmailNotApplicable.Delete(context, account); err != nil {
return entity.ID, err
}
if _, err := entity.Address.Delete(context, account); err != nil {
return entity.ID, err
}
if _, err := entity.Telephone.Delete(context, account); err != nil {
return entity.ID, err
}
return entity.ID, nil
}
// Get the Supervisor entity.
func (entity *Supervisor) Get(context DatabaseService, account int) (int, error) {
entity.AccountID = account
if err := context.CheckTable(entity); err != nil {
return entity.ID, err
}
if err := entity.Find(context); err != nil {
return entity.ID, err
}
if entity.ID != 0 {
if err := context.Select(entity); err != nil {
return entity.ID, err
}
}
if entity.SupervisorNameID != 0 {
if _, err := entity.SupervisorName.Get(context, account); err != nil {
return entity.ID, err
}
}
if entity.TitleID != 0 {
if _, err := entity.Title.Get(context, account); err != nil {
return entity.ID, err
}
}
if entity.EmailID != 0 {
if _, err := entity.Email.Get(context, account); err != nil {
return entity.ID, err
}
}
if entity.EmailNotApplicableID != 0 {
if _, err := entity.EmailNotApplicable.Get(context, account); err != nil {
return entity.ID, err
}
}
if entity.AddressID != 0 {
if _, err := entity.Address.Get(context, account); err != nil {
return entity.ID, err
}
}
if entity.TelephoneID != 0 {
if _, err := entity.Telephone.Get(context, account); err != nil {
return entity.ID, err
}
}
return entity.ID, nil
}
// GetID returns the entity identifier.
func (entity *Supervisor) GetID() int {
return entity.ID
}
// SetID sets the entity identifier.
func (entity *Supervisor) SetID(id int) {
entity.ID = id
}
// Find the previous entity stored if one is available.
func (entity *Supervisor) Find(context DatabaseService) error {
context.Find(&Supervisor{ID: entity.ID, AccountID: entity.AccountID}, func(result interface{}) {
previous := result.(*Supervisor)
if entity.SupervisorName == nil {
entity.SupervisorName = &Text{}
}
entity.SupervisorName.ID = previous.SupervisorNameID
entity.SupervisorNameID = previous.SupervisorNameID
if entity.Title == nil {
entity.Title = &Text{}
}
entity.Title.ID = previous.TitleID
entity.TitleID = previous.TitleID
if entity.Email == nil {
entity.Email = &Email{}
}
entity.Email.ID = previous.EmailID
entity.EmailID = previous.EmailID
if entity.EmailNotApplicable == nil {
entity.EmailNotApplicable = &NotApplicable{}
}
entity.EmailNotApplicable.ID = previous.EmailNotApplicableID
entity.EmailNotApplicableID = previous.EmailNotApplicableID
if entity.Address == nil {
entity.Address = &Location{}
}
entity.Address.ID = previous.AddressID
entity.AddressID = previous.AddressID
if entity.Telephone == nil {
entity.Telephone = &Telephone{}
}
entity.Telephone.ID = previous.TelephoneID
entity.TelephoneID = previous.TelephoneID
})
return nil
}