From e1bce94d18e78788ae3be1f3fd0061ef8eb6d103 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Mon, 17 Jun 2024 14:07:49 -0400 Subject: [PATCH] feature: add and test ProgrammesPOST --- client.go | 17 +++++++++++++++++ client_test.go | 23 +++++++++++++++++++++++ db.go | 2 +- request.go | 41 +++++++++++++++++++++++++++++++++++++++++ routes.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 125 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index 918fbee..dd7e1ea 100644 --- a/client.go +++ b/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 +} diff --git a/client_test.go b/client_test.go index 2c66470..1f902d2 100644 --- a/client_test.go +++ b/client_test.go @@ -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 + } diff --git a/db.go b/db.go index ff715d8..eda594c 100644 --- a/db.go +++ b/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()) diff --git a/request.go b/request.go index ecb4346..3ff29d7 100644 --- a/request.go +++ b/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 +} diff --git a/routes.go b/routes.go index ee07763..6041358 100644 --- a/routes.go +++ b/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 }