-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
372 lines (273 loc) · 8.81 KB
/
main.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package main
import (
"net/http"
"github.com/gorilla/mux"
"encoding/json"
"gopkg.in/mgo.v2/bson"
"log"
"github.com/jpillora/overseer"
"./Module"
"./MotoPark"
"io/ioutil"
"time"
"github.com/jpillora/overseer/fetcher"
)
// Represents a movie, we uses bson keyword to tell the mgo driver how to name
// the properties in mongodb document
// Fetch Example
func LocationFisrtParking(w http.ResponseWriter, r *http.Request) {
res, err := MotoPark.FindAll()
if err != nil {
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
if len(res) == 0 {
respondWithJson(w, http.StatusCreated, "Khong co du lieu")
return
}
respondWithJson(w, http.StatusOK, res)
}
func FindingParkingWithCurrentLocation(w http.ResponseWriter, r *http.Request) {
var findingNear MotoPark.FindingNearLocation
err := json.NewDecoder(r.Body).Decode(&findingNear)
if err != nil {
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
result, err := MotoPark.FindNearLocationParking(findingNear)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
if len(result) == 0 {
respondWithJson(w, http.StatusCreated, "Khong co du lieu")
return
}
respondWithJson(w, http.StatusCreated, result)
}
func InsertParking(w http.ResponseWriter, r *http.Request) {
var parking MotoPark.MotoPark
body, errRead := ioutil.ReadAll(r.Body)
if errRead != nil {
respondWithError(w, http.StatusBadRequest, errRead.Error())
return
}
err := json.Unmarshal(body, &parking)
if err != nil {
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
log.Print(&parking)
parking.ID = bson.NewObjectId()
if err := MotoPark.Insert(parking); err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
respondWithJson(w, http.StatusCreated, parking)
}
//login
func Login(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var users Module.UserService
err := json.NewDecoder(r.Body).Decode(&users)
if err != nil {
log.Print(&users)
respondWithError(w, http.StatusNotFound, err.Error())
return
}
usercurrent, errUser := Module.FindById(users.Username)
println(usercurrent.Username)
if errUser != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
hashPwd, errHashPwd := Module.Generate(users.Password)
log.Print(hashPwd)
if errHashPwd != nil {
respondWithError(w, http.StatusInternalServerError, "Lỗi hệ thống vui lòng thử lại sau")
return
}
errCompare := Module.Compare(hashPwd, usercurrent.Password)
if errCompare != nil {
respondWithError(w, http.StatusInternalServerError, "Password không đúng")
return
}
respondWithJson(w, http.StatusCreated, usercurrent)
}
func FindAllUser(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
users, err := Module.FindAll()
println("user finding all")
if err != nil {
respondWithError(w, http.StatusNotFound, err.Error())
return
}
respondWithJson(w, http.StatusCreated, users)
}
type UserRequest struct {
Username string
}
func FindParksByUser(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var userRequest UserRequest
err := decoder.Decode(&userRequest)
if err != nil {
respondWithJson(w, http.StatusBadRequest, err.Error())
panic(err)
}
result, err := MotoPark.FindParksByUserID(userRequest.Username)
if len(result) == 0 {
respondWithJson(w, http.StatusCreated, "Khong co du lieu")
return
}
if err != nil {
respondWithJson(w, http.StatusBadRequest, err.Error())
return
}
respondWithJson(w, http.StatusCreated, result)
}
func InsertUser(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var users Module.User
log.Print(json.NewDecoder(r.Body))
err := json.NewDecoder(r.Body).Decode(&users)
if err != nil {
log.Print(&users)
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
if len(users.Password) == 0 || len(users.Username) == 0 {
respondWithError(w, http.StatusInternalServerError, "Không được để rỗng Username hoặc Password")
return
}
_, errExist := Module.FindById(users.Username)
if errExist == nil {
respondWithError(w, http.StatusInternalServerError, "Username đã tồn tại")
return
}
users.ID = bson.NewObjectId()
if err := Module.Insert(users); err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
respondWithJson(w, http.StatusCreated, users)
}
func UpdateUser(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var users Module.User
err := json.NewDecoder(r.Body).Decode(&users)
if err != nil {
log.Print(&users)
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
if err := Module.Update(users); err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
respondWithJson(w, http.StatusCreated, users)
}
func UpdateParking(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var parking MotoPark.MotoPark
err := json.NewDecoder(r.Body).Decode(&parking)
if err != nil {
log.Print(&parking)
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
if err := MotoPark.Update(parking); err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
respondWithJson(w, http.StatusCreated, parking)
}
func UpdatePriceParking(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var parkingRequest MotoPark.PriceParkRequest
err := json.NewDecoder(r.Body).Decode(&parkingRequest)
if err != nil {
log.Print(&parkingRequest)
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
park, err := MotoPark.UpdateCost(parkingRequest.IdPark, parkingRequest.Cost)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
respondWithJson(w, http.StatusCreated, park)
}
func UpdateSlotParking(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var parkingSlotRequest MotoPark.SlotParkRequest
err := json.NewDecoder(r.Body).Decode(&parkingSlotRequest)
if err != nil {
log.Print(&parkingSlotRequest)
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
park, err := MotoPark.UpdateAvailableSlot(parkingSlotRequest.IdPark, parkingSlotRequest.Slot)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
respondWithJson(w, http.StatusCreated, park)
}
func deleteParking(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var parking Module.UserFindIDRequest
if err := json.NewDecoder(r.Body).Decode(&parking); err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid request payload")
return
}
if err := MotoPark.Delete(parking.ID); err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
respondWithJson(w, 200, "Sucessfully delete")
}
func respondWithError(w http.ResponseWriter, code int, msg string) {
respondWithJson(w, code, map[string]string{"error": msg})
}
func respondWithJson(w http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(response)
}
//prog(state) runs in a child process
func prog(state overseer.State) {
log.Printf("app (%s) listening...", state.ID)
r := mux.NewRouter()
r.HandleFunc("/api/home", LocationFisrtParking).Methods("GET")
r.HandleFunc("/api/users", FindAllUser).Methods("GET")
r.HandleFunc("/api/users/login", Login).Methods("POST")
r.HandleFunc("/api/users/register", InsertUser).Methods("POST")
r.HandleFunc("/api/users", UpdateUser).Methods("PUT")
r.HandleFunc("/api/parkings", InsertParking).Methods("POST")
r.HandleFunc("/api/parkings/updateCost", UpdatePriceParking).Methods("POST")
r.HandleFunc("/api/parkings/updateSlot", UpdateSlotParking).Methods("POST")
r.HandleFunc("/api/parkings", UpdateParking).Methods("PUT")
r.HandleFunc("/api/parkings", deleteParking).Methods("DELETE")
r.HandleFunc("/api/parkings", LocationFisrtParking).Methods("GET")
r.HandleFunc("/api/parkings/users", FindParksByUser).Methods("POST")
r.HandleFunc("/api/parkings/getNearCurrents", FindingParkingWithCurrentLocation).Methods("POST")
r.HandleFunc("/api/uploads", Module.UploadFiles).Methods("POST")
// push notification
r.HandleFunc("/api/pushNotificationSingle", Module.PushNotificationSingle).Methods("POST")
r.HandleFunc("/api/pushNotificationBookingPark", Module.PushNotificationBookParking).Methods("POST")
r.HandleFunc("/api/deleteDeviceToken", Module.DeleteDeviceTokenByUserID).Methods("POST")
r.HandleFunc("/api/registerDeviceToken", Module.RegisterDeviceTokenByUserID).Methods("POST")
http.Serve(state.Listener, r)
}
func main() {
overseer.Run(overseer.Config{
Program: prog,
Address: ":8080",
Fetcher: &fetcher.HTTP{
URL: "http://localhost:4000",
Interval: 1 * time.Second,
},
})
}