From 86f1284e1253420d4efd27b2791614a74908d352 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Mon, 20 Nov 2023 16:56:44 -0500 Subject: [PATCH] feat: pave V1DocumentDELETE --- apihandler/document.go | 45 +++++++++++++++++++++++++++-------------- apirequest/document.go | 40 ++++++++++++++++++++++++++++++++++++ apiresponse/document.go | 8 ++++++++ cmd/server.go | 6 +++++- 4 files changed, 83 insertions(+), 16 deletions(-) diff --git a/apihandler/document.go b/apihandler/document.go index 032057d..8239474 100644 --- a/apihandler/document.go +++ b/apihandler/document.go @@ -141,12 +141,15 @@ func (h *V1Handler) V1DocumentPUT(c echo.Context) error { // V1DocumentDELETE permet de supprimer un object func (h *V1Handler) V1DocumentDELETE(c echo.Context) error { - bucket := c.Param("bucket") - document := c.Param("document") + var request apirequest.V1DocumentDELETE + var response apiresponse.V1DocumentDELETE + + request.Params.Bucket = c.Param("bucket") + request.Params.Document = c.Param("document") allowed := false for bucket_allowed := range h.Config.Server.Documents.Buckets { - if bucket == bucket_allowed { + if request.Params.Bucket == bucket_allowed { allowed = true } } @@ -155,11 +158,18 @@ func (h *V1Handler) V1DocumentDELETE(c echo.Context) error { return c.JSON(apiresponse.NotFoundResponse()) } + if !request.Complete() { + response.Message = "Incomplete V1DocumentDELETE request received" + response.StatusCode = http.StatusBadRequest + + return c.JSON(response.StatusCode, response) + } + ctx, cancel := context.WithCancel(context.Background()) defer cancel() - bucket_exists, err := h.MediaClient.MinioClient.BucketExists(ctx, bucket) + bucket_exists, err := h.MediaClient.MinioClient.BucketExists(ctx, request.Params.Bucket) if err != nil { return c.JSON(http.StatusInternalServerError, "Error during minio#BucketExists") } @@ -168,29 +178,34 @@ func (h *V1Handler) V1DocumentDELETE(c echo.Context) error { return c.JSON(apiresponse.NotFoundResponse()) } - document_info, err := h.MediaClient.MinioClient.StatObject(ctx, bucket, document, minio.StatObjectOptions{}) + document_info, err := h.MediaClient.MinioClient.StatObject(ctx, request.Params.Bucket, request.Params.Document, minio.StatObjectOptions{}) if err != nil { if err.Error() == "The specified key does not exist." { return c.JSON(apiresponse.NotFoundResponse()) } - return c.JSON(http.StatusInternalServerError, map[string]interface{}{ - "message": "Error during minio#StatObject", - }) + //response.Error = err.Error() + response.Message = "Error during minio#StatObject" + response.StatusCode = http.StatusInternalServerError + + return c.JSON(response.StatusCode, response) } //TODO Add error validation _ = document_info - err = h.MediaClient.MinioClient.RemoveObject(ctx, bucket, document, minio.RemoveObjectOptions{}) + err = h.MediaClient.MinioClient.RemoveObject(ctx, request.Params.Bucket, request.Params.Document, minio.RemoveObjectOptions{}) if err != nil { - return c.JSON(http.StatusInternalServerError, map[string]string{ - "message": "Error during minio#RemoveObject", - }) + //response.Error = err.Error() + response.Message = "Error during minio#RemoveObject" + response.StatusCode = http.StatusInternalServerError + + return c.JSON(response.StatusCode, response) } - return c.JSON(http.StatusOK, map[string]string{ - "message": "Document deleted", - }) + response.Message = "Document deleted" + response.StatusCode = http.StatusOK + + return c.JSON(response.StatusCode, response) } diff --git a/apirequest/document.go b/apirequest/document.go index 5c9ca67..c5c1f9a 100644 --- a/apirequest/document.go +++ b/apirequest/document.go @@ -56,3 +56,43 @@ func (request V1DocumentPOST) Request(v *voki.Voki) (response apiresponse.V1Docu return response, v.UnmarshalIfComplete(http.MethodPost, fmt.Sprintf("/v1/bucket/%s", request.Data.Bucket), &buf, true, &response) } + +var _ request.Requester[apiresponse.V1DocumentDELETE] = V1DocumentDELETE{} + +type V1DocumentDELETE struct { + Params struct { + Bucket string + Document string + } +} + +func NewV1DocumentDELETE(bucket, document string) (request V1DocumentDELETE, err error) { + if bucket == "" { + err = fmt.Errorf("NewV1DocumentDELETE requires non-nil bucket name") + return + } + + request.Params.Bucket = bucket + + if document == "" { + err = fmt.Errorf("NewV1DocumentDELETE requires non-nil document name") + return + } + + request.Params.Document = document + + return +} + +func (request V1DocumentDELETE) Complete() bool { + return request.Params.Bucket != "" && request.Params.Document != "" +} + +func (request V1DocumentDELETE) Request(v *voki.Voki) (response apiresponse.V1DocumentDELETE, err error) { + if !request.Complete() { + err = fmt.Errorf("Incomplete V1DocumentDELETE request") + return + } + + return response, v.UnmarshalIfComplete(http.MethodDelete, fmt.Sprintf("/v1/bucket/%s/%s", request.Params.Bucket, request.Params.Document), nil, true, &response) +} diff --git a/apiresponse/document.go b/apiresponse/document.go index 75c15e3..5865584 100644 --- a/apiresponse/document.go +++ b/apiresponse/document.go @@ -8,3 +8,11 @@ type V1DocumentPOST struct { Size int64 } } + +type V1DocumentDELETE struct { + Response + Data struct { + Bucket string + Document string + } +} diff --git a/cmd/server.go b/cmd/server.go index 11baa50..776adfd 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -252,7 +252,11 @@ func RunServer() { groupV1.PUT("/bucket/:bucket/:document", v1Handler.V1DocumentPUT) - groupV1.DELETE("/bucket/:bucket/:document", v1Handler.V1DocumentDELETE) + if err := pave.EchoRegister[ + apirequest.V1DocumentDELETE, + apiresponse.V1DocumentDELETE](groupV1, &p, "/v1", http.MethodDelete, "/bucket/:bucket/:document", "Delete document in specified bucket", "V1DocumentDELEte", v1Handler.V1DocumentDELETE); err != nil { + log.Fatal(err) + } // HTML Routes client := http.DefaultClient