bottin/handlers/insert.go

89 lines
2.1 KiB
Go

package handlers
import (
"net/http"
"git.agecem.com/agecem/bottin/v5/data"
"git.agecem.com/agecem/bottin/v5/models"
"github.com/labstack/echo/v4"
)
func PostMembres(c echo.Context) error {
client, err := data.NewDataClientFromViper()
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Could not establish database connection",
"error": err.Error(),
})
}
var membres []models.Membre
if err := c.Bind(&membres); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{
"message": "Could not bind membres",
"error": err.Error(),
})
}
if len(membres) == 0 {
return c.JSON(http.StatusOK, map[string]string{
"message": "Nothing to do",
})
}
newMembres, err := client.InsertMembres(membres)
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Could not insert membres",
"error": err.Error(),
})
}
return c.JSON(http.StatusCreated, map[string]interface{}{
"message": "Insert successful",
"data": map[string]interface{}{
"membres": newMembres,
},
})
}
func PostProgrammes(c echo.Context) error {
client, err := data.NewDataClientFromViper()
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Could not establish database connection",
"error": err.Error(),
})
}
var programmes []models.Programme
if err := c.Bind(&programmes); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{
"message": "Could not bind programmes",
"error": err.Error(),
})
}
if len(programmes) == 0 {
return c.JSON(http.StatusOK, map[string]string{
"message": "Nothing to do",
})
}
newProgrammes, err := client.InsertProgrammes(programmes)
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Could not insert programmes",
"error": err.Error(),
})
}
return c.JSON(http.StatusCreated, map[string]interface{}{
"message": "Insert successful",
"data": map[string]interface{}{
"programmes": newProgrammes,
},
})
}