Réécrire le projet pour la v4 #4
4 changed files with 79 additions and 6 deletions
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
60
v4/handlers/update.go
Normal file
60
v4/handlers/update.go
Normal file
|
@ -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,
|
||||
},
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue