-
Notifications
You must be signed in to change notification settings - Fork 4
/
category.i_object.go
311 lines (266 loc) · 7.8 KB
/
category.i_object.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package category
import (
"strings"
"github.com/ottemo/commerce/app/models"
"github.com/ottemo/commerce/app/models/category"
"github.com/ottemo/commerce/app/models/product"
"github.com/ottemo/commerce/db"
"github.com/ottemo/commerce/env"
"github.com/ottemo/commerce/utils"
)
// updatePath is an internal function used to update "path" attribute of object
func (it *DefaultCategory) updatePath() {
if it.GetID() == "" {
it.Path = ""
} else if it.Parent != nil {
parentPath, ok := it.Parent.Get("path").(string)
if ok {
it.Path = parentPath + "/" + it.GetID()
}
} else {
it.Path = "/" + it.GetID()
}
}
// Get returns object attribute value or nil
func (it *DefaultCategory) Get(attribute string) interface{} {
switch strings.ToLower(attribute) {
case "_id", "id":
return it.GetID()
case "enabled":
return it.GetEnabled()
case "name":
return it.GetName()
case "path":
if it.Path == "" {
it.updatePath()
}
return it.Path
case "parent_id":
if it.Parent != nil {
return it.Parent.GetID()
}
return ""
case "parent":
return it.GetParent()
case "image":
return it.GetImage()
case "description":
return it.GetDescription()
case "product_ids":
return it.GetProductIds()
case "products":
var result []map[string]interface{}
for _, categoryProduct := range it.GetProducts() {
result = append(result, categoryProduct.ToHashMap())
}
return result
}
return nil
}
// Set sets attribute value to object or returns error
func (it *DefaultCategory) Set(attribute string, value interface{}) error {
attribute = strings.ToLower(attribute)
switch attribute {
case "_id", "id":
if err := it.SetID(utils.InterfaceToString(value)); err != nil {
_ = env.ErrorNew(ConstErrorModule, ConstErrorLevel, "ef5400da-4a42-4b37-8747-e25a24bc6a45", err.Error())
}
case "enabled":
it.Enabled = utils.InterfaceToBool(value)
case "name":
it.Name = utils.InterfaceToString(value)
case "parent_id":
if value, ok := value.(string); ok {
value = strings.TrimSpace(value)
if value != "" {
model, err := models.GetModel("Category")
if err != nil {
return env.ErrorDispatch(err)
}
categoryModel, ok := model.(category.InterfaceCategory)
if !ok {
return env.ErrorNew(ConstErrorModule, ConstErrorLevel, "39b6496a-4145-4b16-9f67-ca6375fd8b1f", "unsupported category model "+model.GetImplementationName())
}
err = categoryModel.Load(value)
if err != nil {
return env.ErrorDispatch(err)
}
selfID := it.GetID()
if selfID != "" {
parentPath, ok := categoryModel.Get("path").(string)
if categoryModel.GetID() != selfID && ok && !strings.Contains(parentPath, selfID) {
it.Parent = categoryModel
} else {
return env.ErrorNew(ConstErrorModule, ConstErrorLevel, "0ae64841-1123-4add-8250-c4f324ad8eab", "category can't have sub-category or itself as parent")
}
} else {
it.Parent = categoryModel
}
} else {
it.Parent = nil
}
} else {
return env.ErrorNew(ConstErrorModule, ConstErrorLevel, "04ac194b-1912-4520-9087-b0248b9ea758", "unsupported id specified")
}
it.updatePath()
case "parent":
switch value := value.(type) {
case category.InterfaceCategory:
it.Parent = value
case string:
if err := it.Set("parent_id", value); err != nil {
_ = env.ErrorNew(ConstErrorModule, ConstErrorLevel, "d0ee8340-3de0-43e0-8d1d-9da85887cfb7", err.Error())
}
default:
_ = env.ErrorNew(ConstErrorModule, ConstErrorLevel, "2156d563-932b-4de7-a615-7d473717a3bd", "unsupported 'parent' value")
}
// path should be changed as well
it.updatePath()
case "image":
it.Image = utils.InterfaceToString(value)
case "description":
it.Description = utils.InterfaceToString(value)
case "products":
switch typedValue := value.(type) {
case []interface{}:
it.ProductIds = make([]string, 0)
for _, listItem := range typedValue {
productID, ok := listItem.(string)
if ok {
productModel, err := product.LoadProductByID(productID)
if err != nil {
return env.ErrorDispatch(err)
}
it.ProductIds = append(it.ProductIds, productModel.GetID())
}
}
case []product.InterfaceProduct:
it.ProductIds = make([]string, 0)
for _, productItem := range typedValue {
it.ProductIds = append(it.ProductIds, productItem.GetID())
}
default:
return env.ErrorNew(ConstErrorModule, ConstErrorLevel, "84284b03-0a29-4036-aa2d-b35768884b63", "unsupported 'products' value")
}
}
return nil
}
// FromHashMap fills object attributes from map[string]interface{}
func (it *DefaultCategory) FromHashMap(input map[string]interface{}) error {
for attribute, value := range input {
if err := it.Set(attribute, value); err != nil {
_ = env.ErrorDispatch(err)
}
}
return nil
}
// ToHashMap represents object as map[string]interface{}
func (it *DefaultCategory) ToHashMap() map[string]interface{} {
result := make(map[string]interface{})
result["_id"] = it.GetID()
result["enabled"] = it.Get("enabled")
result["description"] = it.Get("description")
result["image"] = it.Get("image")
result["parent_id"] = it.Get("parent_id")
result["name"] = it.Get("name")
result["product_ids"] = it.Get("product_ids")
result["path"] = it.Get("path")
return result
}
// GetAttributesInfo returns information about object attributes
func (it *DefaultCategory) GetAttributesInfo() []models.StructAttributeInfo {
info := []models.StructAttributeInfo{
models.StructAttributeInfo{
Model: category.ConstModelNameCategory,
Collection: ConstCollectionNameCategory,
Attribute: "_id",
Type: db.ConstTypeID,
IsRequired: false,
IsStatic: true,
Label: "ID",
Group: "General",
Editors: "not_editable",
Options: "",
Default: "",
},
models.StructAttributeInfo{
Model: category.ConstModelNameCategory,
Collection: ConstCollectionNameCategory,
Attribute: "enabled",
Type: db.ConstTypeBoolean,
IsRequired: true,
IsStatic: true,
Label: "Enabled",
Group: "General",
Editors: "boolean",
Options: "",
Default: "",
},
models.StructAttributeInfo{
Model: category.ConstModelNameCategory,
Collection: ConstCollectionNameCategory,
Attribute: "name",
Type: db.ConstTypeText,
IsRequired: true,
IsStatic: true,
Label: "Name",
Group: "General",
Editors: "line_text",
Options: "",
Default: "",
},
models.StructAttributeInfo{
Model: category.ConstModelNameCategory,
Collection: ConstCollectionNameCategory,
Attribute: "parent_id",
Type: db.ConstTypeID,
IsRequired: false,
IsStatic: true,
Label: "Parent",
Group: "General",
Editors: "category_selector",
Options: "",
Default: "",
},
models.StructAttributeInfo{
Model: category.ConstModelNameCategory,
Collection: ConstCollectionNameCategory,
Attribute: "description",
Type: db.ConstTypeText,
IsRequired: false,
IsStatic: true,
Label: "Description",
Group: "General",
Editors: "multiline_text",
Options: "",
Default: "",
},
models.StructAttributeInfo{
Model: category.ConstModelNameCategory,
Collection: ConstCollectionNameCategory,
Attribute: "image",
Type: db.ConstTypeVarchar,
IsRequired: false,
IsStatic: true,
Label: "Image",
Group: "General",
Editors: "image_selector",
Options: "",
Default: "",
},
models.StructAttributeInfo{
Model: category.ConstModelNameCategory,
Collection: ConstCollectionNameCategory,
Attribute: "product_ids",
Type: db.TypeArrayOf(db.ConstTypeID),
IsRequired: false,
IsStatic: true,
Label: "Products",
Group: "General",
Editors: "products_selector",
Options: "",
Default: "",
},
}
return info
}