feature(api): add and test ProgrammesGET

This commit is contained in:
Victor Lacasse-Beaudoin 2024-06-19 00:04:19 -04:00
parent 26b3134861
commit 78aafe0ce9
4 changed files with 111 additions and 5 deletions

View file

@ -93,3 +93,21 @@ func (c APIClient) GetMembres(limit int) (membres []Membre, err error) {
return response.Data.Membres, nil return response.Data.Membres, nil
} }
func (c APIClient) GetProgrammes(limit int) (programmes []Programme, err error) {
var request ProgrammesGETRequest
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.Programmes, nil
}

View file

@ -39,8 +39,6 @@ func TestAPI(t *testing.T) {
} }
}) })
//TODO create or replace schema
t.Run("insert programmes", t.Run("insert programmes",
func(t *testing.T) { func(t *testing.T) {
programmes := []Programme{ programmes := []Programme{
@ -70,6 +68,15 @@ func TestAPI(t *testing.T) {
}, },
} }
t.Run("get programmes, max 50",
func(t *testing.T) {
programmes, err := apiClient.GetProgrammes(50)
if err != nil {
t.Error(err)
}
t.Log(programmes)
})
t.Run("insert membres", t.Run("insert membres",
func(t *testing.T) { func(t *testing.T) {
_, err := apiClient.InsertMembres(testMembres...) _, err := apiClient.InsertMembres(testMembres...)
@ -110,7 +117,6 @@ func TestAPI(t *testing.T) {
} }
}) })
*/ */
//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)
@ -119,6 +125,4 @@ func TestAPI(t *testing.T) {
} }
t.Log(membres) t.Log(membres)
}) })
//TODO remove test membres and programmes
} }

41
db.go
View file

@ -210,3 +210,44 @@ LIMIT
return membres, nil return membres, nil
} }
} }
func (d *PostgresClient) GetProgrammes(limit int) (programmes []Programme, 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
"programmes".id,
"programmes".name
FROM
"programmes"
ORDER BY
"programmes".id
LIMIT
$1;
`, limit)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var programme Programme
if err = rows.Scan(
&programme.ID,
&programme.Name,
); err != nil {
return nil, err
}
programmes = append(programmes, programme)
}
if rows.Err() != nil {
return programmes, rows.Err()
}
return programmes, nil
}
}

View file

@ -210,5 +210,48 @@ func addRoutes(e *echo.Echo, db *PostgresClient) error {
}); err != nil { }); err != nil {
return err return err
} }
if err := pave.EchoRegister[ProgrammesGETRequest](
apiGroup,
&p,
apiPath,
http.MethodGet,
"/programme/",
"Get programmes",
"ProgrammesGET", func(c echo.Context) (err error) {
var request, response = ProgrammesGETRequest{}, ProgrammesGETResponse{}
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 ProgrammesGET request received"
return c.JSON(response.StatusCode(), response)
}
response.Data.Programmes, err = db.GetProgrammes(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
} }