-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
344 lines (293 loc) · 8.04 KB
/
handlers.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"regexp"
"strings"
"cloud.google.com/go/firestore"
firebase "firebase.google.com/go"
guuid "github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/mitchellh/mapstructure"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
//// ORDER /////
// CreateOrder godoc
// @Summary Create New Order
// @Description To receive a new Order and insert it in firestore
// @Tags order
// @Accept json
// @Param data body Order true "The input todo struct"
// @Produce json
// @Success 200 {object} HTTPResponse
// @Router /order [post]
func createOrder(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
// Get a Firestore client.
ctx := context.Background()
sa := option.WithCredentialsFile("./avab011-firebase-adminsdk-22i2q-02404661c5.json")
app, err := firebase.NewApp(ctx, nil, sa)
if err != nil {
log.Fatalln(err)
}
client, err := app.Firestore(ctx)
if err != nil {
log.Fatalln(err)
}
defer client.Close()
hotelid := guuid.New().String()
roomid := guuid.New().String()
paymentid := guuid.New().String()
docid := guuid.New().String()
//post data
newOrder := new(Order)
newPayment := new(Payment)
resp := new(HTTPResponse)
_ = json.NewDecoder(r.Body).Decode(newOrder)
hotelNameBreakOut := strings.Fields(strings.ToLower(newOrder.HotelName))
hotelNameBreakOut = append(hotelNameBreakOut, strings.ToLower(newOrder.HotelName))
newOrder.HotelNameArray = hotelNameBreakOut
newOrder.HotelID = hotelid
newOrder.RoomID = roomid
newOrder.PaymentID = paymentid
newOrder.HotelNameLC = strings.ToLower(newOrder.HotelName)
_, err = client.Collection("orders").Doc(docid).Set(ctx, newOrder)
if err != nil {
// Handle any errors in an appropriate way, such as returning them.
fmt.Fprintf(w, "An error has occurred: %s", err)
}
newPayment.OrderID = docid
newPayment.Paid = false
_, err = client.Collection("payment").Doc(paymentid).Set(ctx, newPayment)
if err != nil {
// Handle any errors in an appropriate way, such as returning them.
fmt.Fprintf(w, "An error has occurred: %s", err)
}
resp.Success = true
resp.Msg = docid
e, err := json.Marshal(resp)
if err != nil {
fmt.Println(err)
return
}
fmt.Fprintf(w, "%v", string(e))
}
// SearchOrderByHotel godoc
// @Summary Search Order by Hotel Name
// @Description To search orders by hotel name from firestore
// @Tags order
// @Accept json
// @Param data body HotelSearchQuery true "The input search struct"
// @Produce json
// @Success 200 {array} Order
// @Router /order/hotel [post]
func searchOrderByHotel(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
// Get a Firestore client.
ctx := context.Background()
sa := option.WithCredentialsFile("./avab011-firebase-adminsdk-22i2q-02404661c5.json")
app, err := firebase.NewApp(ctx, nil, sa)
if err != nil {
log.Fatalln(err)
}
client, err := app.Firestore(ctx)
if err != nil {
log.Fatalln(err)
}
defer client.Close()
query := new(HotelSearchQuery)
_ = json.NewDecoder(r.Body).Decode(query)
hasSinglePercentage, _ := regexp.MatchString("^%$", query.Expression)
orders := []*Order{}
queryResult := new(firestore.DocumentIterator)
if hasSinglePercentage {
//find all
queryResult = client.Collection("orders").Documents(ctx)
} else {
//find exact
queryResult = client.Collection("orders").Where("hotelnamearray", "array-contains", query.Expression).Documents(ctx)
}
//run search query
order := new(Order)
for {
doc, err := queryResult.Next()
if err == iterator.Done {
break
}
if err != nil {
fmt.Fprintln(w, "ERROR")
}
err = mapstructure.Decode(doc.Data(), order)
if err != nil {
// error
}
orders = append(orders, order)
}
e, err := json.Marshal(orders)
if err != nil {
fmt.Println(err)
return
}
fmt.Fprintf(w, "%v", string(e))
}
// SearchOrderByCustomer godoc
// @Summary Search order by Customer Information (tel, email, name)
// @Description To search orders by customer info (tel, email, name) from firestore
// @Tags order
// @Accept json
// @Param data body HotelSearchQuery true "The input search struct"
// @Produce json
// @Success 200 {array} Order
// @Router /order/customer [post]
func searchOrderByCustomer(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
// Get a Firestore client.
ctx := context.Background()
sa := option.WithCredentialsFile("./avab011-firebase-adminsdk-22i2q-02404661c5.json")
app, err := firebase.NewApp(ctx, nil, sa)
if err != nil {
log.Fatalln(err)
}
client, err := app.Firestore(ctx)
if err != nil {
log.Fatalln(err)
}
defer client.Close()
query := new(HotelSearchQuery)
_ = json.NewDecoder(r.Body).Decode(query)
hasSinglePercentage, _ := regexp.MatchString("^%$", query.Expression)
orders := []*Order{}
queryResult := new(firestore.DocumentIterator)
if hasSinglePercentage {
//find all
queryResult = client.Collection("orders").Documents(ctx)
} else {
//find exact
queryResult = client.Collection("orders").Where(query.Key, "==", query.Expression).Documents(ctx)
}
//run search query
order := new(Order)
for {
doc, err := queryResult.Next()
if err == iterator.Done {
break
}
if err != nil {
fmt.Fprintln(w, "ERROR")
}
err = mapstructure.Decode(doc.Data(), order)
if err != nil {
// error
}
orders = append(orders, order)
}
e, err := json.Marshal(orders)
if err != nil {
fmt.Println(err)
return
}
fmt.Fprintf(w, "%v", string(e))
}
//// PAYMENT /////
// GetPaymentStatus godoc
// @Summary Get Payment Status of an Order
// @Description To get the status of Payment
// @Tags payment
// @Accept json
// @Param orderid path string true "Order ID"
// @Produce json
// @Success 200 {object} Payment
// @Router /paymentstatus/{orderid} [get]
func getPaymentStatus(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
orderID := vars["orderid"]
payment := new(Payment)
// Get a Firestore client.
ctx := context.Background()
sa := option.WithCredentialsFile("./avab011-firebase-adminsdk-22i2q-02404661c5.json")
app, err := firebase.NewApp(ctx, nil, sa)
if err != nil {
log.Fatalln(err)
}
client, err := app.Firestore(ctx)
if err != nil {
log.Fatalln(err)
}
defer client.Close()
if len(orderID) > 35 {
//find payment status
queryResult := client.Collection("payment").Where("orderid", "==", orderID).Documents(ctx)
for {
doc, err := queryResult.Next()
if err == iterator.Done {
break
}
if err != nil {
fmt.Fprintln(w, "ERROR")
}
err = mapstructure.Decode(doc.Data(), payment)
if err != nil {
// error
}
payment.PaymentID = doc.Ref.ID
}
e, err := json.Marshal(payment)
if err != nil {
fmt.Println(err)
return
}
fmt.Fprintf(w, "%v", string(e))
} else {
fmt.Fprintf(w, "")
}
}
// MakePayment godoc
// @Summary Make Payment of an Order
// @Description To pay the order if not yet paid
// @Tags payment
// @Accept json
// @Param paymentid path string true "Payment ID"
// @Produce json
// @Success 200 {object} Order
// @Router /makepayment/{paymentid} [get]
func makePayment(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
paymentID := vars["paymentid"]
resp := new(HTTPResponse)
// Get a Firestore client.
ctx := context.Background()
sa := option.WithCredentialsFile("./avab011-firebase-adminsdk-22i2q-02404661c5.json")
app, err := firebase.NewApp(ctx, nil, sa)
if err != nil {
log.Fatalln(err)
}
client, err := app.Firestore(ctx)
if err != nil {
log.Fatalln(err)
}
defer client.Close()
if len(paymentID) > 35 {
_, err := client.Collection("payment").Doc(paymentID).Set(ctx, map[string]interface{}{
"paid": true,
}, firestore.MergeAll)
if err != nil {
// Handle any errors in an appropriate way, such as returning them.
log.Printf("An error has occurred: %s", err)
}
resp.Success = true
resp.Msg = "Payment Success"
} else {
resp.Success = false
resp.Msg = "Payment Failed"
}
e, err := json.Marshal(resp)
if err != nil {
fmt.Println(err)
return
}
fmt.Fprintf(w, "%v", string(e))
}