-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcars.go
More file actions
131 lines (107 loc) · 2.94 KB
/
cars.go
File metadata and controls
131 lines (107 loc) · 2.94 KB
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
package handlers
import (
"net/http"
"github.com/gorilla/mux"
"github.com/sarulabs/di"
"github.com/sarulabs/di-example/app/models/garage"
"github.com/sarulabs/di-example/app/models/helpers"
)
// GetCarListHandler is the handler that prints the list of cars.
func GetCarListHandler(w http.ResponseWriter, r *http.Request) {
cars, err := di.Get(r, "car-manager").(*garage.CarManager).GetAll()
if err == nil {
helpers.JSONResponse(w, 200, cars)
return
}
helpers.JSONResponse(w, 500, map[string]interface{}{
"error": "Internal Error",
})
}
// PostCarHandler is the handler that adds a new car.
func PostCarHandler(w http.ResponseWriter, r *http.Request) {
var input *garage.Car
err := helpers.ReadJSONBody(r, &input)
if err != nil {
helpers.JSONResponse(w, 400, map[string]interface{}{
"error": "Could not decode request body.",
})
return
}
car, err := di.Get(r, "car-manager").(*garage.CarManager).Create(input)
if err == nil {
helpers.JSONResponse(w, 200, car)
return
}
switch e := err.(type) {
case *helpers.ErrValidation:
helpers.JSONResponse(w, 400, map[string]interface{}{
"error": e.Error(),
})
default:
helpers.JSONResponse(w, 500, map[string]interface{}{
"error": "Internal Error",
})
}
}
// GetCarHandler is the handler that prints the characteristics of a car.
func GetCarHandler(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["carId"]
car, err := di.Get(r, "car-manager").(*garage.CarManager).Get(id)
if err == nil {
helpers.JSONResponse(w, 200, car)
return
}
switch e := err.(type) {
case *helpers.ErrNotFound:
helpers.JSONResponse(w, 404, map[string]interface{}{
"error": e.Error(),
})
default:
helpers.JSONResponse(w, 500, map[string]interface{}{
"error": "Internal Error",
})
}
}
// PutCarHandler is the handler that updates a car.
func PutCarHandler(w http.ResponseWriter, r *http.Request) {
var input *garage.Car
err := helpers.ReadJSONBody(r, &input)
if err != nil {
helpers.JSONResponse(w, 400, map[string]interface{}{
"error": "Could not decode request body.",
})
return
}
id := mux.Vars(r)["carId"]
car, err := di.Get(r, "car-manager").(*garage.CarManager).Update(id, input)
if err == nil {
helpers.JSONResponse(w, 200, car)
return
}
switch e := err.(type) {
case *helpers.ErrValidation:
helpers.JSONResponse(w, 400, map[string]interface{}{
"error": e.Error(),
})
case *helpers.ErrNotFound:
helpers.JSONResponse(w, 404, map[string]interface{}{
"error": e.Error(),
})
default:
helpers.JSONResponse(w, 500, map[string]interface{}{
"error": "Internal Error",
})
}
}
// DeleteCarHandler is the handler that removes a car from the database.
func DeleteCarHandler(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["carId"]
err := di.Get(r, "car-manager").(*garage.CarManager).Delete(id)
if err == nil {
w.WriteHeader(204)
return
}
helpers.JSONResponse(w, 500, map[string]interface{}{
"error": "Internal Error",
})
}