From 83d669b7a59036c55bca32efdc54088d82f066a3 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Tue, 22 Aug 2023 15:24:57 -0400 Subject: [PATCH 1/2] Ajouter SimpleResponse et NotFoundResponse() --- models/responses.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/models/responses.go b/models/responses.go index beb0cca..f1b9b9e 100644 --- a/models/responses.go +++ b/models/responses.go @@ -14,6 +14,20 @@ func (r Response) Respond() Responder { return r } +type SimpleResponse struct { + Message string +} + +func (r SimpleResponse) Respond() Responder { + return r +} + +func NotFoundResponse() SimpleResponse { + return SimpleResponse{ + Message: "Not Found", + } +} + type HandleAdminDocumentsUploadResponse struct { Response Data struct { -- 2.45.2 From ba43df3e313140d4058d5bdb33d5487ed1a9fac4 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Tue, 22 Aug 2023 15:25:42 -0400 Subject: [PATCH 2/2] =?UTF-8?q?Impl=C3=A9menter=20NotFoundResponse()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api_handlers/api_handlers.go | 30 +++++++++--------------------- web_handlers/web_handlers.go | 4 ++-- 2 files changed, 11 insertions(+), 23 deletions(-) diff --git a/api_handlers/api_handlers.go b/api_handlers/api_handlers.go index 9e6aeb3..8dede91 100644 --- a/api_handlers/api_handlers.go +++ b/api_handlers/api_handlers.go @@ -127,13 +127,7 @@ func HandleV1BucketRead(c echo.Context) error { } if !allowed { - /* TODO models.NotFoundResponse - response.StatusCode = http.StatusNotFound - response.Message = "Not Found" - - return c.JSON(response.StatusCode, response) - */ - return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) + return c.JSON(http.StatusNotFound, models.NotFoundResponse()) } ctx, cancel := context.WithCancel(context.Background()) @@ -159,8 +153,7 @@ func HandleV1BucketRead(c echo.Context) error { } if !exists { - // TODO models.NotFoundResponse - return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) + return c.JSON(http.StatusNotFound, models.NotFoundResponse()) } objectCh := mediaClient.MinioClient.ListObjects(ctx, bucket, minio.ListObjectsOptions{}) @@ -210,7 +203,7 @@ func HandleV1DocumentCreate(c echo.Context) error { } if !allowed { - return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) + return c.JSON(http.StatusNotFound, models.NotFoundResponse()) } ctx, cancel := context.WithCancel(context.Background()) @@ -279,7 +272,7 @@ func HandleV1DocumentRead(c echo.Context) error { } if !allowed { - return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) + return c.JSON(http.StatusNotFound, models.NotFoundResponse()) } ctx, cancel := context.WithCancel(context.Background()) @@ -300,7 +293,7 @@ func HandleV1DocumentRead(c echo.Context) error { } if !bucket_exists { - return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) + return c.JSON(http.StatusNotFound, models.NotFoundResponse()) } document_info, err := mediaClient.MinioClient.StatObject(ctx, bucket, document, minio.StatObjectOptions{}) @@ -308,7 +301,7 @@ func HandleV1DocumentRead(c echo.Context) error { if err != nil { if err.Error() == "The specified key does not exist." { - return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) + return c.JSON(http.StatusNotFound, models.NotFoundResponse()) } return c.JSON(http.StatusInternalServerError, map[string]interface{}{ @@ -356,12 +349,7 @@ func HandleV1DocumentDelete(c echo.Context) error { } if !allowed { - /* - return c.JSON(http.StatusBadRequest, map[string]string{ - "message": "Bucket is not allowed in server.documents.buckets", - }) - */ - return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) + return c.JSON(http.StatusNotFound, models.NotFoundResponse()) } ctx, cancel := context.WithCancel(context.Background()) @@ -382,14 +370,14 @@ func HandleV1DocumentDelete(c echo.Context) error { } if !bucket_exists { - return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) + return c.JSON(http.StatusNotFound, models.NotFoundResponse()) } document_info, err := mediaClient.MinioClient.StatObject(ctx, bucket, document, minio.StatObjectOptions{}) if err != nil { if err.Error() == "The specified key does not exist." { - return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) + return c.JSON(http.StatusNotFound, models.NotFoundResponse()) } return c.JSON(http.StatusInternalServerError, map[string]interface{}{ diff --git a/web_handlers/web_handlers.go b/web_handlers/web_handlers.go index 385fac4..e61902e 100644 --- a/web_handlers/web_handlers.go +++ b/web_handlers/web_handlers.go @@ -130,7 +130,7 @@ func HandleFormulaires(c echo.Context) error { func HandlePublicDocumentation(c echo.Context) error { client, err := api.NewApiClientFromViper() if err != nil { - return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) + return c.JSON(http.StatusNotFound, models.NotFoundResponse()) } bucket := c.Param("bucket") @@ -138,7 +138,7 @@ func HandlePublicDocumentation(c echo.Context) error { result, err := client.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s/%s", bucket, document)) if err != nil { - return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) + return c.JSON(http.StatusNotFound, models.NotFoundResponse()) } // Check if result can fit inside a map containing a message -- 2.45.2