diff --git a/v4/cmd/server.go b/v4/cmd/server.go index a2c8799..e2c4c48 100644 --- a/v4/cmd/server.go +++ b/v4/cmd/server.go @@ -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{ diff --git a/v4/handlers/insert.go b/v4/handlers/insert.go index 4d07443..f534d9c 100644 --- a/v4/handlers/insert.go +++ b/v4/handlers/insert.go @@ -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, + }, + }) +} diff --git a/v4/models/models.go b/v4/models/models.go index 2c29a82..c3f4af8 100644 --- a/v4/models/models.go +++ b/v4/models/models.go @@ -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 {