-
Notifications
You must be signed in to change notification settings - Fork 0
/
health_data_controller.go
55 lines (51 loc) · 1.5 KB
/
health_data_controller.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
package main
import (
"net/http"
"github.com/julienschmidt/httprouter"
)
func addHealthData(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
// Read body
data := ReadPostBody(res, req)
// unmarshal json
hd, err := marshalHealthData(data)
if err != nil {
HttpError(&res, err.Error(), 400, err.Error())
return
}
// save to db
id, err := saveHealthData(hd)
if err != nil {
HttpError(&res, err.Error(), 500, err.Error())
return
}
res.Header().Set("Location", "/api/v0/data/"+id)
res.WriteHeader(http.StatusCreated)
}
func getHealthData(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
healthData, err := findHealthData()
if err != nil {
switch err.(type) {
case NotFoundError:
EmptyResponse(&res, "")
return
default:
InternalServerError(&res, "Internal Server Error")
return
}
}
WriteJSON(res, healthData)
}
func deleteHealthData(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
healthDataId := params.ByName("HealthDataId")
if err := removeHealthData(healthDataId); err != nil {
switch err.(type) {
case NotFoundError:
NotFound(&res, "HealthData with id "+healthDataId+" not found")
return
default:
InternalServerError(&res, "Internal Server Error")
return
}
}
res.WriteHeader(http.StatusNoContent)
}