-
Notifications
You must be signed in to change notification settings - Fork 0
/
place_handlers.go
220 lines (204 loc) · 6.69 KB
/
place_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
package handlers
import (
"encoding/json"
"io"
"log"
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/rs401/letsgorip/api/tokenutils"
"github.com/rs401/letsgorip/pb"
"github.com/rs401/letsgorip/places/models"
)
// PlaceHandlers is the interface defining the place handlerfuncs
type PlaceHandlers interface {
CreatePlace(w http.ResponseWriter, r *http.Request)
GetPlace(w http.ResponseWriter, r *http.Request)
GetPlaces(w http.ResponseWriter, r *http.Request)
UpdatePlace(w http.ResponseWriter, r *http.Request)
DeletePlace(w http.ResponseWriter, r *http.Request)
}
type placeHandlers struct {
placeSvcClient pb.PlaceServiceClient
}
// NewPlaceHandlers takes a pb.PlaceServiceClient and returns a PlaceHandlers
func NewPlaceHandlers(placeSvcClient pb.PlaceServiceClient) PlaceHandlers {
return &placeHandlers{placeSvcClient: placeSvcClient}
}
// CreatePlace is the handlerfunc that makes the service client call to create a
// Place.
func (ph *placeHandlers) CreatePlace(w http.ResponseWriter, r *http.Request) {
var place models.Place
// Decode json place
err := json.NewDecoder(r.Body).Decode(&place)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "json formating decode error"})
return
}
// Create place
response, err := ph.placeSvcClient.CreatePlace(r.Context(), place.ToProtoBuffer())
if err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
return
}
place.Id = response.GetId()
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(place)
}
// GetPlace is the handlerfunc that makes the service client call to retrieve a
// Place.
func (ph *placeHandlers) GetPlace(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
var req pb.PlaceIdRequest
var place models.Place
id, err := strconv.Atoi(vars["id"])
if err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "bad path"})
return
}
req.Id = uint64(id)
pbPlace, err := ph.placeSvcClient.GetPlace(r.Context(), &req)
if err != nil {
log.Printf("Error calling GetPlace: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
return
}
place.FromProtoBuffer(pbPlace)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(place)
}
// GetPlaces is the handlerfunc that makes the service client call to retrieve
// all Places for a specific state.
func (ph *placeHandlers) GetPlaces(w http.ResponseWriter, r *http.Request) {
var gpRequest pb.GetPlacesRequest
var places []*models.Place = make([]*models.Place, 0)
var pbPlaces []*pb.Place = make([]*pb.Place, 0)
done := make(chan bool)
getPlacesStream, err := ph.placeSvcClient.GetPlaces(r.Context(), &gpRequest)
if err != nil {
log.Printf("Error calling GetPlaces: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
return
}
go func() {
for {
response, err := getPlacesStream.Recv()
if err == io.EOF {
done <- true
return
}
if err != nil {
log.Printf("Error receiving places stream")
done <- false
return
}
pbPlaces = append(pbPlaces, response)
}
}()
if <-done {
for _, p := range pbPlaces {
place := &models.Place{}
place.FromProtoBuffer(p)
places = append(places, place)
}
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(places)
}
// UpdatePlace is the handlerfunc that makes the service client call update a
// Place.
func (ph *placeHandlers) UpdatePlace(w http.ResponseWriter, r *http.Request) {
userId := tokenutils.ExtractUserId(r)
if userId == 0 {
log.Printf("UserId could not be extracted from token")
w.WriteHeader(http.StatusUnauthorized)
json.NewEncoder(w).Encode(map[string]string{"error": "unauthorized token error"})
return
}
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "bad path"})
return
}
var place models.Place
err = json.NewDecoder(r.Body).Decode(&place)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "json formating decode error"})
return
}
if place.Id != uint64(id) {
log.Printf("Place id did not match")
w.WriteHeader(http.StatusUnauthorized)
json.NewEncoder(w).Encode(map[string]string{"error": "unauthorized id error"})
return
}
if place.UserId != uint64(userId) {
log.Printf("UserId did not match")
w.WriteHeader(http.StatusUnauthorized)
json.NewEncoder(w).Encode(map[string]string{"error": "unauthorized id error"})
return
}
result, err := ph.placeSvcClient.UpdatePlace(r.Context(), place.ToProtoBuffer())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"error from service client": err.Error()})
return
}
var tmpPlace models.Place
tmpPlace.FromProtoBuffer(result)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(tmpPlace)
}
// DeletePlace is the handlerfunc that makes the service client call to delete a
// Place.
func (ph *placeHandlers) DeletePlace(w http.ResponseWriter, r *http.Request) {
userId := tokenutils.ExtractUserId(r)
if userId == 0 {
log.Printf("UserId could not be extracted from token")
w.WriteHeader(http.StatusUnauthorized)
json.NewEncoder(w).Encode(map[string]string{"error": "unauthorized token error"})
return
}
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "bad path"})
return
}
pbPlace, err := ph.placeSvcClient.GetPlace(r.Context(), &pb.PlaceIdRequest{Id: uint64(id)})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
return
}
if pbPlace.UserId != uint64(userId) {
log.Printf("UserId did not match")
w.WriteHeader(http.StatusUnauthorized)
json.NewEncoder(w).Encode(map[string]string{"error": "unauthorized id error"})
return
}
result, err := ph.placeSvcClient.DeletePlace(r.Context(), &pb.PlaceIdRequest{Id: uint64(id)})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
return
}
if result.Id != uint64(id) {
// Uh oh
log.Printf("Delete place response.Id != path id. Got: %d, Expected: %d\n", result.Id, id)
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"error": "internal server error"})
return
}
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(map[string]string{"delete": "success"})
}