From 9d1ea871285cef65c4ab4a8d0cd3b76b2b0d1b49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sun, 19 Nov 2023 15:19:47 +0100 Subject: [PATCH] members bulk delete. (#59) --- internal/api/members/bluk.go | 27 +++++++++++++++++++++++++++ internal/api/members/handler.go | 1 + openapi.yaml | 28 ++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/internal/api/members/bluk.go b/internal/api/members/bluk.go index a7a4552e..ead74b88 100644 --- a/internal/api/members/bluk.go +++ b/internal/api/members/bluk.go @@ -55,3 +55,30 @@ func (h *MembersHandler) membersBulkUpdate(w http.ResponseWriter, r *http.Reques return utils.HttpSuccess(w) } + +type MemberBulkDeletePayload struct { + IDs []string `json:"ids"` +} + +func (h *MembersHandler) membersBulkDelete(w http.ResponseWriter, r *http.Request) error { + bytes, err := io.ReadAll(r.Body) + if err != nil { + return utils.HttpBadRequest("unable to read post body").WithInternalErr(err) + } + + data := &MemberBulkDeletePayload{} + if err := json.Unmarshal(bytes, &data); err != nil { + return utils.HttpBadRequest("unable to unmarshal payload").WithInternalErr(err) + } + + for _, memberId := range data.IDs { + if err := h.members.Delete(memberId); err != nil { + return utils.HttpInternalServerError(). + WithInternalErr(err). + WithInternalMsg("unable to delete member"). + Msgf("failed to delete member %s", memberId) + } + } + + return utils.HttpSuccess(w) +} diff --git a/internal/api/members/handler.go b/internal/api/members/handler.go index 45d022a5..79d469f5 100644 --- a/internal/api/members/handler.go +++ b/internal/api/members/handler.go @@ -47,6 +47,7 @@ func (h *MembersHandler) Route(r types.Router) { func (h *MembersHandler) RouteBulk(r types.Router) { r.With(auth.AdminsOnly).Group(func(r types.Router) { r.Post("/update", h.membersBulkUpdate) + r.Post("/delete", h.membersBulkDelete) }) } diff --git a/openapi.yaml b/openapi.yaml index 6b6d37de..2fa9e787 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -929,6 +929,27 @@ paths: schema: $ref: '#/components/schemas/MemberBulkUpdate' required: true + /api/members_bulk/delete: + post: + tags: + - members + summary: bulk delete members + operationId: membersBulkDelete + responses: + '204': + description: OK + '401': + $ref: '#/components/responses/Unauthorized' + '403': + $ref: '#/components/responses/Forbidden' + '404': + $ref: '#/components/responses/NotFound' + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/MemberBulkDelete' + required: true components: securitySchemes: @@ -1186,6 +1207,13 @@ components: profile: $ref: '#/components/schemas/MemberProfile' + MemberBulkDelete: + properties: + ids: + type: array + items: + type: string + security: - BearerAuth: [] - CookieAuth: []