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
/
branch.go
96 lines (76 loc) · 2.05 KB
/
branch.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
package api
import (
"encoding/json"
"strings"
)
// Branch is a basic yes/no input.
type Branch struct {
ID int `json:"-"`
Value string `json:"value"`
}
// Unmarshal bytes in to the entity properties.
func (entity *Branch) Unmarshal(raw []byte) error {
return json.Unmarshal(raw, entity)
}
// Marshal to payload structure
func (entity *Branch) Marshal() Payload {
return MarshalPayloadEntity("branch", entity)
}
// Valid checks the value(s) against an battery of tests.
func (entity *Branch) Valid() (bool, error) {
var stack ErrorStack
trimmed := strings.TrimSpace(string(entity.Value))
if trimmed == "" {
stack.Append("Branch", ErrFieldRequired{"Branch value is required"})
}
if trimmed != "Yes" && trimmed != "No" {
stack.Append("Branch", ErrFieldInvalid{"Invalid value for branch"})
}
return !stack.HasErrors(), stack
}
// Save the Branch entity.
func (entity *Branch) 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 Branch entity.
func (entity *Branch) 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 Branch entity.
func (entity *Branch) 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 *Branch) GetID() int {
return entity.ID
}
// SetID sets the entity identifier.
func (entity *Branch) SetID(id int) {
entity.ID = id
}
// Find the previous entity stored if one is available.
func (entity *Branch) Find(context DatabaseService) error {
return nil
}