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
/
country.go
89 lines (70 loc) · 1.99 KB
/
country.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
package api
import "encoding/json"
// Country is a basic input.
type Country struct {
ID int `json:"-"`
Value []string `json:"value" pg:",array"`
Comments string `json:"comments,omitempty"`
}
// Unmarshal bytes in to the entity properties.
func (entity *Country) Unmarshal(raw []byte) error {
return json.Unmarshal(raw, entity)
}
// Marshal to payload structure
func (entity *Country) Marshal() Payload {
return MarshalPayloadEntity("country", entity)
}
// Valid checks the value(s) against an battery of tests.
func (entity *Country) Valid() (bool, error) {
var stack ErrorStack
if len(entity.Value) == 0 {
stack.Append("Country", ErrFieldRequired{"Country is required"})
}
return !stack.HasErrors(), stack
}
// Save the country to data storage.
func (entity *Country) 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 country from data storage.
func (entity *Country) 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 country from data storage.
func (entity *Country) 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 *Country) GetID() int {
return entity.ID
}
// SetID sets the entity identifier.
func (entity *Country) SetID(id int) {
entity.ID = id
}
// Find is not used for countries. Please use the `Get` method.
func (entity *Country) Find(context DatabaseService) error {
return nil
}