@@ -114,18 +114,14 @@ func (p *Webhook[T]) GetSingleResourceWebhooks(ctx *fiber.Ctx) error {
114
114
// PostSingleResourceWebhook creates a new webhook associated to resources
115
115
// (fe. Software, Publishers) and returns any error encountered.
116
116
func (p * Webhook [T ]) PostResourceWebhook (ctx * fiber.Ctx ) error {
117
+ const errMsg = "can't create Webhook"
118
+
117
119
webhookReq := new (common.Webhook )
118
120
119
121
var resource T
120
122
121
- if err := ctx .BodyParser (& webhookReq ); err != nil {
122
- return common .Error (fiber .StatusBadRequest , "can't create Webhook" , "invalid json" )
123
- }
124
-
125
- if err := common .ValidateStruct (* webhookReq ); err != nil {
126
- return common .ErrorWithValidationErrors (
127
- fiber .StatusUnprocessableEntity , "can't create Webhook" , err ,
128
- )
123
+ if err := common .ValidateRequestEntity (ctx , webhookReq , errMsg ); err != nil {
124
+ return err //nolint:wrapcheck
129
125
}
130
126
131
127
webhook := models.Webhook {
@@ -137,7 +133,7 @@ func (p *Webhook[T]) PostResourceWebhook(ctx *fiber.Ctx) error {
137
133
}
138
134
139
135
if err := p .db .Create (& webhook ).Error ; err != nil {
140
- return common .Error (fiber .StatusInternalServerError , "can't create Webhook" , "db error" )
136
+ return common .Error (fiber .StatusInternalServerError , errMsg , "db error" )
141
137
}
142
138
143
139
return ctx .JSON (& webhook )
@@ -146,6 +142,8 @@ func (p *Webhook[T]) PostResourceWebhook(ctx *fiber.Ctx) error {
146
142
// PostResourceWebhook creates a new webhook associated to a resource with the given ID
147
143
// (fe. a specific Software or Publisher) and returns any error encountered.
148
144
func (p * Webhook [T ]) PostSingleResourceWebhook (ctx * fiber.Ctx ) error {
145
+ const errMsg = "can't create Webhook"
146
+
149
147
webhookReq := new (common.Webhook )
150
148
151
149
var resource T
@@ -162,14 +160,8 @@ func (p *Webhook[T]) PostSingleResourceWebhook(ctx *fiber.Ctx) error {
162
160
)
163
161
}
164
162
165
- if err := ctx .BodyParser (& webhookReq ); err != nil {
166
- return common .Error (fiber .StatusBadRequest , "can't create Webhook" , "invalid json" )
167
- }
168
-
169
- if err := common .ValidateStruct (* webhookReq ); err != nil {
170
- return common .ErrorWithValidationErrors (
171
- fiber .StatusUnprocessableEntity , "can't create Webhook" , err ,
172
- )
163
+ if err := common .ValidateRequestEntity (ctx , webhookReq , errMsg ); err != nil {
164
+ return err //nolint:wrapcheck
173
165
}
174
166
175
167
webhook := models.Webhook {
@@ -181,44 +173,40 @@ func (p *Webhook[T]) PostSingleResourceWebhook(ctx *fiber.Ctx) error {
181
173
}
182
174
183
175
if err := p .db .Create (& webhook ).Error ; err != nil {
184
- return common .Error (fiber .StatusInternalServerError , "can't create Webhook" , "db error" )
176
+ return common .Error (fiber .StatusInternalServerError , errMsg , "db error" )
185
177
}
186
178
187
179
return ctx .JSON (& webhook )
188
180
}
189
181
190
182
// PatchWebhook updates the webhook with the given ID.
191
183
func (p * Webhook [T ]) PatchWebhook (ctx * fiber.Ctx ) error {
192
- webhookReq := new (common. Webhook )
184
+ const errMsg = "can't update Webhook"
193
185
194
- if err := ctx .BodyParser (webhookReq ); err != nil {
195
- return common .Error (fiber .StatusBadRequest , "can't update Webhook" , "invalid json" )
196
- }
186
+ webhookReq := new (common.Webhook )
197
187
198
- if err := common .ValidateStruct (* webhookReq ); err != nil {
199
- return common .ErrorWithValidationErrors (
200
- fiber .StatusUnprocessableEntity , "can't update Webhook" , err ,
201
- )
188
+ if err := common .ValidateRequestEntity (ctx , webhookReq , errMsg ); err != nil {
189
+ return err //nolint:wrapcheck
202
190
}
203
191
204
192
webhook := models.Webhook {}
205
193
206
194
if err := p .db .First (& webhook , "id = ?" , ctx .Params ("id" )).Error ; err != nil {
207
195
if errors .Is (err , gorm .ErrRecordNotFound ) {
208
- return common .Error (fiber .StatusNotFound , "can't update Webhook" , "Webhook was not found" )
196
+ return common .Error (fiber .StatusNotFound , errMsg , "Webhook was not found" )
209
197
}
210
198
211
199
return common .Error (
212
200
fiber .StatusInternalServerError ,
213
- "can't update Webhook" ,
201
+ errMsg ,
214
202
fiber .ErrInternalServerError .Message ,
215
203
)
216
204
}
217
205
218
206
webhook .URL = webhookReq .URL
219
207
220
208
if err := p .db .Updates (& webhook ).Error ; err != nil {
221
- return common .Error (fiber .StatusInternalServerError , "can't update Webhook" , "db error" )
209
+ return common .Error (fiber .StatusInternalServerError , errMsg , "db error" )
222
210
}
223
211
224
212
return ctx .JSON (& webhook )
0 commit comments