/
handlers.go
169 lines (148 loc) · 4.55 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
package rest
import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"github.com/martin-helmich/cloudnativego-backend/src/contracts"
"github.com/martin-helmich/cloudnativego-backend/src/lib/msgqueue"
"github.com/martin-helmich/cloudnativego-backend/src/lib/persistence"
"net/http"
"strings"
"time"
)
type eventServiceHandler struct {
dbhandler persistence.DatabaseHandler
eventEmitter msgqueue.EventEmitter
}
func newEventHandler(databasehandler persistence.DatabaseHandler, eventEmitter msgqueue.EventEmitter) *eventServiceHandler {
return &eventServiceHandler{
dbhandler: databasehandler,
eventEmitter: eventEmitter,
}
}
func (eh *eventServiceHandler) findEventHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
criteria, ok := vars["SearchCriteria"]
if !ok {
fmt.Fprint(w, `No search criteria found, you can either search by id via /id/4
to search by name via /name/coldplayconcert`)
return
}
searchkey, ok := vars["search"]
if !ok {
fmt.Fprint(w, `No search keys found, you can either search by id via /id/4
to search by name via /name/coldplayconcert`)
return
}
var event persistence.Event
var err error
switch strings.ToLower(criteria) {
case "name":
event, err = eh.dbhandler.FindEventByName(searchkey)
case "id":
id, err := hex.DecodeString(searchkey)
if nil == err {
event, err = eh.dbhandler.FindEvent(id)
}
}
if err != nil {
w.WriteHeader(404)
fmt.Fprintf(w, "Error occured %s", err)
return
}
w.Header().Set("Content-Type", "application/json;charset=utf8")
json.NewEncoder(w).Encode(&event)
}
func (eh *eventServiceHandler) allEventHandler(w http.ResponseWriter, r *http.Request) {
events, err := eh.dbhandler.FindAllAvailableEvents()
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "Error occured while trying to find all available events %s", err)
return
}
w.Header().Set("Content-Type", "application/json;charset=utf8")
err = json.NewEncoder(w).Encode(&events)
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "Error occured while trying encode events to JSON %s", err)
}
}
func (eh *eventServiceHandler) oneEventHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
eventID, ok := vars["eventID"]
if !ok {
w.WriteHeader(400)
fmt.Fprint(w, "missing route parameter 'eventID'")
return
}
eventIDBytes, _ := hex.DecodeString(eventID)
event, err := eh.dbhandler.FindEvent(eventIDBytes)
if err != nil {
w.WriteHeader(404)
fmt.Fprintf(w, "event with id %s was not found", eventID)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf8")
json.NewEncoder(w).Encode(&event)
}
func (eh *eventServiceHandler) newEventHandler(w http.ResponseWriter, r *http.Request) {
event := persistence.Event{}
err := json.NewDecoder(r.Body).Decode(&event)
if nil != err {
w.WriteHeader(500)
fmt.Fprintf(w, "error occured while decoding event data %s", err)
return
}
id, err := eh.dbhandler.AddEvent(event)
if nil != err {
w.WriteHeader(500)
fmt.Fprintf(w, "error occured while persisting event %s", err)
return
}
msg := contracts.EventCreatedEvent{
ID: hex.EncodeToString(id),
Name: event.Name,
Start: time.Unix(event.StartDate, 0),
End: time.Unix(event.EndDate, 0),
LocationID: string(event.Location.ID),
}
eh.eventEmitter.Emit(&msg)
w.Header().Set("Content-Type", "application/json;charset=utf8")
w.WriteHeader(201)
json.NewEncoder(w).Encode(&event)
}
func (eh *eventServiceHandler) allLocationsHandler(w http.ResponseWriter, r *http.Request) {
locations, err := eh.dbhandler.FindAllLocations()
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "could not load locations: %s", err)
return
}
json.NewEncoder(w).Encode(locations)
}
func (eh *eventServiceHandler) newLocationHandler(w http.ResponseWriter, r *http.Request) {
location := persistence.Location{}
err := json.NewDecoder(r.Body).Decode(&location)
if err != nil {
w.WriteHeader(400)
fmt.Fprintf(w, "request body could not be unserialized to location: %s", err)
return
}
persistedLocation, err := eh.dbhandler.AddLocation(location)
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "could not persist location: %s", err)
}
msg := contracts.LocationCreatedEvent{
ID: string(persistedLocation.ID),
Name: persistedLocation.Name,
Address: persistedLocation.Address,
Country: persistedLocation.Country,
Halls: persistedLocation.Halls,
}
eh.eventEmitter.Emit(&msg)
w.Header().Set("Content-Type", "application/json;charset=utf8")
w.WriteHeader(201)
json.NewEncoder(w).Encode(&persistedLocation)
}