|
8 | 8 | "fmt" |
9 | 9 | "os" |
10 | 10 |
|
| 11 | + "github.com/google/uuid" |
| 12 | + |
11 | 13 | "github.com/NdoleStudio/httpsms/pkg/repositories" |
12 | 14 | "github.com/NdoleStudio/httpsms/pkg/requests" |
13 | 15 | "github.com/NdoleStudio/httpsms/pkg/services" |
@@ -47,24 +49,148 @@ func (h *DiscordHandler) RegisterRoutes(app *fiber.App, authMiddleware fiber.Han |
47 | 49 | router := app.Group("discord") |
48 | 50 | router.Post("/event", h.computeRoute(middlewares, h.Event)...) |
49 | 51 |
|
50 | | - authRouter := app.Group("v1/discord") |
51 | | - authRouter.Post("/", h.computeRoute(append(middlewares, authMiddleware), h.Event)...) |
| 52 | + authRouter := app.Group("v1/discord-integrations") |
| 53 | + authRouter.Post("/", h.computeRoute(append(middlewares, authMiddleware), h.Store)...) |
| 54 | + authRouter.Get("/", h.computeRoute(append(middlewares, authMiddleware), h.Index)...) |
| 55 | + authRouter.Delete("/:discordID", h.computeRoute(append(middlewares, authMiddleware), h.Delete)...) |
| 56 | + authRouter.Put("/:discordID", h.computeRoute(append(middlewares, authMiddleware), h.Update)...) |
| 57 | +} |
| 58 | + |
| 59 | +// Index returns the discord integrations of a user |
| 60 | +// @Summary Get discord integrations of a user |
| 61 | +// @Description Get the discord integrations of a user |
| 62 | +// @Security ApiKeyAuth |
| 63 | +// @Tags DiscordIntegration |
| 64 | +// @Accept json |
| 65 | +// @Produce json |
| 66 | +// @Param skip query int false "number of discord integrations to skip" minimum(0) |
| 67 | +// @Param query query string false "filter discord integrations containing query" |
| 68 | +// @Param limit query int false "number of discord integrations to return" minimum(1) maximum(20) |
| 69 | +// @Success 200 {object} responses.DiscordsResponse |
| 70 | +// @Failure 400 {object} responses.BadRequest |
| 71 | +// @Failure 401 {object} responses.Unauthorized |
| 72 | +// @Failure 422 {object} responses.UnprocessableEntity |
| 73 | +// @Failure 500 {object} responses.InternalServerError |
| 74 | +// @Router /discord-integrations [get] |
| 75 | +func (h *DiscordHandler) Index(c *fiber.Ctx) error { |
| 76 | + ctx, span, ctxLogger := h.tracer.StartFromFiberCtxWithLogger(c, h.logger) |
| 77 | + defer span.End() |
| 78 | + |
| 79 | + var request requests.DiscordIndex |
| 80 | + if err := c.QueryParser(&request); err != nil { |
| 81 | + msg := fmt.Sprintf("cannot marshall URL [%s] into %T", c.OriginalURL(), request) |
| 82 | + ctxLogger.Warn(stacktrace.Propagate(err, msg)) |
| 83 | + return h.responseBadRequest(c, err) |
| 84 | + } |
| 85 | + |
| 86 | + if errors := h.validator.ValidateIndex(ctx, request.Sanitize()); len(errors) != 0 { |
| 87 | + msg := fmt.Sprintf("validation errors [%s], while fetching discord integrations [%+#v]", spew.Sdump(errors), request) |
| 88 | + ctxLogger.Warn(stacktrace.NewError(msg)) |
| 89 | + return h.responseUnprocessableEntity(c, errors, "validation errors while fetching discord integrations") |
| 90 | + } |
| 91 | + |
| 92 | + discordIntegrations, err := h.service.Index(ctx, h.userIDFomContext(c), request.ToIndexParams()) |
| 93 | + if err != nil { |
| 94 | + msg := fmt.Sprintf("cannot get discord integrations with params [%+#v]", request) |
| 95 | + ctxLogger.Error(stacktrace.Propagate(err, msg)) |
| 96 | + return h.responseInternalServerError(c) |
| 97 | + } |
| 98 | + |
| 99 | + return h.responseOK(c, fmt.Sprintf("fetched %d discord %s", len(discordIntegrations), h.pluralize("integration", len(discordIntegrations))), discordIntegrations) |
52 | 100 | } |
53 | 101 |
|
54 | | -// Store a webhook |
55 | | -// @Summary Store a webhook |
56 | | -// @Description Store a webhook for the authenticated user |
| 102 | +// Delete a discord integration |
| 103 | +// @Summary Delete discord integration |
| 104 | +// @Description Delete a discord integration for a user |
57 | 105 | // @Security ApiKeyAuth |
58 | 106 | // @Tags Webhooks |
59 | 107 | // @Accept json |
60 | 108 | // @Produce json |
61 | | -// @Param payload body requests.WebhookStore true "Payload of the webhook request" |
62 | | -// @Success 200 {object} responses.WebhookResponse |
| 109 | +// @Param discordID path string true "ID of the discord integration" default(32343a19-da5e-4b1b-a767-3298a73703ca) |
| 110 | +// @Success 204 {object} responses.NoContent |
| 111 | +// @Failure 400 {object} responses.BadRequest |
| 112 | +// @Failure 401 {object} responses.Unauthorized |
| 113 | +// @Failure 422 {object} responses.UnprocessableEntity |
| 114 | +// @Failure 500 {object} responses.InternalServerError |
| 115 | +// @Router /discord-integrations/{discordID} [delete] |
| 116 | +func (h *DiscordHandler) Delete(c *fiber.Ctx) error { |
| 117 | + ctx, span, ctxLogger := h.tracer.StartFromFiberCtxWithLogger(c, h.logger) |
| 118 | + defer span.End() |
| 119 | + |
| 120 | + discordID := c.Params("discordID") |
| 121 | + if errors := h.validator.ValidateUUID(ctx, discordID, "discordID"); len(errors) != 0 { |
| 122 | + msg := fmt.Sprintf("validation errors [%s], while deleting discord integration with ID [%s]", spew.Sdump(errors), discordID) |
| 123 | + ctxLogger.Warn(stacktrace.NewError(msg)) |
| 124 | + return h.responseUnprocessableEntity(c, errors, "validation errors while deleting discord integration") |
| 125 | + } |
| 126 | + |
| 127 | + err := h.service.Delete(ctx, h.userIDFomContext(c), uuid.MustParse(discordID)) |
| 128 | + if err != nil { |
| 129 | + msg := fmt.Sprintf("cannot delete discord integration with ID [%+#v]", discordID) |
| 130 | + ctxLogger.Error(stacktrace.Propagate(err, msg)) |
| 131 | + return h.responseInternalServerError(c) |
| 132 | + } |
| 133 | + |
| 134 | + return h.responseOK(c, "discord integration deleted successfully", nil) |
| 135 | +} |
| 136 | + |
| 137 | +// Update an entities.Discord |
| 138 | +// @Summary Update a discord integration |
| 139 | +// @Description Update a discord integration for the currently authenticated user |
| 140 | +// @Security ApiKeyAuth |
| 141 | +// @Tags DiscordIntegration |
| 142 | +// @Accept json |
| 143 | +// @Produce json |
| 144 | +// @Param discordID path string true "ID of the discord integration" default(32343a19-da5e-4b1b-a767-3298a73703ca) |
| 145 | +// @Param payload body requests.DiscordUpdate true "Payload of discord integration to update" |
| 146 | +// @Success 200 {object} responses.DiscordResponse |
| 147 | +// @Failure 400 {object} responses.BadRequest |
| 148 | +// @Failure 401 {object} responses.Unauthorized |
| 149 | +// @Failure 422 {object} responses.UnprocessableEntity |
| 150 | +// @Failure 500 {object} responses.InternalServerError |
| 151 | +// @Router /discord-integrations/{discordID} [put] |
| 152 | +func (h *DiscordHandler) Update(c *fiber.Ctx) error { |
| 153 | + ctx, span, ctxLogger := h.tracer.StartFromFiberCtxWithLogger(c, h.logger) |
| 154 | + defer span.End() |
| 155 | + |
| 156 | + var request requests.DiscordUpdate |
| 157 | + if err := c.BodyParser(&request); err != nil { |
| 158 | + msg := fmt.Sprintf("cannot marshall params [%s] into [%T]", c.Body(), request) |
| 159 | + ctxLogger.Warn(stacktrace.Propagate(err, msg)) |
| 160 | + return h.responseBadRequest(c, err) |
| 161 | + } |
| 162 | + |
| 163 | + request.DiscordID = c.Params("discordID") |
| 164 | + if errors := h.validator.ValidateUpdate(ctx, request.Sanitize()); len(errors) != 0 { |
| 165 | + msg := fmt.Sprintf("validation errors [%s], while updating user [%+#v]", spew.Sdump(errors), request) |
| 166 | + ctxLogger.Warn(stacktrace.NewError(msg)) |
| 167 | + return h.responseUnprocessableEntity(c, errors, "validation errors while updating discord integration") |
| 168 | + } |
| 169 | + |
| 170 | + user, err := h.service.Update(ctx, request.ToUpdateParams(h.userFromContext(c))) |
| 171 | + if err != nil { |
| 172 | + msg := fmt.Sprintf("cannot update discord integration with params [%+#v]", request) |
| 173 | + ctxLogger.Error(stacktrace.Propagate(err, msg)) |
| 174 | + return h.responseInternalServerError(c) |
| 175 | + } |
| 176 | + |
| 177 | + return h.responseOK(c, "discord integration updated successfully", user) |
| 178 | +} |
| 179 | + |
| 180 | +// Store an entities.Discord |
| 181 | +// @Summary Store discord integration |
| 182 | +// @Description Store a discord integration for the authenticated user |
| 183 | +// @Security ApiKeyAuth |
| 184 | +// @Tags DiscordIntegration |
| 185 | +// @Accept json |
| 186 | +// @Produce json |
| 187 | +// @Param payload body requests.DiscordStore true "Payload of the discord integration request" |
| 188 | +// @Success 201 {object} responses.DiscordResponse |
63 | 189 | // @Failure 400 {object} responses.BadRequest |
64 | 190 | // @Failure 401 {object} responses.Unauthorized |
65 | 191 | // @Failure 422 {object} responses.UnprocessableEntity |
66 | 192 | // @Failure 500 {object} responses.InternalServerError |
67 | | -// @Router /webhooks [post] |
| 193 | +// @Router /discord-integrations [post] |
68 | 194 | func (h *DiscordHandler) Store(c *fiber.Ctx) error { |
69 | 195 | ctx, span := h.tracer.StartFromFiberCtx(c) |
70 | 196 | defer span.End() |
|
0 commit comments