From b9044fa08d23da86c6f426f50ac6a1df73ea0852 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Thu, 25 May 2023 04:31:29 -0400 Subject: [PATCH] Ajouter update de membre.prefered_name --- v4/cmd/server.go | 1 + v4/data/data.go | 14 ++++++++++ v4/handlers/insert.go | 10 +++----- v4/handlers/update.go | 60 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 v4/handlers/update.go diff --git a/v4/cmd/server.go b/v4/cmd/server.go index e2c4c48..58a0912 100644 --- a/v4/cmd/server.go +++ b/v4/cmd/server.go @@ -44,6 +44,7 @@ var serverCmd = &cobra.Command{ e.GET("/v4/membres/:membre_id/", handlers.ReadMembre) e.POST("/v4/membres/", handlers.PostMembres) + e.PUT("/v4/membres/:membre_id/prefered_name/", handlers.PutMembrePreferedName) e.POST("/v4/programmes/", handlers.PostProgrammes) diff --git a/v4/data/data.go b/v4/data/data.go index 4817e7a..db8addb 100644 --- a/v4/data/data.go +++ b/v4/data/data.go @@ -131,6 +131,20 @@ func (d *DataClient) GetMembre(membreID string) (models.Membre, error) { return membre, nil } +func (d *DataClient) UpdateMembreName(membreID, newName string) (int64, error) { + result, err := d.DB.Exec("UPDATE membres SET prefered_name = $1 WHERE id = $2;", newName, membreID) + if err != nil { + return 0, err + } + + rows, err := result.RowsAffected() + if err != nil { + return rows, err + } + + return rows, nil +} + /* func (d *DataClient) Insert(assets []models.Asset) (id int64, err error) { // Check for minimal required info diff --git a/v4/handlers/insert.go b/v4/handlers/insert.go index f534d9c..629d6e7 100644 --- a/v4/handlers/insert.go +++ b/v4/handlers/insert.go @@ -36,9 +36,8 @@ func PostMembres(c echo.Context) error { } if len(membres) == 0 { - return c.JSON(http.StatusBadRequest, map[string]string{ + return c.JSON(http.StatusOK, map[string]string{ "message": "Nothing to do", - "error": "No valid membres to insert were found", }) } @@ -50,7 +49,7 @@ func PostMembres(c echo.Context) error { }) } - return c.JSON(http.StatusOK, map[string]interface{}{ + return c.JSON(http.StatusCreated, map[string]interface{}{ "message": "Insert successful", "data": map[string]interface{}{ "membres": newMembres, @@ -85,9 +84,8 @@ func PostProgrammes(c echo.Context) error { } if len(programmes) == 0 { - return c.JSON(http.StatusBadRequest, map[string]string{ + return c.JSON(http.StatusOK, map[string]string{ "message": "Nothing to do", - "error": "No valid programmes to insert were found", }) } @@ -99,7 +97,7 @@ func PostProgrammes(c echo.Context) error { }) } - return c.JSON(http.StatusOK, map[string]interface{}{ + return c.JSON(http.StatusCreated, map[string]interface{}{ "message": "Insert successful", "data": map[string]interface{}{ "programmes": newProgrammes, diff --git a/v4/handlers/update.go b/v4/handlers/update.go new file mode 100644 index 0000000..8a8cf0a --- /dev/null +++ b/v4/handlers/update.go @@ -0,0 +1,60 @@ +package handlers + +import ( + "net/http" + + "git.agecem.com/agecem/bottin/v4/data" + "github.com/labstack/echo/v4" + "github.com/spf13/viper" +) + +func PutMembrePreferedName(c echo.Context) error { + connection := data.PostgresConnection{ + User: viper.GetString("db.user"), + Password: viper.GetString("db.password"), + Host: viper.GetString("db.host"), + Database: viper.GetString("db.database"), + Port: viper.GetInt("db.port"), + } + + client, err := data.NewDataClient(connection) + if err != nil { + return c.JSON(http.StatusInternalServerError, map[string]string{ + "message": "Could not establish database connection", + "error": err.Error(), + }) + } + + membreID := c.Param("membre_id") + + var newName string + + err = c.Bind(&newName) + if err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{ + "message": "Could not bind newName", + "error": err.Error(), + }) + } + + rows, err := client.UpdateMembreName(membreID, newName) + if err != nil { + return c.JSON(http.StatusInternalServerError, map[string]string{ + "message": "Could not update membre name", + "error": err.Error(), + }) + } + + if rows == 0 { + return c.JSON(http.StatusBadRequest, map[string]string{ + "message": "No update was done, probably no membre by that id", + }) + } + + return c.JSON(http.StatusOK, map[string]interface{}{ + "message": "Update successful", + "data": map[string]interface{}{ + "rows": rows, + }, + }) +}