-
Notifications
You must be signed in to change notification settings - Fork 3
/
api_category.go
149 lines (93 loc) · 3.32 KB
/
api_category.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
package main
import (
"net/http"
"strings"
b64 "encoding/base64"
"github.com/romana/rlog"
"go.mongodb.org/mongo-driver/bson"
)
func getProductSkusInCategory(w http.ResponseWriter, r *http.Request) {
rlog.Debug("getProductsInCategory() handle function invoked ...")
if !pre(w, r) {
return
}
csx := getAccessToken(r)
ctcol := csx + CategoryTreeExtension
var ctrq CATEGORYREQUEST
if !mapInput(w, r, &ctrq) {
return
}
path := cleanCategoryPath(ctrq.Path)
if !pathExists(w, r, path, ExternalDB+csx, ctcol) {
respondWith(w, r, nil, "Category path does not exit ...", nil, http.StatusBadRequest, false)
return
}
SKUs := getSKUsInTheCategoryPath(w, r, path, ExternalDB+csx, ctcol, true)
respondWith(w, r, nil, "Products in category path ...", bson.M{path: SKUs}, http.StatusOK, true)
}
func getProductsInCategory(w http.ResponseWriter, r *http.Request) {
rlog.Debug("getProductsInCategory() handle function invoked ...")
if !pre(w, r) {
return
}
csx := getAccessToken(r)
ctcol := csx + CategoryTreeExtension
pth := strings.Split(r.URL.Path, "/")
cid := pth[len(pth)-1]
pathx, _ := b64.StdEncoding.DecodeString(cid)
path := cleanCategoryPath(string(pathx))
if !pathExists(w, r, path, ExternalDB+csx, ctcol) {
respondWith(w, r, nil, "Category path does not exit ...", nil, http.StatusBadRequest, false)
return
}
products := getProductsInTheCategoryPath(w, r, path, ExternalDB+csx, ctcol, true, csx)
respondWith(w, r, nil, "Products in category path ...", products, http.StatusOK, true)
}
func getRootCategory(w http.ResponseWriter, r *http.Request) {
rlog.Debug("getRootCategory() handle function invoked ...")
if !pre(w, r) {
return
}
csx := getAccessToken(r)
ctcol := csx + CategoryTreeExtension
cats := getRootCategories(w, r, ExternalDB+csx, ctcol)
respondWith(w, r, nil, "Root categories ...", cats, http.StatusOK, true)
}
func getImmediateSubCategories(w http.ResponseWriter, r *http.Request) {
rlog.Debug("getImmediateSubCategories() handle function invoked ...")
if !pre(w, r) {
return
}
csx := getAccessToken(r)
ctcol := csx + CategoryTreeExtension
pth := strings.Split(r.URL.Path, "/")
cid := pth[len(pth)-1]
catNode := getCategoryNodeByID(w, r, cid, ExternalDB+csx, ctcol)
var childNodes []*CATEGORYTREENODE
if catNode.Children == nil {
respondWith(w, r, nil, "Category with ID: "+cid+" does not have a sub category ...", nil, http.StatusNotFound, false)
return
}
for _, child := range catNode.Children {
childNodes = append(childNodes, getCategoryNode(w, r, child, ExternalDB+csx, ctcol))
}
respondWith(w, r, nil, "Immediate Sub categories ...", childNodes, http.StatusOK, true)
}
func getParentCategory(w http.ResponseWriter, r *http.Request) {
rlog.Debug("getParentCategory() handle function invoked ...")
if !pre(w, r) {
return
}
csx := getAccessToken(r)
ctcol := csx + CategoryTreeExtension
pth := strings.Split(r.URL.Path, "/")
cid := pth[len(pth)-1]
catNode := getCategoryNodeByID(w, r, cid, ExternalDB+csx, ctcol)
var parentNode *CATEGORYTREENODE
if catNode.Parent == "" {
respondWith(w, r, nil, "Category "+cid+" does not have a parent ...", nil, http.StatusNotFound, false)
return
}
parentNode = getCategoryNode(w, r, catNode.Parent, ExternalDB+csx, ctcol)
respondWith(w, r, nil, "Category parent ...", parentNode, http.StatusOK, true)
}