From aa6f3479f6dad9eea2c8ea4a8d5ea40610b81ddd Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Mon, 18 Sep 2023 22:55:40 -0400 Subject: [PATCH] =?UTF-8?q?R=C3=A9utiliser=20*data.DataClient=20=C3=A0=20t?= =?UTF-8?q?ravers=20API=20handlers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/api.go | 34 +++++++++++++++++++--------------- handlers/handlers.go | 11 +++++++++++ handlers/health.go | 2 +- handlers/insert.go | 28 ++++------------------------ handlers/read.go | 22 ++-------------------- handlers/seed.go | 22 ++-------------------- handlers/update.go | 24 +++--------------------- 7 files changed, 42 insertions(+), 101 deletions(-) create mode 100644 handlers/handlers.go diff --git a/cmd/api.go b/cmd/api.go index 9a05014..6de994f 100644 --- a/cmd/api.go +++ b/cmd/api.go @@ -39,21 +39,7 @@ var apiCmd = &cobra.Command{ })) } - // Routes - - e.GET("/v5/health/", handlers.GetHealth) - - e.POST("/v5/membres/", handlers.PostMembres) - - e.GET("/v5/membres/:membre_id/", handlers.ReadMembre) - - e.PUT("/v5/membres/:membre_id/prefered_name/", handlers.PutMembrePreferedName) - - e.POST("/v5/programmes/", handlers.PostProgrammes) - - e.POST("/v5/seed/", handlers.PostSeed) - - // Execution + // DataClient client, err := data.NewDataClientFromViper() if err != nil { @@ -71,6 +57,24 @@ var apiCmd = &cobra.Command{ log.Fatalf("Error during client.Seed(): %s", err) } + h := handlers.New(client) + + // Routes + + e.GET("/v5/health/", h.GetHealth) + + e.POST("/v5/membres/", h.PostMembres) + + e.GET("/v5/membres/:membre_id/", h.ReadMembre) + + e.PUT("/v5/membres/:membre_id/prefered_name/", h.PutMembrePreferedName) + + e.POST("/v5/programmes/", h.PostProgrammes) + + e.POST("/v5/seed/", h.PostSeed) + + // Execution + e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", apiPort))) }, } diff --git a/handlers/handlers.go b/handlers/handlers.go new file mode 100644 index 0000000..7bbb88f --- /dev/null +++ b/handlers/handlers.go @@ -0,0 +1,11 @@ +package handlers + +import "git.agecem.com/agecem/bottin/v5/data" + +type Handler struct { + DataClient *data.DataClient +} + +func New(dataClient *data.DataClient) *Handler { + return &Handler{DataClient: dataClient} +} diff --git a/handlers/health.go b/handlers/health.go index d4fe868..9e58673 100644 --- a/handlers/health.go +++ b/handlers/health.go @@ -8,7 +8,7 @@ import ( "github.com/labstack/echo/v4" ) -func GetHealth(c echo.Context) error { +func (h *Handler) GetHealth(c echo.Context) error { var response responses.GetHealthResponse dataClient, err := data.NewDataClientFromViper() diff --git a/handlers/insert.go b/handlers/insert.go index 789beb3..9a1c79b 100644 --- a/handlers/insert.go +++ b/handlers/insert.go @@ -5,7 +5,6 @@ import ( "io" "net/http" - "git.agecem.com/agecem/bottin/v5/data" "git.agecem.com/agecem/bottin/v5/models" "git.agecem.com/agecem/bottin/v5/responses" "github.com/labstack/echo/v4" @@ -13,17 +12,8 @@ import ( "github.com/gocarina/gocsv" ) -func PostMembres(c echo.Context) error { +func (h *Handler) PostMembres(c echo.Context) error { var response responses.PostMembresResponse - client, err := data.NewDataClientFromViper() - if err != nil { - response.StatusCode = http.StatusInternalServerError - response.Message = "Could not establish database connection" - response.Error = err.Error() - - return c.JSON(response.StatusCode, response) - } - defer client.DB.Close() var membres []models.Membre @@ -63,7 +53,7 @@ func PostMembres(c echo.Context) error { return c.JSON(response.StatusCode, response) } - newMembres, err := client.InsertMembres(membres) + newMembres, err := h.DataClient.InsertMembres(membres) if err != nil { response.StatusCode = http.StatusInternalServerError response.Message = "Could not insert membres" @@ -77,19 +67,9 @@ func PostMembres(c echo.Context) error { return c.JSON(response.StatusCode, response) } -func PostProgrammes(c echo.Context) error { +func (h *Handler) PostProgrammes(c echo.Context) error { var response responses.PostProgrammesResponse - client, err := data.NewDataClientFromViper() - if err != nil { - response.StatusCode = http.StatusInternalServerError - response.Message = "Could not establish database connection" - response.Error = err.Error() - - return c.JSON(response.StatusCode, response) - } - defer client.DB.Close() - var programmes []models.Programme switch c.Request().Header.Get("Content-Type") { @@ -134,7 +114,7 @@ func PostProgrammes(c echo.Context) error { return c.JSON(response.StatusCode, response) } - newProgrammes, err := client.InsertProgrammes(programmes) + newProgrammes, err := h.DataClient.InsertProgrammes(programmes) if err != nil { response.StatusCode = http.StatusInternalServerError response.Message = "Could not insert programmes" diff --git a/handlers/read.go b/handlers/read.go index 13a8007..5048f25 100644 --- a/handlers/read.go +++ b/handlers/read.go @@ -3,31 +3,13 @@ package handlers import ( "net/http" - "git.agecem.com/agecem/bottin/v5/data" "github.com/labstack/echo/v4" - "github.com/spf13/viper" ) -func ReadMembre(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(), - }) - } - +func (h *Handler) ReadMembre(c echo.Context) error { membreID := c.Param("membre_id") - membre, err := client.GetMembre(membreID) + membre, err := h.DataClient.GetMembre(membreID) if err != nil { if err.Error() == "No membre by that id was found" { return c.JSON(http.StatusNotFound, map[string]string{ diff --git a/handlers/seed.go b/handlers/seed.go index 947707d..27f5958 100644 --- a/handlers/seed.go +++ b/handlers/seed.go @@ -3,29 +3,11 @@ package handlers import ( "net/http" - "git.agecem.com/agecem/bottin/v5/data" "github.com/labstack/echo/v4" - "github.com/spf13/viper" ) -func PostSeed(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(), - }) - } - - rows, err := client.Seed() +func (h *Handler) PostSeed(c echo.Context) error { + rows, err := h.DataClient.Seed() if err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{ "message": "Seed failed", diff --git a/handlers/update.go b/handlers/update.go index a0d9ba2..8b4bd04 100644 --- a/handlers/update.go +++ b/handlers/update.go @@ -3,33 +3,15 @@ package handlers import ( "net/http" - "git.agecem.com/agecem/bottin/v5/data" "github.com/labstack/echo/v4" - "github.com/spf13/viper" ) -func PutMembrePreferedName(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(), - }) - } - +func (h *Handler) PutMembrePreferedName(c echo.Context) error { membreID := c.Param("membre_id") var newName string - err = c.Bind(&newName) + err := c.Bind(&newName) if err != nil { return c.JSON(http.StatusBadRequest, map[string]string{ "message": "Could not bind newName", @@ -37,7 +19,7 @@ func PutMembrePreferedName(c echo.Context) error { }) } - rows, err := client.UpdateMembreName(membreID, newName) + rows, err := h.DataClient.UpdateMembreName(membreID, newName) if err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{ "message": "Could not update membre name",