From 6ede2083fa34a483b57d8fcf4c5b4d75c47fe51b Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Tue, 19 Sep 2023 19:09:51 -0400 Subject: [PATCH] Ajouter GET /v5/membres pour lister membres en json --- cmd/api.go | 2 ++ data/apiclient.go | 4 ++++ data/data.go | 4 ++++ handlers/read.go | 30 ++++++++++++++++++++++++++++++ responses/list.go | 13 +++++++++++++ 5 files changed, 53 insertions(+) create mode 100644 responses/list.go diff --git a/cmd/api.go b/cmd/api.go index 6de994f..0c8f84b 100644 --- a/cmd/api.go +++ b/cmd/api.go @@ -65,6 +65,8 @@ var apiCmd = &cobra.Command{ e.POST("/v5/membres/", h.PostMembres) + e.GET("/v5/membres/", h.ListMembres) + e.GET("/v5/membres/:membre_id/", h.ReadMembre) e.PUT("/v5/membres/:membre_id/prefered_name/", h.PutMembrePreferedName) diff --git a/data/apiclient.go b/data/apiclient.go index 3ce3694..d5257cf 100644 --- a/data/apiclient.go +++ b/data/apiclient.go @@ -68,3 +68,7 @@ func (a *ApiClient) GetMembre(membreID string) (models.Membre, error) { return getMembreResponse.Data.Membre, nil } + +func (a *ApiClient) ListMembres() (r responses.ListMembresResponse, err error) { + return r, a.Voki.Unmarshal(http.MethodGet, "/v5/membres", nil, true, &r) +} diff --git a/data/data.go b/data/data.go index ec603f7..98c293b 100644 --- a/data/data.go +++ b/data/data.go @@ -182,3 +182,7 @@ func (d *DataClient) UpdateMembreName(membreID, newName string) (int64, error) { return rows, nil } + +func (d *DataClient) GetMembres() (membres []models.Membre, err error) { + return membres, d.DB.Select(&membres, "SELECT * FROM membres;") +} diff --git a/handlers/read.go b/handlers/read.go index 5048f25..8fec2b1 100644 --- a/handlers/read.go +++ b/handlers/read.go @@ -1,8 +1,10 @@ package handlers import ( + "fmt" "net/http" + "git.agecem.com/agecem/bottin/v5/responses" "github.com/labstack/echo/v4" ) @@ -29,3 +31,31 @@ func (h *Handler) ReadMembre(c echo.Context) error { }, }) } + +func (h *Handler) ListMembres(c echo.Context) error { + var r responses.ListMembresResponse + + membres, err := h.DataClient.GetMembres() + if err != nil { + r.StatusCode = http.StatusInternalServerError + r.Error = err.Error() + r.Message = "Error during (*handlers.Handler).DataClient.GetMembres" + + return c.JSON(r.StatusCode, r) + } + + r.StatusCode = http.StatusOK + + switch membres := len(membres); membres { + case 0: + r.Message = "No membres returned from database" + case 1: + r.Message = "Membre returned from database" + default: + r.Message = fmt.Sprintf("%d membres returned from database", membres) + } + + r.Data.Membres = membres + + return c.JSON(r.StatusCode, r) +} diff --git a/responses/list.go b/responses/list.go new file mode 100644 index 0000000..04df99f --- /dev/null +++ b/responses/list.go @@ -0,0 +1,13 @@ +package responses + +import ( + "codeberg.org/vlbeaudoin/voki/response" + "git.agecem.com/agecem/bottin/v5/models" +) + +type ListMembresResponse struct { + response.ResponseWithError + Data struct { + Membres []models.Membre + } +}