From 93334871e9cdb6dd1c0f86cecd9628ac292160b2 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Tue, 5 Sep 2023 16:14:54 -0400 Subject: [PATCH 1/3] Ajouter Response et Responder --- responses/root.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 responses/root.go diff --git a/responses/root.go b/responses/root.go new file mode 100644 index 0000000..0f0bcce --- /dev/null +++ b/responses/root.go @@ -0,0 +1,15 @@ +package responses + +type Response struct { + StatusCode int + Message string + Error string +} + +type Responder interface { + Respond() Responder +} + +func (r Response) Respond() Responder { + return r +} From 6faca0e708107b108cb54f12bdd581c9790ade7a Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Tue, 5 Sep 2023 16:15:22 -0400 Subject: [PATCH 2/3] Ajouter PostMembres et postProgrammes responses --- responses/post.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 responses/post.go diff --git a/responses/post.go b/responses/post.go new file mode 100644 index 0000000..91eef96 --- /dev/null +++ b/responses/post.go @@ -0,0 +1,15 @@ +package responses + +type PostMembresResponse struct { + Response + Data struct { + MembresInserted int64 + } +} + +type PostProgrammesResponse struct { + Response + Data struct { + ProgrammesInserted int64 + } +} From 59eeb7a38a0e2c4513db3b544dcc7f68f3297a4b Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Tue, 5 Sep 2023 16:15:40 -0400 Subject: [PATCH 3/3] =?UTF-8?q?Impl=C3=A9menter=20responses=20POST=20pour?= =?UTF-8?q?=20application/json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handlers/insert.go | 112 +++++++++++++++++++++++++++------------------ 1 file changed, 68 insertions(+), 44 deletions(-) diff --git a/handlers/insert.go b/handlers/insert.go index 0fc6238..7452659 100644 --- a/handlers/insert.go +++ b/handlers/insert.go @@ -5,87 +5,111 @@ import ( "git.agecem.com/agecem/bottin/v5/data" "git.agecem.com/agecem/bottin/v5/models" + "git.agecem.com/agecem/bottin/v5/responses" "github.com/labstack/echo/v4" ) func PostMembres(c echo.Context) error { + var response responses.PostMembresResponse client, err := data.NewDataClientFromViper() if err != nil { - return c.JSON(http.StatusInternalServerError, map[string]string{ - "message": "Could not establish database connection", - "error": err.Error(), - }) + response.StatusCode = http.StatusInternalServerError + response.Message = "Could not establish database connection" + response.Error = err.Error() + + return c.JSON(response.StatusCode, response) } defer client.DB.Close() var membres []models.Membre - if err := c.Bind(&membres); err != nil { - return c.JSON(http.StatusBadRequest, map[string]string{ - "message": "Could not bind membres", - "error": err.Error(), - }) + switch c.Request().Header.Get("Content-Type") { + case "application/json": + if err := c.Bind(&membres); err != nil { + response.StatusCode = http.StatusBadRequest + response.Message = "Could not bind membres" + response.Error = err.Error() + return c.JSON(response.StatusCode, response) + } + case "text/csv": + response.StatusCode = http.StatusNotImplemented + response.Message = "Not Implemented" + return c.JSON(response.StatusCode, response) + default: + response.StatusCode = http.StatusBadRequest + response.Message = "Invalid Content-Type" + return c.JSON(response.StatusCode, response) } if len(membres) == 0 { - return c.JSON(http.StatusOK, map[string]string{ - "message": "Nothing to do", - }) + response.StatusCode = http.StatusOK + response.Message = "Nothing to do" + return c.JSON(response.StatusCode, response) } newMembres, err := client.InsertMembres(membres) if err != nil { - return c.JSON(http.StatusInternalServerError, map[string]string{ - "message": "Could not insert membres", - "error": err.Error(), - }) + response.StatusCode = http.StatusInternalServerError + response.Message = "Could not insert membres" + response.Error = err.Error() + return c.JSON(response.StatusCode, response) } - return c.JSON(http.StatusCreated, map[string]interface{}{ - "message": "Insert successful", - "data": map[string]interface{}{ - "membres": newMembres, - }, - }) + response.StatusCode = http.StatusCreated + response.Message = "Insert successful" + response.Data.MembresInserted = newMembres + return c.JSON(response.StatusCode, response) } func PostProgrammes(c echo.Context) error { + var response responses.PostProgrammesResponse + client, err := data.NewDataClientFromViper() if err != nil { - return c.JSON(http.StatusInternalServerError, map[string]string{ - "message": "Could not establish database connection", - "error": err.Error(), - }) + response.StatusCode = http.StatusInternalServerError + response.Message = "Could not establish database connection" + response.Error = err.Error() + + return c.JSON(response.StatusCode, response) } defer client.DB.Close() var programmes []models.Programme - if err := c.Bind(&programmes); err != nil { - return c.JSON(http.StatusBadRequest, map[string]string{ - "message": "Could not bind programmes", - "error": err.Error(), - }) + switch c.Request().Header.Get("Content-Type") { + case "application/json": + if err := c.Bind(&programmes); err != nil { + response.StatusCode = http.StatusBadRequest + response.Message = "Could not bind programmes" + response.Error = err.Error() + return c.JSON(response.StatusCode, response) + } + case "text/csv": + response.StatusCode = http.StatusNotImplemented + response.Message = "Not Implemented" + return c.JSON(response.StatusCode, response) + default: + response.StatusCode = http.StatusBadRequest + response.Message = "Invalid Content-Type" + return c.JSON(response.StatusCode, response) } if len(programmes) == 0 { - return c.JSON(http.StatusOK, map[string]string{ - "message": "Nothing to do", - }) + response.StatusCode = http.StatusOK + response.Message = "Nothing to do" + return c.JSON(response.StatusCode, response) } newProgrammes, err := client.InsertProgrammes(programmes) if err != nil { - return c.JSON(http.StatusInternalServerError, map[string]string{ - "message": "Could not insert programmes", - "error": err.Error(), - }) + response.StatusCode = http.StatusInternalServerError + response.Message = "Could not insert programmes" + response.Error = err.Error() + return c.JSON(response.StatusCode, response) } - return c.JSON(http.StatusCreated, map[string]interface{}{ - "message": "Insert successful", - "data": map[string]interface{}{ - "programmes": newProgrammes, - }, - }) + response.StatusCode = http.StatusCreated + response.Message = "Insert successful" + response.Data.ProgrammesInserted = newProgrammes + return c.JSON(response.StatusCode, response) }