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
/
signature.go
186 lines (150 loc) · 3.97 KB
/
signature.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
package api
import "encoding/json"
// Signature is a basic input.
type Signature struct {
PayloadName Payload `json:"Name" sql:"-"`
PayloadDate Payload `json:"Date" sql:"-"`
// Validator specific fields
Name *Text `json:"-" sql:"-"`
Date *DateControl `json:"-" sql:"-"`
// Persister specific fields
ID int `json:"-"`
AccountID int `json:"-"`
NameID int `json:"-"`
DateID int `json:"-"`
}
// Unmarshal bytes in to the entity properties.
func (entity *Signature) Unmarshal(raw []byte) error {
err := json.Unmarshal(raw, entity)
if err != nil {
return err
}
name, err := entity.PayloadName.Entity()
if err != nil {
return err
}
entity.Name = name.(*Text)
date, err := entity.PayloadDate.Entity()
if err != nil {
return err
}
entity.Date = date.(*DateControl)
return err
}
// Marshal to payload structure
func (entity *Signature) Marshal() Payload {
if entity.Name != nil {
entity.PayloadName = entity.Name.Marshal()
}
if entity.Date != nil {
entity.PayloadDate = entity.Date.Marshal()
}
return MarshalPayloadEntity("signature", entity)
}
// Valid checks the value(s) against an battery of tests.
func (entity *Signature) Valid() (bool, error) {
var stack ErrorStack
if ok, err := entity.Name.Valid(); !ok {
stack.Append("Name", err)
}
if ok, err := entity.Date.Valid(); !ok {
stack.Append("Date", err)
}
return !stack.HasErrors(), stack
}
// Save the signature to data storage.
func (entity *Signature) 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
}
nameID, err := entity.Name.Save(context, account)
if err != nil {
return nameID, err
}
entity.NameID = nameID
dateID, err := entity.Date.Save(context, account)
if err != nil {
return dateID, err
}
entity.DateID = dateID
if err := context.Save(entity); err != nil {
return entity.ID, err
}
return entity.ID, nil
}
// Delete the signature from data storage.
func (entity *Signature) 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 _, err := entity.Name.Delete(context, account); err != nil {
return entity.ID, err
}
if _, err := entity.Date.Delete(context, account); err != nil {
return entity.ID, err
}
if entity.ID != 0 {
if err := context.Delete(entity); err != nil {
return entity.ID, err
}
}
return entity.ID, nil
}
// Get the signature from data storage.
func (entity *Signature) Get(context DatabaseService, account int) (int, error) {
entity.AccountID = account
if err := context.CheckTable(entity); err != nil {
return entity.ID, err
}
if entity.ID != 0 {
if err := context.Select(entity); err != nil {
return entity.ID, err
}
}
if entity.NameID != 0 {
entity.Name = &Text{ID: entity.NameID}
if _, err := entity.Name.Get(context, account); err != nil {
return entity.ID, err
}
}
if entity.DateID != 0 {
entity.Date = &DateControl{ID: entity.DateID}
if _, err := entity.Date.Get(context, account); err != nil {
return entity.ID, err
}
}
return entity.ID, nil
}
// GetID returns the entity identifier.
func (entity *Signature) GetID() int {
return entity.ID
}
// SetID sets the entity identifier.
func (entity *Signature) SetID(id int) {
entity.ID = id
}
// Find the previous entity stored if one is available.
func (entity *Signature) Find(context DatabaseService) error {
context.Find(&Signature{ID: entity.ID}, func(result interface{}) {
previous := result.(*Signature)
if entity.Name == nil {
entity.Name = &Text{}
}
entity.NameID = previous.NameID
entity.Name.ID = previous.NameID
if entity.Date == nil {
entity.Date = &DateControl{}
}
entity.DateID = previous.DateID
entity.Date.ID = previous.DateID
})
return nil
}