From d6573287f412ed8b0fc622d65347c4b269b61b78 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Sat, 3 Jun 2023 20:41:13 -0400 Subject: [PATCH] Ajouter GetMembreResponse --- handlers/membre.go | 37 +++++++++++++++++++++++-------------- responses/responses.go | 10 ++++++++++ 2 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 responses/responses.go diff --git a/handlers/membre.go b/handlers/membre.go index 3b8286c..67b5215 100644 --- a/handlers/membre.go +++ b/handlers/membre.go @@ -6,6 +6,7 @@ import ( "github.com/labstack/echo/v4" "github.com/spf13/viper" + "git.agecem.com/agecem/bottin-agenda/responses" bottindata "git.agecem.com/agecem/bottin/v5/data" ) @@ -25,23 +26,31 @@ func GetMembre(c echo.Context) error { membreID := c.Param("membre_id") + getMembreResponse := responses.GetMembreResponse{} + membre, err := bottinConnection.GetMembre(membreID) if err != nil { - if err.Error() == "No membre by that id was found" { - return c.JSON(http.StatusNotFound, map[string]string{ - "message": "Not Found", - }) + getMembreResponse.Message = err.Error() + + var statusCode int + + switch err.Error() { + case "Veuillez fournir un numéro étudiant à rechercher": + statusCode = http.StatusBadRequest + case "Ce numéro étudiant ne correspond à aucunE membre": + statusCode = http.StatusNotFound + default: + statusCode = http.StatusInternalServerError } - return c.JSON(http.StatusInternalServerError, map[string]string{ - "message": "Unknown error during GetMembre", - "error": err.Error(), - }) + + getMembreResponse.Data.Membre = membre + + return c.JSON(statusCode, getMembreResponse) } - return c.JSON(http.StatusOK, map[string]interface{}{ - "message": "Read successful", - "data": map[string]interface{}{ - "membre": &membre, - }, - }) + getMembreResponse.Data.Membre = membre + + getMembreResponse.Message = "Read successful" + + return c.JSON(http.StatusOK, getMembreResponse) } diff --git a/responses/responses.go b/responses/responses.go new file mode 100644 index 0000000..64406af --- /dev/null +++ b/responses/responses.go @@ -0,0 +1,10 @@ +package responses + +import bottinmodels "git.agecem.com/agecem/bottin/v5/models" + +type GetMembreResponse struct { + Message string `json:"message"` + Data struct { + Membre bottinmodels.Membre + } `json:"data"` +}