feature(api): add and test UpdateMembrePreferedName

This commit is contained in:
Victor Lacasse-Beaudoin 2024-06-19 00:28:26 -04:00
parent 78aafe0ce9
commit e6103c6e6e
5 changed files with 94 additions and 10 deletions

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
}