MCPcopy Index your code
hub / github.com/NdoleStudio/httpsms / Store

Method Store

api/pkg/handlers/discord_handler.go:204–242  ·  view source on GitHub ↗

Store an entities.Discord @Summary Store discord integration @Description Store a discord integration for the authenticated user @Security ApiKeyAuth @Tags DiscordIntegration @Accept json @Produce json @Param payload body requests.DiscordStore true "Payload o

(c fiber.Ctx)

Source from the content-addressed store, hash-verified

202// @Failure 500 {object} responses.InternalServerError
203// @Router /discord-integrations [post]
204func (h *DiscordHandler) Store(c fiber.Ctx) error {
205 ctx, span := h.tracer.StartFromFiberCtx(c)
206 defer span.End()
207
208 ctxLogger := h.tracer.CtxLogger(h.logger, span)
209
210 var request requests.DiscordStore
211 if err := c.Bind().Body(&request); err != nil {
212 msg := fmt.Sprintf("cannot marshall body [%s] into [%T]", c.Body(), request)
213 ctxLogger.Warn(stacktrace.Propagate(err, msg))
214 return h.responseBadRequest(c, err)
215 }
216
217 if errors := h.validator.ValidateStore(ctx, request.Sanitize()); len(errors) != 0 {
218 msg := fmt.Sprintf("validation errors [%s], while storing discord integration [%+#v]", spew.Sdump(errors), request)
219 ctxLogger.Warn(stacktrace.NewError(msg))
220 return h.responseUnprocessableEntity(c, errors, "validation errors while storing discord integration")
221 }
222
223 discordIntegrations, err := h.service.Index(ctx, h.userIDFomContext(c), repositories.IndexParams{Skip: 0, Limit: 1})
224 if err != nil {
225 ctxLogger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot index discord integrations for user [%s]", h.userIDFomContext(c))))
226 return h.responseInternalServerError(c)
227 }
228
229 if len(discordIntegrations) > 0 {
230 ctxLogger.Warn(stacktrace.NewError(fmt.Sprintf("user with ID [%s] wants to create more than 1 discord integration", h.userIDFomContext(c))))
231 return h.responsePaymentRequired(c, "You can't create more than 1 discord integration contact us to upgrade your account.")
232 }
233
234 discordIntegration, err := h.service.Store(ctx, request.ToStoreParams(h.userFromContext(c)))
235 if err != nil {
236 msg := fmt.Sprintf("cannot store discord integration with params [%+#v]", request)
237 ctxLogger.Error(stacktrace.Propagate(err, msg))
238 return h.responseInternalServerError(c)
239 }
240
241 return h.responseCreated(c, "discord integration created successfully", discordIntegration)
242}
243
244// Event consumes a discord event
245// @Summary Consume a discord event

Callers

nothing calls this directly

Calls 15

SanitizeMethod · 0.95
ToStoreParamsMethod · 0.95
responseBadRequestMethod · 0.80
userIDFomContextMethod · 0.80
userFromContextMethod · 0.80
responseCreatedMethod · 0.80
StartFromFiberCtxMethod · 0.65
CtxLoggerMethod · 0.65
WarnMethod · 0.65

Tested by

no test coverage detected