/
credit_card.go
301 lines (232 loc) · 7.77 KB
/
credit_card.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
package api
import (
"encoding/json"
"net/http"
"strconv"
"github.com/scmn-dev/core/app"
"github.com/scmn-dev/core/db"
"github.com/scmn-dev/core/model"
"github.com/spf13/viper"
"github.com/gorilla/mux"
)
const (
// InvalidRequestPayload represents invalid request payload messaage
InvalidRequestPayload = "Invalid request payload"
// CreditCardDeleted represents message when deleting credit cart successfully
CreditCardDeleted = "CreditCard deleted successfully!"
// Success represent success message
Success = "Success"
)
// FindAllCreditCards finds all credid carts
func FindAllCreditCards(s db.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var err error
var creditCardList []model.CreditCard
// Setup variables
transmissionKey := r.Context().Value("transmissionKey").(string)
// Get all credit cards from db
schema := r.Context().Value("schema").(string)
creditCardList, err = s.CreditCards().All(schema)
if err != nil {
RespondWithError(w, http.StatusNotFound, err.Error())
return
}
// Decrypt server side encrypted fields
for i := range creditCardList {
uCreditCard, err := app.DecryptModel(&creditCardList[i])
if err != nil {
RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}
creditCardList[i] = *uCreditCard.(*model.CreditCard)
}
RespondWithEncJSON(w, http.StatusOK, transmissionKey, creditCardList)
}
}
// FindCreditCardByID finds a credit cart by id
func FindCreditCardByID(s db.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Setup variables
transmissionKey := r.Context().Value("transmissionKey").(string)
// Check if id is integer
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
RespondWithError(w, http.StatusBadRequest, err.Error())
return
}
// Find credit card by id from db
schema := r.Context().Value("schema").(string)
creditCard, err := s.CreditCards().FindByID(uint(id), schema)
if err != nil {
RespondWithError(w, http.StatusNotFound, err.Error())
return
}
// Decrypt server side encrypted fields
uCreditCard, err := app.DecryptModel(creditCard)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}
// Create DTO
creditCardDTO := model.ToCreditCardDTO(uCreditCard.(*model.CreditCard))
RespondWithEncJSON(w, http.StatusOK, transmissionKey, creditCardDTO)
}
}
// CreateCreditCard creates a credit cart
func CreateCreditCard(s db.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Setup variables
env := viper.GetString("server.env")
transmissionKey := r.Context().Value("transmissionKey").(string)
// Update request body according to env.
// If env is dev, then do nothing
// If env is prod, then decrypt payload with transmission key
if err := ToBody(r, env, transmissionKey); err != nil {
RespondWithError(w, http.StatusBadRequest, InvalidRequestPayload)
return
}
defer r.Body.Close()
// Unmarshal request body to creditCardDTO
var creditCardDTO model.CreditCardDTO
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&creditCardDTO); err != nil {
RespondWithError(w, http.StatusBadRequest, "Invalid resquest payload")
return
}
defer r.Body.Close()
// Add new credit card to db
schema := r.Context().Value("schema").(string)
createdCreditCard, err := app.CreateCreditCard(s, &creditCardDTO, schema)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}
// Decrypt server side encrypted fields
decCreditCard, err := app.DecryptModel(createdCreditCard)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}
// Create DTO
createdCreditCardDTO := model.ToCreditCardDTO(decCreditCard.(*model.CreditCard))
RespondWithEncJSON(w, http.StatusOK, transmissionKey, createdCreditCardDTO)
}
}
// UpdateCreditCard updates a credit cart
func UpdateCreditCard(s db.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Setup variables
env := viper.GetString("server.env")
transmissionKey := r.Context().Value("transmissionKey").(string)
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
RespondWithError(w, http.StatusBadRequest, err.Error())
return
}
if err := ToBody(r, env, transmissionKey); err != nil {
RespondWithError(w, http.StatusBadRequest, InvalidRequestPayload)
return
}
defer r.Body.Close()
// Unmarshal request body to creditCardDTO
var creditCardDTO model.CreditCardDTO
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&creditCardDTO); err != nil {
RespondWithError(w, http.StatusBadRequest, "Invalid resquest payload")
return
}
defer r.Body.Close()
// Find credit card defined by id
schema := r.Context().Value("schema").(string)
creditCard, err := s.CreditCards().FindByID(uint(id), schema)
if err != nil {
RespondWithError(w, http.StatusNotFound, err.Error())
return
}
// Update credit card
updatedCreditCard, err := app.UpdateCreditCard(s, creditCard, &creditCardDTO, schema)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}
// Decrypt server side encrypted fields
decCreditCard, err := app.DecryptModel(updatedCreditCard)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}
// Create DTO
updatedCreditCardDTO := model.ToCreditCardDTO(decCreditCard.(*model.CreditCard))
RespondWithEncJSON(w, http.StatusOK, transmissionKey, updatedCreditCardDTO)
}
}
// BulkUpdateCreditCards updates creditCards in payload
func BulkUpdateCreditCards(s db.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var creditCardList []model.CreditCardDTO
// Setup variables
env := viper.GetString("server.env")
transmissionKey := r.Context().Value("transmissionKey").(string)
if err := ToBody(r, env, transmissionKey); err != nil {
RespondWithError(w, http.StatusBadRequest, InvalidRequestPayload)
return
}
defer r.Body.Close()
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&creditCardList); err != nil {
RespondWithError(w, http.StatusInternalServerError, err.Error())
}
defer r.Body.Close()
for _, creditCardDTO := range creditCardList {
// Find creditCard defined by id
schema := r.Context().Value("schema").(string)
creditCard, err := s.CreditCards().FindByID(creditCardDTO.ID, schema)
if err != nil {
RespondWithError(w, http.StatusNotFound, err.Error())
return
}
// Update creditCard
_, err = app.UpdateCreditCard(s, creditCard, &creditCardDTO, schema)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}
}
response := model.Response{
Code: http.StatusOK,
Status: "Success",
Message: "Bulk update completed successfully!",
}
RespondWithJSON(w, http.StatusOK, response)
}
}
// DeleteCreditCard deletes a credit cart
func DeleteCreditCard(s db.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
RespondWithError(w, http.StatusBadRequest, err.Error())
return
}
schema := r.Context().Value("schema").(string)
creditCard, err := s.CreditCards().FindByID(uint(id), schema)
if err != nil {
RespondWithError(w, http.StatusNotFound, err.Error())
return
}
err = s.CreditCards().Delete(creditCard.ID, schema)
if err != nil {
RespondWithError(w, http.StatusNotFound, err.Error())
return
}
response := model.Response{
Code: http.StatusOK,
Status: Success,
Message: CreditCardDeleted,
}
RespondWithJSON(w, http.StatusOK, response)
}
}