-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlineitem.go
220 lines (195 loc) · 5.57 KB
/
lineitem.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
package apifaker
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/jinzhu/inflection"
"sort"
"strconv"
)
type LineItem struct {
dataMap map[string]interface{}
}
// NewLineItemWithMap allocates and returns a new LineItem,
// using the dataMap param as the new LineItem's dataMap
func NewLineItemWithMap(dataMap map[string]interface{}) LineItem {
return LineItem{dataMap}
}
// NewLineItemWithGinContext allocates and returns a new LineItem,
// its keys are from Model.Cloumns, values are from gin.Contex.PostForm(),
// error will be not nil if gin.Contex.PostForm() has no value for any key
func NewLineItemWithGinContext(ctx *gin.Context, model *Model) (LineItem, error) {
li := LineItem{make(map[string]interface{})}
for _, column := range model.Columns {
// skip id column
if column.Name == "id" {
continue
}
if value := ctx.PostForm(column.Name); value != "" {
li.SetStringValue(column.Name, value, column.Type)
} else {
return li, fmt.Errorf("doesn't has column: %s", column.Name)
}
}
return li, nil
}
// ID returns the float64 of id
func (li *LineItem) ID() float64 {
return li.Id().(float64)
}
// Id returns the value of LineItem's dataMap["id"]
// it will panic if got a nil or a not float64 "id"
func (li LineItem) Id() interface{} {
if id, ok := li.Get("id"); ok {
if idFloat64, ok := id.(float64); ok {
return idFloat64
} else {
panic(fmt.Sprintf("[LineItem]: id must be a float64, %#v\n", li.ToMap()))
}
} else {
panic(fmt.Sprintf("[LineItem]: must has id: %#v\n", li.ToMap()))
}
}
// Len returns LineItem's dataMap's length
func (li LineItem) Len() int {
return len(li.dataMap)
}
// Get gets dataMap[key] and returns its value and exsiting in LinItem
func (li LineItem) Get(key string) (interface{}, bool) {
value, ok := li.dataMap[key]
return value, ok
}
// Set set key-value of dataMap in LineItem
func (li *LineItem) Set(key string, value interface{}) {
li.dataMap[key] = value
}
// SetStringValue formats the given string value with given valueType,
// sets key into LineItem formated value
func (li *LineItem) SetStringValue(key, value, valueType string) error {
formatValue, err := FormatValue(valueType, value)
if err != nil {
return err
} else {
li.Set(key, formatValue)
}
return nil
}
// InsertRelatedData allocates and returns a new LineItem,
// it will has all data of the caller LineItem,
// it will insert all related data if the given Model's has any Column named xxx_id
func (li LineItem) InsertRelatedData(model *Model) LineItem {
// has one relationship
newLi := NewLineItemWithMap(li.ToMap())
singularName := inflection.Singular(model.Name)
// HasOne
for _, resName := range model.HasOne {
resStruct := map[string]interface{}{}
if resRouter, ok := model.router.apiFaker.Routers[inflection.Plural(resName)]; ok {
resLis := resRouter.Model.ToLineItems()
sort.Sort(resLis)
for _, resLi := range resLis {
curId, ok := resLi.Get(fmt.Sprintf("%s_id", singularName))
if ok && curId == newLi.Id() {
resStruct = resLi.ToMap()
break
}
}
}
if len(resStruct) > 0 {
newLi.Set(inflection.Singular(resName), resStruct)
}
}
// HasMany
for _, resName := range model.HasMany {
resSlice := []interface{}{}
if resRouter, ok := model.router.apiFaker.Routers[resName]; ok {
resLis := resRouter.Model.ToLineItems()
sort.Sort(resLis)
for _, resLi := range resLis {
curId, ok := resLi.Get(fmt.Sprintf("%s_id", singularName))
if ok && curId == newLi.Id() {
resSlice = append(resSlice, resLi.ToMap())
}
}
}
if len(resSlice) > 0 {
newLi.Set(resName, resSlice)
}
}
return newLi
}
// DeleteRelatedLis deletes all related data
func (li LineItem) DeleteRelatedLis(id float64, model *Model) {
_, ok := model.Get(id)
if !ok {
return
}
for _, rotuer := range model.router.apiFaker.Routers {
var isRelatedRouter bool
var foreign_key = fmt.Sprintf("%s_id", inflection.Singular(model.Name))
for _, column := range rotuer.Model.Columns {
if column.Name == foreign_key {
isRelatedRouter = true
break
}
}
if isRelatedRouter {
for _, li := range rotuer.Model.ToLineItems() {
key, ok := li.Get(foreign_key)
if ok && key == id {
rotuer.Model.Delete(li.ID())
}
}
}
}
}
// FormatValue format the given string value described by the given valueType
func FormatValue(valueType, value string) (interface{}, error) {
var nilValue interface{}
switch valueType {
case number.Name():
if numberVal, err := strconv.ParseFloat(value, 64); err != nil {
return nilValue, err
} else {
return numberVal, nil
}
case boolean.Name():
if booleanVal, err := strconv.ParseBool(value); err != nil {
return nilValue, err
} else {
return booleanVal, nil
}
default:
return value, nil
}
}
// ToMap allocates and returns a new map[string]interface{} filled with LineItem's dataMap
func (li LineItem) ToMap() map[string]interface{} {
newMap := map[string]interface{}{}
for k, v := range li.dataMap {
newMap[k] = v
}
return newMap
}
type LineItems []LineItem
// Len returns LineItems's length
func (lis LineItems) Len() int {
return len(lis)
}
// Len returns comparation of value's of two LineItem's "id"
func (lis LineItems) Less(i, j int) bool {
return lis[i].ID() < lis[j].ID()
}
// Swap swaps two LineItem
func (lis LineItems) Swap(i, j int) {
tmpItem := lis[i]
lis[i] = lis[j]
lis[j] = tmpItem
}
// ToSlice return a []map[string]interface{} filled with LineItems' elements
func (lis LineItems) ToSlice() []map[string]interface{} {
slice := []map[string]interface{}{}
for _, li := range lis {
slice = append(slice, li.ToMap())
}
return slice
}