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 125 additions and 1 deletions
Showing only changes of commit e1bce94d18 - Show all commits

View file

@ -24,3 +24,20 @@ func (c APIClient) GetHealth() (health string, err error) {
return response.Message, nil 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
}

View file

@ -8,6 +8,10 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
func init() {
initConfig()
}
func TestAPI(t *testing.T) { func TestAPI(t *testing.T) {
var cfg Config var cfg Config
if err := viper.Unmarshal(&cfg); err != nil { 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
View file

@ -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 { select {
case <-d.Ctx.Done(): case <-d.Ctx.Done():
return inserted, fmt.Errorf("PostgresClient.Ctx closed: %s", d.Ctx.Err()) return inserted, fmt.Errorf("PostgresClient.Ctx closed: %s", d.Ctx.Err())

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
@ -38,3 +39,43 @@ func (request HealthGETRequest) Request(v *voki.Voki) (response HealthGETRespons
return 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
}

View file

@ -41,5 +41,48 @@ func addRoutes(e *echo.Echo, db *PostgresClient) error {
}); err != nil { }); err != nil {
return err 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 return nil
} }