/
orderCommentHandler.go
165 lines (156 loc) · 5.46 KB
/
orderCommentHandler.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
package handlers
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
jv "github.com/Ulbora/GoAuth2JwtValidator"
m "github.com/Ulbora/Six910/managers"
sdbi "github.com/Ulbora/six910-database-interface"
"github.com/gorilla/mux"
)
/*
Six910 is a shopping cart and E-commerce system.
Copyright (C) 2020 Ulbora Labs LLC. (www.ulboralabs.com)
All rights reserved.
Copyright (C) 2020 Ken Williamson
All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//OrderCommentReq OrderCommentReq
type OrderCommentReq struct {
StoreID int64 `json:"storeId"`
OrderComment sdbi.OrderComment `json:"orderComment"`
}
// AddOrderComments godoc
// @Summary Add a new orderComment
// @Description Adds a new orderComment to a store
// @Tags OrderComment
// @Accept json
// @Produce json
// @Param orderComment body OrderCommentReq true "orderComment"
// @Param apiKey header string false "apiKey required for non OAuth2 stores only"
// @Param storeName header string true "store name"
// @Param localDomain header string true "store localDomain"
// @Param Authorization header string true "token"
// @Param clientId header string false "OAuth2 client ID only for OAuth2 stores"
// @Param userId header string false "User ID only for OAuth2 stores"
// @Success 200 {object} managers.ResponseID
// @Router /rs/orderComment/add [post]
func (h *Six910Handler) AddOrderComments(w http.ResponseWriter, r *http.Request) {
var addorcURL = "/six910/rs/orderComment/add"
var aorcc jv.Claim
aorcc.Role = customerRole
aorcc.URL = addorcURL
aorcc.Scope = "write"
h.Log.Debug("client: ", h.ValidatorClient)
clientID := r.Header.Get("clientId")
var auth bool
if clientID != "" {
auth = h.processSecurity(r, &aorcc)
} else {
auth = h.processBasicSecurity(r, &aorcc)
}
h.Log.Debug("order comment add authorized: ", auth)
h.SetContentType(w)
if auth {
acOk := h.CheckContent(r)
h.Log.Debug("conOk: ", acOk)
if !acOk {
http.Error(w, "json required", http.StatusUnsupportedMediaType)
} else {
var aorc OrderCommentReq
aorcsuc, aorcerr := h.ProcessBody(r, &aorc)
h.Log.Debug("aorcsuc: ", aorcsuc)
h.Log.Debug("aorc: ", aorc)
h.Log.Debug("aorcerr: ", aorcerr)
if !aorcsuc && aorcerr != nil {
http.Error(w, aorcerr.Error(), http.StatusBadRequest)
} else {
aorcres := h.Manager.AddOrderComments(&aorc.OrderComment, aorc.StoreID)
h.Log.Debug("aorcres: ", *aorcres)
if aorcres.Success && aorcres.ID != 0 {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
resJSON, _ := json.Marshal(aorcres)
fmt.Fprint(w, string(resJSON))
}
}
} else {
var aorcf m.ResponseID
w.WriteHeader(http.StatusUnauthorized)
resJSON, _ := json.Marshal(aorcf)
fmt.Fprint(w, string(resJSON))
}
}
// GetOrderCommentList godoc
// @Summary Get list of orderComment
// @Description Get list of orderComment for a store
// @Tags OrderComment
// @Accept json
// @Produce json
// @Param orderId path string true "order Id"
// @Param storeId path string true "store storeId"
// @Param apiKey header string false "apiKey required for non OAuth2 stores only"
// @Param storeName header string true "store name"
// @Param localDomain header string true "store localDomain"
// @Param Authorization header string true "token"
// @Param clientId header string false "OAuth2 client ID only for OAuth2 stores"
// @Param userId header string false "User ID only for OAuth2 stores"
// @Success 200 {array} six910-database-interface.OrderComment
// @Router /rs/orderComment/get/list/{orderId}/{storeId} [get]
func (h *Six910Handler) GetOrderCommentList(w http.ResponseWriter, r *http.Request) {
var gorclURL = "/six910/rs/orderComment/list"
var gorccl jv.Claim
gorccl.Role = customerRole
gorccl.URL = gorclURL
gorccl.Scope = "read"
h.Log.Debug("client: ", h.ValidatorClient)
clientID := r.Header.Get("clientId")
var auth bool
if clientID != "" {
auth = h.processSecurity(r, &gorccl)
} else {
auth = h.processBasicSecurity(r, &gorccl)
}
h.Log.Debug("order comment get list authorized: ", auth)
h.SetContentType(w)
if auth {
vars := mux.Vars(r)
h.Log.Debug("vars: ", len(vars))
if vars != nil && len(vars) == 2 {
h.Log.Debug("vars: ", vars)
var orclordIDStr = vars["orderId"]
var orclstoreIDStr = vars["storeId"]
orderID, sorcloiderr := strconv.ParseInt(orclordIDStr, 10, 64)
storeID, sorclserr := strconv.ParseInt(orclstoreIDStr, 10, 64)
var gorclres *[]sdbi.OrderComment
if sorcloiderr == nil && sorclserr == nil {
gorclres = h.Manager.GetOrderCommentList(orderID, storeID)
h.Log.Debug("get order comment list: ", gorclres)
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusBadRequest)
var nc = []sdbi.OrderComment{}
gorclres = &nc
}
resJSON, _ := json.Marshal(gorclres)
fmt.Fprint(w, string(resJSON))
} else {
w.WriteHeader(http.StatusBadRequest)
}
} else {
w.WriteHeader(http.StatusUnauthorized)
}
}