From f8b5c720036b5d37bd1415ed4628ef002e08cc90 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Tue, 18 Jun 2024 21:21:30 -0400 Subject: [PATCH] feature: add and test GetMembres --- client.go | 18 ++++++++++++++++++ client_test.go | 19 ++++++++++--------- db.go | 21 +++++++++++---------- request.go | 34 ++++++++++++++++++++++++++++++++++ routes.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 117 insertions(+), 19 deletions(-) diff --git a/client.go b/client.go index d47a723..386dbdd 100644 --- a/client.go +++ b/client.go @@ -75,3 +75,21 @@ func (c APIClient) GetMembre(membreID string) (membre Membre, err error) { return response.Data.Membre, nil } + +func (c APIClient) GetMembres(limit int) (membres []Membre, err error) { + var request MembresGETRequest + + request.Query.Limit = limit + + response, err := request.Request(c.Voki) + if err != nil { + return + } + + if code, message := response.StatusCode(), response.Message; code >= 400 { + err = fmt.Errorf("%d: %s", code, message) + return + } + + return response.Data.Membres, nil +} diff --git a/client_test.go b/client_test.go index 7a847ad..7a7a62d 100644 --- a/client_test.go +++ b/client_test.go @@ -111,13 +111,14 @@ func TestAPI(t *testing.T) { }) */ //TODO get membres - /* - t.Run("get membres, max 50", - func(t *testing.T) { - membres, err := apiClient.GetMembres(50) - if err != nil { - t.Error(err) - } - }) - */ + t.Run("get membres, max 50", + func(t *testing.T) { + membres, err := apiClient.GetMembres(50) + if err != nil { + t.Error(err) + } + t.Log(membres) + }) + + //TODO remove test membres and programmes } diff --git a/db.go b/db.go index bb401ae..90d24f7 100644 --- a/db.go +++ b/db.go @@ -164,24 +164,25 @@ func (d *PostgresClient) UpdateMembreName(membreID, newName string) (int64, erro } */ -func (d *PostgresClient) GetMembres() (membres []Membre, err error) { +func (d *PostgresClient) GetMembres(limit int) (membres []Membre, err error) { select { case <-d.Ctx.Done(): return nil, fmt.Errorf("PostgresClient.Ctx closed: %s", d.Ctx.Err()) default: rows, err := d.Pool.Query(d.Ctx, ` SELECT - membres.id, - membres.last_name, - membres.first_name, - membres.prefered_name, - membres.programme_id + "membres".id, + "membres".last_name, + "membres".first_name, + "membres".prefered_name, + "membres".programme_id FROM - membres -LIMIT - 10000 + "membres" ORDER BY - membres.id;`) + "membres".id +LIMIT + $1; +`, limit) if err != nil { return nil, err } diff --git a/request.go b/request.go index 277e6f4..df538b3 100644 --- a/request.go +++ b/request.go @@ -161,3 +161,37 @@ func (request MembreGETRequest) Request(v *voki.Voki) (response MembreGETRespons return } + +var _ voki.Requester[MembresGETResponse] = MembresGETRequest{} + +type MembresGETRequest struct { + Query struct { + Limit int `json:"limit" query:"limit"` + } +} + +func (request MembresGETRequest) Complete() bool { return true } + +func (request MembresGETRequest) Request(v *voki.Voki) (response MembresGETResponse, err error) { + if !request.Complete() { + err = fmt.Errorf("Incomplete MembresGETRequest") + return + } + + statusCode, body, err := v.CallAndParse( + http.MethodGet, + fmt.Sprintf("/api/v7/membre/?limit=%d", request.Query.Limit), + nil, + true, + ) + if err != nil { + err = fmt.Errorf("code=%d err=%s", statusCode, err) + return + } + response.SetStatusCode(statusCode) + if err = json.Unmarshal(body, &response); err != nil { + return + } + + return +} diff --git a/routes.go b/routes.go index c5b3083..412141a 100644 --- a/routes.go +++ b/routes.go @@ -3,6 +3,7 @@ package main import ( "fmt" "net/http" + "strconv" "codeberg.org/vlbeaudoin/pave/v2" "codeberg.org/vlbeaudoin/voki/v3" @@ -166,5 +167,48 @@ func addRoutes(e *echo.Echo, db *PostgresClient) error { }); err != nil { return err } + + if err := pave.EchoRegister[MembresGETRequest]( + apiGroup, + &p, + apiPath, + http.MethodGet, + "/membre/", + "Get membres", + "MembresGET", func(c echo.Context) (err error) { + var request, response = MembresGETRequest{}, MembresGETResponse{} + + request.Query.Limit, err = strconv.Atoi(c.QueryParam("limit")) + if err != nil { + var response voki.ResponseBadRequest + response.Message = fmt.Sprintf("parsing limit: %s", err) + return c.JSON(response.StatusCode(), response) + } + + if !request.Complete() { + var response voki.ResponseBadRequest + response.Message = "Incomplete MembresGET request received" + return c.JSON(response.StatusCode(), response) + } + + response.Data.Membres, err = db.GetMembres(request.Query.Limit) + if err != nil { + var response voki.ResponseBadRequest + response.Message = fmt.Sprintf("db: %s", err) + return c.JSON(response.StatusCode(), response) + } + + if err := response.SetStatusCode(http.StatusOK); err != nil { + var response voki.ResponseInternalServerError + response.Message = fmt.Sprintf("handler: %s", err) + return c.JSON(response.StatusCode(), response) + } + + response.Message = "ok" + return c.JSON(response.StatusCode(), response) + + }); err != nil { + return err + } return nil }