Version 7 #53

Merged
vlbeaudoin merged 33 commits from rewrite/v7 into main 2024-09-03 11:17:26 -04:00
5 changed files with 117 additions and 19 deletions
Showing only changes of commit f8b5c72003 - Show all commits

View file

@ -75,3 +75,21 @@ func (c APIClient) GetMembre(membreID string) (membre Membre, err error) {
return response.Data.Membre, nil 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
}

View file

@ -111,13 +111,14 @@ func TestAPI(t *testing.T) {
}) })
*/ */
//TODO get membres //TODO get membres
/* t.Run("get membres, max 50",
t.Run("get membres, max 50", func(t *testing.T) {
func(t *testing.T) { membres, err := apiClient.GetMembres(50)
membres, err := apiClient.GetMembres(50) if err != nil {
if err != nil { t.Error(err)
t.Error(err) }
} t.Log(membres)
}) })
*/
//TODO remove test membres and programmes
} }

21
db.go
View file

@ -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 { select {
case <-d.Ctx.Done(): case <-d.Ctx.Done():
return nil, fmt.Errorf("PostgresClient.Ctx closed: %s", d.Ctx.Err()) return nil, fmt.Errorf("PostgresClient.Ctx closed: %s", d.Ctx.Err())
default: default:
rows, err := d.Pool.Query(d.Ctx, ` rows, err := d.Pool.Query(d.Ctx, `
SELECT SELECT
membres.id, "membres".id,
membres.last_name, "membres".last_name,
membres.first_name, "membres".first_name,
membres.prefered_name, "membres".prefered_name,
membres.programme_id "membres".programme_id
FROM FROM
membres "membres"
LIMIT
10000
ORDER BY ORDER BY
membres.id;`) "membres".id
LIMIT
$1;
`, limit)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -161,3 +161,37 @@ func (request MembreGETRequest) Request(v *voki.Voki) (response MembreGETRespons
return 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
}

View file

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"strconv"
"codeberg.org/vlbeaudoin/pave/v2" "codeberg.org/vlbeaudoin/pave/v2"
"codeberg.org/vlbeaudoin/voki/v3" "codeberg.org/vlbeaudoin/voki/v3"
@ -166,5 +167,48 @@ func addRoutes(e *echo.Echo, db *PostgresClient) error {
}); err != nil { }); err != nil {
return err 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 return nil
} }