77 lines
1.6 KiB
Go
77 lines
1.6 KiB
Go
|
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(),
|
||
|
})
|
||
|
}
|
||
|
|
||
|
programmes := []models.Programme{
|
||
|
{
|
||
|
ID: "foo",
|
||
|
Titre: "Foo",
|
||
|
},
|
||
|
{
|
||
|
ID: "bar",
|
||
|
Titre: "Bar",
|
||
|
},
|
||
|
}
|
||
|
|
||
|
newProgrammes, err := client.InsertProgrammes(programmes)
|
||
|
if err != nil {
|
||
|
return c.JSON(http.StatusInternalServerError, map[string]string{
|
||
|
"message": "Could not insert programmes",
|
||
|
"error": err.Error(),
|
||
|
})
|
||
|
}
|
||
|
|
||
|
membres := []models.Membre{
|
||
|
{
|
||
|
ID: "1327163",
|
||
|
PreferedName: "victor",
|
||
|
ProgrammeID: "foo",
|
||
|
},
|
||
|
{
|
||
|
ID: "0000000",
|
||
|
PreferedName: "test user",
|
||
|
ProgrammeID: "bar",
|
||
|
},
|
||
|
}
|
||
|
|
||
|
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.StatusOK, map[string]interface{}{
|
||
|
"message": "Insert successful",
|
||
|
"data": map[string]interface{}{
|
||
|
"membres": newMembres,
|
||
|
"programmes": newProgrammes,
|
||
|
},
|
||
|
})
|
||
|
}
|