bottin/v4/handlers/insert.go

107 lines
2.6 KiB
Go
Raw Permalink Normal View History

2023-05-25 02:21:09 -04:00
package handlers
import (
"net/http"
"git.agecem.com/agecem/bottin/v4/data"
"git.agecem.com/agecem/bottin/v4/models"
"github.com/labstack/echo/v4"
"github.com/spf13/viper"
)
func PostMembres(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(),
})
}
var membres []models.Membre
2023-05-25 02:21:09 -04:00
if err := c.Bind(&membres); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{
"message": "Could not bind membres",
2023-05-25 02:21:09 -04:00
"error": err.Error(),
})
}
if len(membres) == 0 {
2023-05-25 04:31:29 -04:00
return c.JSON(http.StatusOK, map[string]string{
"message": "Nothing to do",
})
2023-05-25 02:21:09 -04:00
}
newMembres, err := client.InsertMembres(membres)
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Could not insert membres",
"error": err.Error(),
})
}
2023-05-25 04:31:29 -04:00
return c.JSON(http.StatusCreated, map[string]interface{}{
2023-05-25 02:21:09 -04:00
"message": "Insert successful",
"data": map[string]interface{}{
"membres": newMembres,
2023-05-25 02:21:09 -04:00
},
})
}
2023-05-25 03:44:41 -04:00
func PostProgrammes(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(),
})
}
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 {
2023-05-25 04:31:29 -04:00
return c.JSON(http.StatusOK, map[string]string{
2023-05-25 03:44:41 -04:00
"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(),
})
}
2023-05-25 04:31:29 -04:00
return c.JSON(http.StatusCreated, map[string]interface{}{
2023-05-25 03:44:41 -04:00
"message": "Insert successful",
"data": map[string]interface{}{
"programmes": newProgrammes,
},
})
}