-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparameter_route.go
161 lines (150 loc) · 4.09 KB
/
parameter_route.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
package api
import (
"context"
"net/http"
"strconv"
"github.com/pressly/chi"
chiRender "github.com/pressly/chi/render"
"github.com/titouanfreville/popcubeapi/datastores"
"github.com/titouanfreville/popcubeapi/models"
)
const (
oldParameterKey key = "oldParameter"
)
func initParameterRoute(router chi.Router) {
router.Route("/parameter", func(r chi.Router) {
r.Use(tokenAuth.Verifier)
r.Use(Authenticator)
// swagger:route GET /parameter Parameters getAllParameter
//
// Get parameters
//
// This will get all the parameters available in the organisation.
//
// Responses:
// 200: parameterObjectSuccess
// 503: databaseError
// default: genericError
r.Get("/", getAllParameter)
// swagger:route POST /parameter Parameters newParameter
//
// New parameter
//
// This will create an parameter for organisation parameters library.
//
// Responses:
// 201: parameterObjectSuccess
// 422: wrongEntity
// 503: databaseError
// default: genericError
r.Post("/", newParameter)
// swagger:route GET /parameter/all Parameters getAllParameter1
//
// Get parameters
//
// This will get all the parameters available in the organisation.
//
// Responses:
// 200: parameterObjectSuccess
// 503: databaseError
// default: genericError
r.Get("/all", getAllParameter)
// swagger:route POST /parameter/new Parameters newParameter1
//
// New parameter
//
// This will create an parameter for organisation parameters library.
//
// Responses:
// 201: parameterObjectSuccess
// 422: wrongEntity
// 503: databaseError
// default: genericError
r.Post("/new", newParameter)
r.Route("/:parameterID", func(r chi.Router) {
r.Use(parameterContext)
// swagger:route PUT /parameter/{parameterID} Parameters updateParameter
//
// Update parameter
//
// This will return the new parameter object
//
// Responses:
// 200: avatarObjectSuccess
// 422: wrongEntity
// 503: databaseError
// default: genericError
r.Put("/update", updateParameter)
})
})
}
func parameterContext(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := strconv.ParseUint(chi.URLParam(r, "parameterID"), 10, 64)
parameter := models.Parameter{}
if err == nil {
parameter = datastores.Store().Parameter().Get(dbStore.db)
}
ctx := context.WithValue(r.Context(), oldParameterKey, parameter)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func getAllParameter(w http.ResponseWriter, r *http.Request) {
store := datastores.Store()
db := dbStore.db
if err := db.DB().Ping(); err == nil {
result := store.Parameter().Get(db)
render.JSON(w, 200, result)
} else {
render.JSON(w, error503.StatusCode, error503)
}
}
func newParameter(w http.ResponseWriter, r *http.Request) {
var data struct {
Parameter *models.Parameter
OmitID interface{} `json:"id,omitempty"`
}
store := datastores.Store()
db := dbStore.db
request := r.Body
err := chiRender.Bind(request, &data)
if err != nil || data.Parameter == nil {
render.JSON(w, 500, err)
} else {
if err := db.DB().Ping(); err == nil {
err := store.Parameter().Save(data.Parameter, db)
if err == nil {
render.JSON(w, 200, data.Parameter)
} else {
render.JSON(w, err.StatusCode, err)
}
} else {
render.JSON(w, error503.StatusCode, error503)
}
}
}
func updateParameter(w http.ResponseWriter, r *http.Request) {
var data struct {
Parameter *models.Parameter
OmitID interface{} `json:"id,omitempty"`
}
store := datastores.Store()
db := dbStore.db
request := r.Body
err := chiRender.Bind(request, &data)
parameter := r.Context().Value(oldParameterKey).(models.Parameter)
if err != nil || data.Parameter == nil {
render.JSON(w, error422.StatusCode, error422)
} else {
if err := db.DB().Ping(); err == nil {
err := store.Parameter().Update(¶meter, data.Parameter, db)
if err == nil {
render.JSON(w, 200, parameter)
} else {
render.JSON(w, err.StatusCode, err)
}
} else {
render.JSON(w, error503.StatusCode, error503)
}
}
}