Ajouter insertion de programmes

This commit is contained in:
Victor Lacasse-Beaudoin 2023-05-25 03:44:41 -04:00
parent cb51ada4b6
commit f1a4d190df
3 changed files with 52 additions and 1 deletions

View file

@ -45,6 +45,8 @@ var serverCmd = &cobra.Command{
e.GET("/v4/membres/:membre_id/", handlers.ReadMembre)
e.POST("/v4/membres/", handlers.PostMembres)
e.POST("/v4/programmes/", handlers.PostProgrammes)
// Execution
connection := data.PostgresConnection{

View file

@ -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,
},
})
}

View file

@ -17,7 +17,7 @@ CREATE TABLE membres (
type Programme struct {
ID string `db:"id" json:"programme_id"`
Titre string `db:"titre" json:"titre"`
Titre string `db:"titre" json:"nom_programme"`
}
type Membre struct {