-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPI.go
134 lines (124 loc) · 3.11 KB
/
API.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/gorilla/mux"
)
func testOrderHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Category: %v\n", vars["category"])
}
type OrderNotif struct {
OrderId string `json:"order_id"`
Address string `json:"address"`
Dish string `json:"dish"`
}
func addOrderHr(w http.ResponseWriter, r *http.Request) {
//get request object
inp := &Order{}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
err = json.Unmarshal(body, inp)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// got order, now first persist it!
orderid, err := AddOrder(inp)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Println("coud not persist order", err)
return
}
//get list of restaurants and push message to them
rests, err := ListRestaurants()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Println("coud not list restaurants", err)
return
}
fmt.Printf("restaurants are: %+v\n", rests)
for _, rst := range rests {
notif := OrderNotif{
OrderId: orderid,
Dish: inp.PizzaName,
Address: inp.Address,
}
notifMsg, _ := json.Marshal(notif)
pushNotif(rst, string(notifMsg))
}
//done!
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "ok")
}
type AcceptReq struct {
RestaurantName string `json:"restaurant_name"`
}
func acceptOrderHr(w http.ResponseWriter, r *http.Request) {
inp := &AcceptReq{}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
err = json.Unmarshal(body, inp)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
id := mux.Vars(r)["orderid"]
err = acceptOrder(id, inp.RestaurantName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Println("coud not update order", err)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "ok")
}
func registerRestaurantHr(w http.ResponseWriter, r *http.Request) {
//get request object
inp := &RestaurantInput{}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Println("error reading request body", err)
return
}
err = json.Unmarshal(body, inp)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Println("error unmarshal request body", err)
return
}
if inp.Subscription.Endpoint == "" {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "restaurant must have subscription information\n")
return
}
// got restaurant, now first persist it!
rst := &Restaurant{
Name: inp.Name,
EndPoint: inp.Subscription.Endpoint,
P256DH: inp.Subscription.Keys.P256dh,
Auth: inp.Subscription.Keys.Auth,
}
err = AddRestaurant(rst)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("error adding restaurant: %s\n\n", err)
return
}
// pushNotif(*rst, "you are added!")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "ok")
}
func finalizeOrder(w http.ResponseWriter, r *http.Request) {
}