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 94 additions and 10 deletions
Showing only changes of commit e6103c6e6e - Show all commits

View file

@ -111,3 +111,25 @@ func (c APIClient) GetProgrammes(limit int) (programmes []Programme, err error)
return response.Data.Programmes, nil
}
func (c APIClient) UpdateMembrePreferedName(membreID string, name string) (err error) {
var request MembrePreferedNamePUTRequest
if !IsMembreID(membreID) {
return fmt.Errorf("Numéro étudiant '%s' invalide", membreID)
}
request.Param.MembreID = membreID
request.Data.PreferedName = name
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 nil
}

View file

@ -108,15 +108,12 @@ func TestAPI(t *testing.T) {
}
})
//TODO update membre prefered name
/*
t.Run("",
t.Run("update membre prefered name",
func(t *testing.T) {
if err := apiClient.UpdateMembrePreferedName(testMembres[0].ID, "User, Galaxy"); err != nil {
t.Error(err)
}
})
*/
t.Run("get membres, max 50",
func(t *testing.T) {
membres, err := apiClient.GetMembres(50)

21
db.go
View file

@ -251,3 +251,24 @@ LIMIT
return programmes, nil
}
}
func (d *PostgresClient) UpdateMembrePreferedName(membreID string, name string) (err error) {
select {
case <-d.Ctx.Done():
return fmt.Errorf("PostgresClient.Ctx closed: %s", d.Ctx.Err())
default:
if !IsMembreID(membreID) {
return fmt.Errorf("Numéro étudiant '%s' invalide", membreID)
}
_, err = d.Pool.Exec(d.Ctx, `
UPDATE
"membres"
SET
prefered_name = $1
WHERE
"membres".id = $2;
`, name, membreID)
}
return
}

View file

@ -224,7 +224,7 @@ func (request MembrePreferedNamePUTRequest) Request(v *voki.Voki) (response Memb
statusCode, body, err := v.CallAndParse(
http.MethodPut,
"/api/v7/membre/%s/",
fmt.Sprintf("/api/v7/membre/%s/prefered_name/", request.Param.MembreID),
&buf,
true,
)

View file

@ -253,5 +253,49 @@ func addRoutes(e *echo.Echo, db *PostgresClient) error {
}); err != nil {
return err
}
if err := pave.EchoRegister[MembrePreferedNamePUTRequest](
apiGroup,
&p,
apiPath,
http.MethodPut,
"/membre/:membre_id/prefered_name/",
"Update membre prefered name, which is prioritized in the membres_for_display view",
"MembrePreferedNamePUT", func(c echo.Context) error {
var request, response = MembrePreferedNamePUTRequest{}, MembrePreferedNamePUTResponse{}
request.Param.MembreID = c.Param("membre_id")
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 MembrePreferedNamePUT request received"
return c.JSON(response.StatusCode(), response)
}
if err := db.UpdateMembrePreferedName(request.Param.MembreID, request.Data.PreferedName); 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 = fmt.Sprintf("Updated membre %s name to %s", request.Param.MembreID, request.Data.PreferedName)
return c.JSON(response.StatusCode(), response)
}); err != nil {
return err
}
return nil
}