-
Notifications
You must be signed in to change notification settings - Fork 2
/
command_settings_handler.go
262 lines (240 loc) · 7.77 KB
/
command_settings_handler.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
package handlers
import (
"errors"
"net/http"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog"
kerr "github.com/krok-o/krok/errors"
"github.com/krok-o/krok/pkg/krok/providers"
"github.com/krok-o/krok/pkg/models"
)
// CommandSettingsHandlerDependencies defines the dependencies for the command settings handler provider.
type CommandSettingsHandlerDependencies struct {
Logger zerolog.Logger
CommandStorer providers.CommandStorer
}
// CommandSettingsHandler is a handler taking care of command settings related api calls.
type CommandSettingsHandler struct {
CommandSettingsHandlerDependencies
}
var _ providers.CommandSettingsHandler = &CommandSettingsHandler{}
// NewCommandSettingsHandler creates a new command settings handler.
func NewCommandSettingsHandler(deps CommandSettingsHandlerDependencies) *CommandSettingsHandler {
return &CommandSettingsHandler{
CommandSettingsHandlerDependencies: deps,
}
}
// Delete deletes a setting.
// swagger:operation DELETE /command/settings/{id} deleteCommandSetting
// Deletes a given command setting.
// ---
// parameters:
// - name: id
// in: path
// description: 'The ID of the command setting to delete'
// required: true
// type: integer
// format: int
// responses:
// '200':
// description: 'OK in case the deletion was successful'
// '400':
// description: 'invalid id'
// schema:
// "$ref": "#/responses/Message"
// '404':
// description: 'command setting not found'
// schema:
// "$ref": "#/responses/Message"
// '500':
// description: 'when the deletion operation failed'
// schema:
// "$ref": "#/responses/Message"
func (ch *CommandSettingsHandler) Delete() echo.HandlerFunc {
return func(c echo.Context) error {
n, err := GetParamAsInt("id", c)
if err != nil {
apiError := kerr.APIError("invalid id", http.StatusBadRequest, nil)
return c.JSON(http.StatusBadRequest, apiError)
}
ctx := c.Request().Context()
if err := ch.CommandStorer.DeleteSetting(ctx, n); err != nil {
if errors.Is(err, kerr.ErrNotFound) {
return c.JSON(http.StatusNotFound, kerr.APIError("command setting not found", http.StatusNotFound, err))
}
ch.Logger.Debug().Err(err).Msg("Command Setting Delete failed.")
return c.JSON(http.StatusInternalServerError, kerr.APIError("failed to delete command setting", http.StatusInternalServerError, err))
}
return c.NoContent(http.StatusOK)
}
}
// List lists command settings.
// swagger:operation POST /command/{id}/settings listCommandSettings
// List settings for a command.
// ---
// produces:
// - application/json
// parameters:
// - name: id
// in: path
// description: 'The ID of the command to list settings for'
// required: true
// type: integer
// format: int
// responses:
// '200':
// schema:
// type: array
// items:
// "$ref": "#/definitions/CommandSetting"
// '400':
// description: 'invalid id'
// schema:
// "$ref": "#/responses/Message"
// '500':
// description: 'failed to list settings'
// schema:
// "$ref": "#/responses/Message"
func (ch *CommandSettingsHandler) List() echo.HandlerFunc {
return func(c echo.Context) error {
n, err := GetParamAsInt("id", c)
if err != nil {
apiError := kerr.APIError("invalid id", http.StatusBadRequest, nil)
return c.JSON(http.StatusBadRequest, apiError)
}
ctx := c.Request().Context()
list, err := ch.CommandStorer.ListSettings(ctx, n)
if err != nil {
ch.Logger.Debug().Err(err).Msg("Command List failed.")
return c.JSON(http.StatusInternalServerError, kerr.APIError("failed to list commands", http.StatusInternalServerError, err))
}
return c.JSON(http.StatusOK, list)
}
}
// Get returns a specific setting.
// swagger:operation GET /command/settings/{id} getCommandSetting
// Get a specific setting.
// ---
// produces:
// - application/json
// parameters:
// - name: id
// in: path
// description: 'The ID of the command setting to retrieve'
// required: true
// type: integer
// format: int
// responses:
// '200':
// schema:
// "$ref": "#/definitions/CommandSetting"
// '400':
// description: 'invalid command id'
// schema:
// "$ref": "#/responses/Message"
// '404':
// description: 'command setting not found'
// schema:
// "$ref": "#/responses/Message"
// '500':
// description: 'failed to get command setting'
// schema:
// "$ref": "#/responses/Message"
func (ch *CommandSettingsHandler) Get() echo.HandlerFunc {
return func(c echo.Context) error {
n, err := GetParamAsInt("id", c)
if err != nil {
apiError := kerr.APIError("invalid id", http.StatusBadRequest, nil)
return c.JSON(http.StatusBadRequest, apiError)
}
ctx := c.Request().Context()
repo, err := ch.CommandStorer.GetSetting(ctx, n)
if err != nil {
if errors.Is(err, kerr.ErrNotFound) {
return c.JSON(http.StatusNotFound, kerr.APIError("command setting not found", http.StatusNotFound, err))
}
apiError := kerr.APIError("failed to get command setting", http.StatusInternalServerError, err)
return c.JSON(http.StatusInternalServerError, apiError)
}
return c.JSON(http.StatusOK, repo)
}
}
// Update updates a setting.
// swagger:operation POST /command/settings/update updateCommandSetting
// Updates a given command setting.
// ---
// consumes:
// - application/json
// parameters:
// - name: setting
// in: body
// required: true
// schema:
// "$ref": "#/definitions/CommandSetting"
// responses:
// '200':
// description: 'successfully updated command setting'
// '400':
// description: 'binding error'
// schema:
// "$ref": "#/responses/Message"
// '500':
// description: 'failed to update the command setting'
// schema:
// "$ref": "#/responses/Message"
func (ch *CommandSettingsHandler) Update() echo.HandlerFunc {
return func(c echo.Context) error {
setting := &models.CommandSetting{}
if err := c.Bind(setting); err != nil {
ch.Logger.Debug().Err(err).Msg("Failed to bind command.")
return c.JSON(http.StatusBadRequest, kerr.APIError("failed to bind command", http.StatusBadRequest, err))
}
ctx := c.Request().Context()
if err := ch.CommandStorer.UpdateSetting(ctx, setting); err != nil {
ch.Logger.Debug().Err(err).Msg("Command setting update failed.")
return c.JSON(http.StatusInternalServerError, kerr.APIError("failed to update command setting", http.StatusInternalServerError, err))
}
return c.NoContent(http.StatusOK)
}
}
// Create creates a command setting.
// swagger:operation POST /command/settings/update updateCommandSetting
// Create a new command setting.
// ---
// consumes:
// - application/json
// produces:
// - application/json
// parameters:
// - name: setting
// in: body
// required: true
// schema:
// "$ref": "#/definitions/CommandSetting"
// responses:
// '200':
// description: 'successfully created command setting'
// '400':
// description: 'binding error'
// schema:
// "$ref": "#/responses/Message"
// '500':
// description: 'failed to create the command setting'
// schema:
// "$ref": "#/responses/Message"
func (ch *CommandSettingsHandler) Create() echo.HandlerFunc {
return func(c echo.Context) error {
setting := &models.CommandSetting{}
if err := c.Bind(setting); err != nil {
ch.Logger.Debug().Err(err).Msg("Failed to bind command setting.")
return c.JSON(http.StatusBadRequest, kerr.APIError("failed to bind command setting", http.StatusBadRequest, err))
}
ctx := c.Request().Context()
setting, err := ch.CommandStorer.CreateSetting(ctx, setting)
if err != nil {
ch.Logger.Debug().Err(err).Msg("Command setting create failed.")
return c.JSON(http.StatusInternalServerError, kerr.APIError("failed to create command setting", http.StatusInternalServerError, err))
}
return c.JSON(http.StatusCreated, setting)
}
}