Ajouter insertion de programmes
This commit is contained in:
parent
cb51ada4b6
commit
f1a4d190df
3 changed files with 52 additions and 1 deletions
|
@ -57,3 +57,52 @@ func PostMembres(c echo.Context) error {
|
|||
},
|
||||
})
|
||||
}
|
||||
|
||||
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 {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{
|
||||
"message": "Nothing to do",
|
||||
"error": "No valid programmes to insert were found",
|
||||
})
|
||||
}
|
||||
|
||||
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.StatusOK, map[string]interface{}{
|
||||
"message": "Insert successful",
|
||||
"data": map[string]interface{}{
|
||||
"programmes": newProgrammes,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
Reference in a new issue