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
/
datecontrol.go
128 lines (103 loc) · 2.79 KB
/
datecontrol.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
package api
import (
"encoding/json"
"strconv"
"strings"
"time"
)
// DateControl is a basic input.
type DateControl struct {
ID int `json:"-"`
Month string `json:"month"`
Day string `json:"day"`
Year string `json:"year"`
Estimated bool `json:"estimated"`
}
// Date is composed from the structure properties
func (entity *DateControl) Date() time.Time {
m, err := strconv.Atoi(entity.Month)
if err != nil {
return time.Time{}
}
d, err := strconv.Atoi(entity.Day)
if err != nil {
return time.Time{}
}
y, err := strconv.Atoi(entity.Year)
if err != nil {
return time.Time{}
}
return time.Date(y, time.Month(m), d, 0, 0, 0, 0, time.UTC)
}
// Unmarshal bytes in to the entity properties.
func (entity *DateControl) Unmarshal(raw []byte) error {
return json.Unmarshal(raw, entity)
}
// Marshal to payload structure
func (entity *DateControl) Marshal() Payload {
return MarshalPayloadEntity("datecontrol", entity)
}
// Valid checks the value(s) against an battery of tests.
func (entity *DateControl) Valid() (bool, error) {
var stack ErrorStack
if strings.TrimSpace(entity.Month) == "" {
stack.Append("Month", ErrFieldRequired{"Month is required"})
}
if strings.TrimSpace(entity.Day) == "" {
if entity.Estimated {
entity.Day = "15"
} else {
stack.Append("Day", ErrFieldRequired{"Day is required"})
}
}
if strings.TrimSpace(entity.Year) == "" {
stack.Append("Year", ErrFieldRequired{"Year is required"})
}
return !stack.HasErrors(), stack
}
// Save the date control to data storage.
func (entity *DateControl) Save(context DatabaseService, account int) (int, error) {
if err := context.CheckTable(entity); err != nil {
return entity.ID, err
}
if err := context.Save(entity); err != nil {
return entity.ID, err
}
return entity.ID, nil
}
// Delete the date control from data storage.
func (entity *DateControl) Delete(context DatabaseService, account int) (int, error) {
if err := context.CheckTable(entity); 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 date control from data storage.
func (entity *DateControl) Get(context DatabaseService, account int) (int, error) {
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
}
}
return entity.ID, nil
}
// GetID returns the entity identifier.
func (entity *DateControl) GetID() int {
return entity.ID
}
// SetID sets the entity identifier.
func (entity *DateControl) SetID(id int) {
entity.ID = id
}
// Find is not used for date controls. Please use the `Get` method.
func (entity *DateControl) Find(context DatabaseService) error {
return nil
}