bottin/handlers/update.go

61 lines
1.4 KiB
Go
Raw Permalink Normal View History

2023-05-25 04:31:29 -04:00
package handlers
import (
"net/http"
"git.agecem.com/agecem/bottin/v5/data"
2023-05-25 04:31:29 -04:00
"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,
},
})
}