/
escalations.go
211 lines (180 loc) · 4.93 KB
/
escalations.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
package routes
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"github.com/klaxn/klaxn-api/pkg/model/escalation"
)
// @BasePath /api/v1
// GetEscalations godoc
//
// @Summary Get all escalations
// @Schemes
// @Description Get all escalations
// @Tags escalations
// @Accept json
// @Produce json
// @Success 200 {array} escalation.Escalation
// @Failure 500 {object} data.Error
// @Router /escalations [get]
func (r *Router) GetEscalations(c *gin.Context) {
_, span := r.tracer.Start(c, "GetEscalations")
defer span.End()
escalations, err := r.db.GetEscalations()
if err != nil {
r.SendErr(c, http.StatusInternalServerError, err, span)
return
}
slice, err := escalation.FromDataSlice(escalations)
if err != nil {
r.SendErr(c, http.StatusInternalServerError, err, span)
return
}
r.SendJsonResponse(c, http.StatusOK, slice, span)
}
// GetEscalation godoc
//
// @Summary Get an escalation
// @Description Get an escalation
// @Tags escalations
// @Accept json
// @Produce json
// @Success 200 {object} escalation.Escalation
// @Failure 400 {object} data.Error
// @Failure 500 {object} data.Error
// @Router /escalations/{id} [get]
// @Param id path int true "id"
func (r *Router) GetEscalation(c *gin.Context) {
_, span := r.tracer.Start(c, "GetEscalation")
defer span.End()
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
r.SendErr(c, http.StatusBadRequest, err, span)
return
}
de, err := r.db.GetEscalation(uint(id))
if err != nil {
r.SendErr(c, http.StatusInternalServerError, err, span)
return
}
e, err := escalation.FromData(de)
if err != nil {
r.SendErr(c, http.StatusInternalServerError, err, span)
return
}
c.JSON(http.StatusOK, e)
}
// CreateEscalation godoc
//
// @Summary Create an escalation
// @Description Create an escalation
// @Tags escalations
// @Accept json
// @Produce json
// @Success 200 {object} escalation.Escalation
// @Failure 400 {object} data.Error
// @Failure 500 {object} data.Error
// @Router /escalations [post]
// @Param escalation body escalation.Escalation true "escalation"
func (r *Router) CreateEscalation(c *gin.Context) {
_, span := r.tracer.Start(c, "CreateEscalation")
defer span.End()
var e escalation.Escalation
if err := c.ShouldBindJSON(&e); err != nil {
r.SendErr(c, http.StatusBadRequest, err, span)
return
}
json, err := e.ToData()
if err != nil {
r.SendErr(c, http.StatusInternalServerError, err, span)
return
}
if err := json.Validate(nil); err != nil {
r.SendErr(c, http.StatusBadRequest, err, span)
return
}
newData, err := r.db.UpdateEscalation(json)
if err != nil {
r.SendErr(c, http.StatusInternalServerError, err, span)
return
}
newEscalation, err := escalation.FromData(newData)
if err != nil {
r.SendErr(c, http.StatusInternalServerError, err, span)
return
}
r.SendJsonResponse(c, http.StatusOK, newEscalation, span)
return
}
// UpdateEscalation godoc
//
// @Summary Update an existing escalation
// @Description Create an existing escalation
// @Tags escalations
// @Accept json
// @Produce json
// @Success 200 {object} escalation.Escalation
// @Failure 400 {object} data.Error
// @Failure 500 {object} data.Error
// @Router /escalations/{id} [put]
// @Param escalation body escalation.Escalation true "escalation"
// @Param id path int true "id"
func (r *Router) UpdateEscalation(c *gin.Context) {
_, span := r.tracer.Start(c, "UpdateEscalation")
defer span.End()
var e escalation.Escalation
if err := c.ShouldBindJSON(&e); err != nil {
r.SendErr(c, http.StatusBadRequest, errors.Wrap(err, "could not parse json"), span)
return
}
json, err := e.ToData()
if err != nil {
r.SendErr(c, http.StatusInternalServerError, err, span)
return
}
if err := json.Validate(nil); err != nil {
r.SendErr(c, http.StatusBadRequest, err, span)
return
}
newData, err := r.db.UpdateEscalation(json)
if err != nil {
r.SendErr(c, http.StatusInternalServerError, err, span)
return
}
updatedEscalation, err := escalation.FromData(newData)
if err != nil {
r.SendErr(c, http.StatusInternalServerError, err, span)
return
}
r.SendJsonResponse(c, http.StatusOK, updatedEscalation, span)
return
}
// DeleteEscalation godoc
//
// @Summary Delete an existing escalation
// @Description Delete an existing escalation
// @Tags escalations
// @Accept json
// @Produce json
// @Success 204
// @Failure 400 {object} data.Error
// @Failure 500 {object} data.Error
// @Router /escalations/{id} [delete]
// @Param id path int true "id"
func (r *Router) DeleteEscalation(c *gin.Context) {
_, span := r.tracer.Start(c, "DeleteEscalation")
defer span.End()
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
r.SendErr(c, http.StatusBadRequest, err, span)
return
}
err = r.db.DeleteEscalation(uint(id))
if err != nil {
r.SendErr(c, http.StatusInternalServerError, err, span)
return
}
r.SendNullResponse(c, http.StatusNoContent, span)
return
}