-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcollection_groups.go
299 lines (281 loc) · 10.1 KB
/
collection_groups.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package handler
import (
"net/http"
"strconv"
"time"
"github.com/USACE/instrumentation-api/api/internal/db"
"github.com/USACE/instrumentation-api/api/internal/dto"
"github.com/USACE/instrumentation-api/api/internal/httperr"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
)
// ListCollectionGroups godoc
//
// @Summary lists instrument groups
// @Tags collection-groups
// @Produce json
// @Param project_id path string true "project uuid" Format(uuid)
// @Success 200 {array} db.CollectionGroup
// @Failure 400 {object} echo.HTTPError
// @Failure 404 {object} echo.HTTPError
// @Failure 500 {object} echo.HTTPError
// @Router /projects/{project_id}/collection_groups [get]
func (h *ApiHandler) ListCollectionGroups(c echo.Context) error {
pID, err := uuid.Parse(c.Param("project_id"))
if err != nil {
return httperr.MalformedID(err)
}
cc, err := h.DBService.CollectionGroupListForProject(c.Request().Context(), pID)
if err != nil {
return httperr.InternalServerError(err)
}
return c.JSON(http.StatusOK, &cc)
}
// GetCollectionGroupDetails godoc
//
// @Summary gets all data needed to render collection group form
// @Tags collection-groups
// @Produce json
// @Param project_id path string true "project uuid" Format(uuid)
// @Param collection_group_id path string true "collection group uuid" Format(uuid)
// @Success 200 {object} db.VCollectionGroupDetails
// @Failure 400 {object} echo.HTTPError
// @Failure 404 {object} echo.HTTPError
// @Failure 500 {object} echo.HTTPError
// @Router /projects/{project_id}/collection_groups/{collection_group_id} [get]
func (h *ApiHandler) GetCollectionGroupDetails(c echo.Context) error {
_, err := uuid.Parse(c.Param("project_id"))
if err != nil {
return httperr.MalformedID(err)
}
cgID, err := uuid.Parse(c.Param("collection_group_id"))
if err != nil {
return httperr.MalformedID(err)
}
d, err := h.DBService.CollectionGroupDetailsGet(c.Request().Context(), cgID)
if err != nil {
return httperr.InternalServerError(err)
}
return c.JSON(http.StatusOK, d)
}
// CreateCollectionGroup godoc
//
// @Summary creates a new collection group
// @Description lists alert configs for a single project optionally filtered by alert_type_id
// @Tags collection-groups
// @Produce json
// @Param project_id path string true "project uuid" Format(uuid)
// @Param collection_group body dto.CollectionGroup true "collection group payload"
// @Param key query string false "api key"
// @Success 200 {array} db.CollectionGroup
// @Failure 400 {object} echo.HTTPError
// @Failure 404 {object} echo.HTTPError
// @Failure 500 {object} echo.HTTPError
// @Router /projects/{project_id}/collection_groups [post]
// @Security Bearer
func (h *ApiHandler) CreateCollectionGroup(c echo.Context) error {
var cg dto.CollectionGroup
// Bind Information Provided
if err := c.Bind(&cg); err != nil {
return httperr.MalformedBody(err)
}
// Project ID from Route Params
pID, err := uuid.Parse(c.Param("project_id"))
if err != nil {
return httperr.MalformedID(err)
}
cg.ProjectID = pID
p := c.Get("profile").(db.VProfile)
cg.CreatedBy, cg.CreatedAt = p.ID, time.Now()
cgNew, err := h.DBService.CollectionGroupCreate(c.Request().Context(), cg)
if err != nil {
return httperr.InternalServerError(err)
}
return c.JSON(http.StatusCreated, []db.CollectionGroup{cgNew})
}
// UpdateCollectionGroup godoc
//
// @Summary updates an existing collection group
// @Tags collection-groups
// @Produce json
// @Param project_id path string true "project uuid" Format(uuid)
// @Param collection_group_id path string true "collection group uuid"
// @Param collection_group body dto.CollectionGroup true "collection group payload"
// @Param key query string false "api key"
// @Success 200 {object} db.CollectionGroup
// @Failure 400 {object} echo.HTTPError
// @Failure 404 {object} echo.HTTPError
// @Failure 500 {object} echo.HTTPError
// @Router /projects/{project_id}/collection_groups/{collection_group_id} [put]
// @Security Bearer
func (h *ApiHandler) UpdateCollectionGroup(c echo.Context) error {
var cg dto.CollectionGroup
if err := c.Bind(&cg); err != nil {
return httperr.MalformedBody(err)
}
pID, err := uuid.Parse(c.Param("project_id"))
if err != nil {
return httperr.MalformedID(err)
}
cg.ProjectID = pID
cgID, err := uuid.Parse(c.Param("collection_group_id"))
if err != nil {
return httperr.MalformedID(err)
}
cg.ID = cgID
p := c.Get("profile").(db.VProfile)
t := time.Now()
cg.UpdatedBy, cg.UpdatedAt = &p.ID, &t
cgUpdated, err := h.DBService.CollectionGroupUpdate(c.Request().Context(), cg)
if err != nil {
return httperr.InternalServerError(err)
}
return c.JSON(http.StatusOK, cgUpdated)
}
// DeleteCollectionGroup godoc
//
// @Summary deletes a collection group using the id of the collection group
// @Tags collection-groups
// @Produce json
// @Param project_id path string true "project uuid" Format(uuid)
// @Param collection_group_id path string true "collection group uuid" Format(uuid)
// @Param key query string false "api key"
// @Success 200 {object} map[string]interface{}
// @Failure 400 {object} echo.HTTPError
// @Failure 404 {object} echo.HTTPError
// @Failure 500 {object} echo.HTTPError
// @Router /projects/{project_id}/collection_groups/{collection_group_id} [delete]
// @Security Bearer
func (h *ApiHandler) DeleteCollectionGroup(c echo.Context) error {
pID, err := uuid.Parse(c.Param("project_id"))
if err != nil {
return httperr.MalformedID(err)
}
cgID, err := uuid.Parse(c.Param("collection_group_id"))
if err != nil {
return httperr.MalformedID(err)
}
if err := h.DBService.CollectionGroupDelete(c.Request().Context(), db.CollectionGroupDeleteParams{
ProjectID: pID,
ID: cgID,
}); err != nil {
return httperr.InternalServerError(err)
}
return c.JSON(http.StatusOK, make(map[string]interface{}))
}
// AddTimeseriesToCollectionGroup godoc
//
// @Summary adds a timeseries to a collection group
// @Tags collection-groups
// @Produce json
// @Param project_id path string true "project uuid" Format(uuid)
// @Param collection_group_id path string true "collection group uuid" Format(uuid)
// @Param timeseries_id path string true "timeseries uuid" Format(uuid)
// @Param key query string false "api key"
// @Success 201 {object} map[string]interface{}
// @Failure 400 {object} echo.HTTPError
// @Failure 404 {object} echo.HTTPError
// @Failure 500 {object} echo.HTTPError
// @Router /projects/{project_id}/collection_groups/{collection_group_id}/timeseries/{timeseries_id} [post]
// @Security Bearer
func (h *ApiHandler) AddTimeseriesToCollectionGroup(c echo.Context) error {
cgID, err := uuid.Parse(c.Param("collection_group_id"))
if err != nil {
return httperr.MalformedID(err)
}
tsID, err := uuid.Parse(c.Param("timeseries_id"))
if err != nil {
return httperr.MalformedID(err)
}
var sortOrder int32
soParam := c.QueryParam("sort_order")
if soParam != "" {
so64, err := strconv.ParseInt(soParam, 10, 32)
if err != nil {
return httperr.BadRequest(err)
}
sortOrder = int32(so64)
}
if err := h.DBService.CollectionGroupTimeseriesCreate(c.Request().Context(), db.CollectionGroupTimeseriesCreateParams{
CollectionGroupID: cgID,
TimeseriesID: tsID,
SortOrder: sortOrder,
}); err != nil {
return httperr.InternalServerError(err)
}
return c.JSON(http.StatusCreated, make(map[string]interface{}))
}
// UpdateTimeseriesCollectionGroupSortOrder godoc
//
// @Summary updates sort order for collection group timesries
// @Tags collection-groups
// @Produce json
// @Param project_id path string true "project uuid" Format(uuid)
// @Param collection_group_id path string true "collection group uuid" Format(uuid)
// @Param timeseries_id path string true "timeseries uuid" Format(uuid)
// @Param key query string false "api key"
// @Success 200 {object} map[string]interface{}
// @Failure 400 {object} echo.HTTPError
// @Failure 404 {object} echo.HTTPError
// @Failure 500 {object} echo.HTTPError
// @Router /projects/{project_id}/collection_groups/{collection_group_id}/timeseries/{timeseries_id} [put]
// @Security Bearer
func (h *ApiHandler) UpdateTimeseriesCollectionGroupSortOrder(c echo.Context) error {
cgID, err := uuid.Parse(c.Param("collection_group_id"))
if err != nil {
return httperr.MalformedID(err)
}
tsID, err := uuid.Parse(c.Param("timeseries_id"))
if err != nil {
return httperr.MalformedID(err)
}
var sortOrder int32
soParam := c.QueryParam("sort_order")
if soParam != "" {
so64, err := strconv.ParseInt(soParam, 10, 32)
if err != nil {
return httperr.BadRequest(err)
}
sortOrder = int32(so64)
}
if err := h.DBService.CollectionGroupTimeseriesUpdateSortOrder(c.Request().Context(), db.CollectionGroupTimeseriesUpdateSortOrderParams{
CollectionGroupID: cgID,
TimeseriesID: tsID,
SortOrder: sortOrder,
}); err != nil {
return httperr.InternalServerError(err)
}
return c.JSON(http.StatusOK, make(map[string]interface{}))
}
// RemoveTimeseriesFromCollectionGroup godoc
//
// @Summary removes a timeseries from a collection group
// @Tags collection-groups
// @Produce json
// @Param project_id path string true "project uuid" Format(uuid)
// @Param collection_group_id path string true "collection group uuid" Format(uuid)
// @Param timeseries_id path string true "timeseries uuid" Format(uuid)
// @Param key query string false "api key"
// @Success 200 {object} map[string]interface{}
// @Failure 400 {object} echo.HTTPError
// @Failure 404 {object} echo.HTTPError
// @Failure 500 {object} echo.HTTPError
// @Router /projects/{project_id}/collection_groups/{collection_group_id}/timeseries/{timeseries_id} [delete]
// @Security Bearer
func (h *ApiHandler) RemoveTimeseriesFromCollectionGroup(c echo.Context) error {
cgID, err := uuid.Parse(c.Param("collection_group_id"))
if err != nil {
return httperr.MalformedID(err)
}
tsID, err := uuid.Parse(c.Param("timeseries_id"))
if err != nil {
return httperr.MalformedID(err)
}
if err := h.DBService.CollectionGroupTimeseriesDelete(c.Request().Context(), db.CollectionGroupTimeseriesDeleteParams{
CollectionGroupID: cgID,
TimeseriesID: tsID,
}); err != nil {
return httperr.InternalServerError(err)
}
return c.JSON(http.StatusOK, make(map[string]interface{}))
}