feature: add and test ProgrammesPOST
This commit is contained in:
parent
c5339bd45b
commit
e1bce94d18
5 changed files with 125 additions and 1 deletions
17
client.go
17
client.go
|
@ -24,3 +24,20 @@ func (c APIClient) GetHealth() (health string, err error) {
|
|||
|
||||
return response.Message, nil
|
||||
}
|
||||
|
||||
func (c APIClient) InsertProgrammes(programmes ...Programme) (amountInserted int64, err error) {
|
||||
var request ProgrammesPOSTRequest
|
||||
request.Data.Programmes = programmes
|
||||
|
||||
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.ProgrammesInserted, nil
|
||||
}
|
||||
|
|
|
@ -8,6 +8,10 @@ import (
|
|||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func init() {
|
||||
initConfig()
|
||||
}
|
||||
|
||||
func TestAPI(t *testing.T) {
|
||||
var cfg Config
|
||||
if err := viper.Unmarshal(&cfg); err != nil {
|
||||
|
@ -35,4 +39,23 @@ func TestAPI(t *testing.T) {
|
|||
}
|
||||
})
|
||||
|
||||
//TODO create or replace schema
|
||||
//TODO insert programmes
|
||||
t.Run("insert programmes",
|
||||
func(t *testing.T) {
|
||||
programmes := []Programme{
|
||||
{"404.42", "Cool programme"},
|
||||
{"200.10", "Autre programme"},
|
||||
}
|
||||
t.Log("programmes:", programmes)
|
||||
_, err := apiClient.InsertProgrammes(programmes...)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
//TODO insert membres
|
||||
//TODO get membre
|
||||
//TODO update membre prefered name
|
||||
//TODO get membres
|
||||
|
||||
}
|
||||
|
|
2
db.go
2
db.go
|
@ -66,7 +66,7 @@ ON CONFLICT (id) DO NOTHING;`,
|
|||
}
|
||||
}
|
||||
|
||||
func (d *PostgresClient) InsertProgrammes(programmes []Programme) (inserted int64, err error) {
|
||||
func (d *PostgresClient) InsertProgrammes(programmes ...Programme) (inserted int64, err error) {
|
||||
select {
|
||||
case <-d.Ctx.Done():
|
||||
return inserted, fmt.Errorf("PostgresClient.Ctx closed: %s", d.Ctx.Err())
|
||||
|
|
41
request.go
41
request.go
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
@ -38,3 +39,43 @@ func (request HealthGETRequest) Request(v *voki.Voki) (response HealthGETRespons
|
|||
|
||||
return
|
||||
}
|
||||
|
||||
var _ voki.Requester[ProgrammesPOSTResponse] = ProgrammesPOSTRequest{}
|
||||
|
||||
type ProgrammesPOSTRequest struct {
|
||||
Data struct {
|
||||
Programmes []Programme
|
||||
}
|
||||
}
|
||||
|
||||
func (request ProgrammesPOSTRequest) Complete() bool {
|
||||
return len(request.Data.Programmes) != 0
|
||||
}
|
||||
func (request ProgrammesPOSTRequest) Request(v *voki.Voki) (response ProgrammesPOSTResponse, err error) {
|
||||
if !request.Complete() {
|
||||
err = fmt.Errorf("Incomplete ProgrammesPOSTRequest")
|
||||
return
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err = json.NewEncoder(&buf).Encode(request.Data); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
statusCode, body, err := v.CallAndParse(
|
||||
http.MethodPost,
|
||||
"/api/programmes/",
|
||||
&buf,
|
||||
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
|
||||
}
|
||||
|
|
43
routes.go
43
routes.go
|
@ -41,5 +41,48 @@ func addRoutes(e *echo.Echo, db *PostgresClient) error {
|
|||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := pave.EchoRegister[ProgrammesPOSTRequest](
|
||||
apiGroup,
|
||||
&p,
|
||||
apiPath,
|
||||
http.MethodPost,
|
||||
"/programmes/",
|
||||
"Get registered programmes",
|
||||
"ProgrammesPOST", func(c echo.Context) error {
|
||||
var request, response = ProgrammesPOSTRequest{}, ProgrammesPOSTResponse{}
|
||||
|
||||
if err := c.Bind(&request.Data); err != nil {
|
||||
var response voki.ResponseBadRequest
|
||||
response.Message = fmt.Sprintf("parse request body: %s", err)
|
||||
return c.JSON(response.StatusCode(), response)
|
||||
}
|
||||
|
||||
if !request.Complete() {
|
||||
var response voki.ResponseBadRequest
|
||||
response.Message = "Incomplete ProgrammesPOST request received"
|
||||
return c.JSON(response.StatusCode(), response)
|
||||
}
|
||||
|
||||
amountInserted, err := db.InsertProgrammes(request.Data.Programmes...)
|
||||
if err != nil {
|
||||
var response voki.ResponseBadRequest
|
||||
response.Message = fmt.Sprintf("db: %s", err)
|
||||
return c.JSON(response.StatusCode(), response)
|
||||
}
|
||||
response.Data.ProgrammesInserted = amountInserted
|
||||
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue