Version 7 #53
4 changed files with 111 additions and 5 deletions
18
client.go
18
client.go
|
@ -93,3 +93,21 @@ func (c APIClient) GetMembres(limit int) (membres []Membre, err error) {
|
|||
|
||||
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
|
||||
}
|
||||
|
|
|
@ -39,8 +39,6 @@ func TestAPI(t *testing.T) {
|
|||
}
|
||||
})
|
||||
|
||||
//TODO create or replace schema
|
||||
|
||||
t.Run("insert programmes",
|
||||
func(t *testing.T) {
|
||||
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",
|
||||
func(t *testing.T) {
|
||||
_, err := apiClient.InsertMembres(testMembres...)
|
||||
|
@ -110,7 +117,6 @@ func TestAPI(t *testing.T) {
|
|||
}
|
||||
})
|
||||
*/
|
||||
//TODO get membres
|
||||
t.Run("get membres, max 50",
|
||||
func(t *testing.T) {
|
||||
membres, err := apiClient.GetMembres(50)
|
||||
|
@ -119,6 +125,4 @@ func TestAPI(t *testing.T) {
|
|||
}
|
||||
t.Log(membres)
|
||||
})
|
||||
|
||||
//TODO remove test membres and programmes
|
||||
}
|
||||
|
|
41
db.go
41
db.go
|
@ -210,3 +210,44 @@ LIMIT
|
|||
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
|
||||
}
|
||||
}
|
||||
|
|
43
routes.go
43
routes.go
|
@ -210,5 +210,48 @@ func addRoutes(e *echo.Echo, db *PostgresClient) error {
|
|||
}); err != nil {
|
||||
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
|
||||
}
|
||||
|
|
Reference in a new issue