Réutiliser *data.DataClient à travers API handlers

This commit is contained in:
Victor Lacasse-Beaudoin 2023-09-18 22:55:40 -04:00
parent bf12cd123d
commit aa6f3479f6
7 changed files with 42 additions and 101 deletions

View file

@ -39,21 +39,7 @@ var apiCmd = &cobra.Command{
})) }))
} }
// Routes // DataClient
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
client, err := data.NewDataClientFromViper() client, err := data.NewDataClientFromViper()
if err != nil { if err != nil {
@ -71,6 +57,24 @@ var apiCmd = &cobra.Command{
log.Fatalf("Error during client.Seed(): %s", err) 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))) e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", apiPort)))
}, },
} }

11
handlers/handlers.go Normal file
View file

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

View file

@ -8,7 +8,7 @@ import (
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
func GetHealth(c echo.Context) error { func (h *Handler) GetHealth(c echo.Context) error {
var response responses.GetHealthResponse var response responses.GetHealthResponse
dataClient, err := data.NewDataClientFromViper() dataClient, err := data.NewDataClientFromViper()

View file

@ -5,7 +5,6 @@ import (
"io" "io"
"net/http" "net/http"
"git.agecem.com/agecem/bottin/v5/data"
"git.agecem.com/agecem/bottin/v5/models" "git.agecem.com/agecem/bottin/v5/models"
"git.agecem.com/agecem/bottin/v5/responses" "git.agecem.com/agecem/bottin/v5/responses"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
@ -13,17 +12,8 @@ import (
"github.com/gocarina/gocsv" "github.com/gocarina/gocsv"
) )
func PostMembres(c echo.Context) error { func (h *Handler) PostMembres(c echo.Context) error {
var response responses.PostMembresResponse 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 var membres []models.Membre
@ -63,7 +53,7 @@ func PostMembres(c echo.Context) error {
return c.JSON(response.StatusCode, response) return c.JSON(response.StatusCode, response)
} }
newMembres, err := client.InsertMembres(membres) newMembres, err := h.DataClient.InsertMembres(membres)
if err != nil { if err != nil {
response.StatusCode = http.StatusInternalServerError response.StatusCode = http.StatusInternalServerError
response.Message = "Could not insert membres" response.Message = "Could not insert membres"
@ -77,19 +67,9 @@ func PostMembres(c echo.Context) error {
return c.JSON(response.StatusCode, response) return c.JSON(response.StatusCode, response)
} }
func PostProgrammes(c echo.Context) error { func (h *Handler) PostProgrammes(c echo.Context) error {
var response responses.PostProgrammesResponse 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 var programmes []models.Programme
switch c.Request().Header.Get("Content-Type") { switch c.Request().Header.Get("Content-Type") {
@ -134,7 +114,7 @@ func PostProgrammes(c echo.Context) error {
return c.JSON(response.StatusCode, response) return c.JSON(response.StatusCode, response)
} }
newProgrammes, err := client.InsertProgrammes(programmes) newProgrammes, err := h.DataClient.InsertProgrammes(programmes)
if err != nil { if err != nil {
response.StatusCode = http.StatusInternalServerError response.StatusCode = http.StatusInternalServerError
response.Message = "Could not insert programmes" response.Message = "Could not insert programmes"

View file

@ -3,31 +3,13 @@ package handlers
import ( import (
"net/http" "net/http"
"git.agecem.com/agecem/bottin/v5/data"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/spf13/viper"
) )
func ReadMembre(c echo.Context) error { func (h *Handler) 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(),
})
}
membreID := c.Param("membre_id") membreID := c.Param("membre_id")
membre, err := client.GetMembre(membreID) membre, err := h.DataClient.GetMembre(membreID)
if err != nil { if err != nil {
if err.Error() == "No membre by that id was found" { if err.Error() == "No membre by that id was found" {
return c.JSON(http.StatusNotFound, map[string]string{ return c.JSON(http.StatusNotFound, map[string]string{

View file

@ -3,29 +3,11 @@ package handlers
import ( import (
"net/http" "net/http"
"git.agecem.com/agecem/bottin/v5/data"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/spf13/viper"
) )
func PostSeed(c echo.Context) error { func (h *Handler) PostSeed(c echo.Context) error {
connection := data.PostgresConnection{ rows, err := h.DataClient.Seed()
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()
if err != nil { if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{ return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Seed failed", "message": "Seed failed",

View file

@ -3,33 +3,15 @@ package handlers
import ( import (
"net/http" "net/http"
"git.agecem.com/agecem/bottin/v5/data"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/spf13/viper"
) )
func PutMembrePreferedName(c echo.Context) error { func (h *Handler) 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(),
})
}
membreID := c.Param("membre_id") membreID := c.Param("membre_id")
var newName string var newName string
err = c.Bind(&newName) err := c.Bind(&newName)
if err != nil { if err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{ return c.JSON(http.StatusBadRequest, map[string]string{
"message": "Could not bind newName", "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 { if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{ return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Could not update membre name", "message": "Could not update membre name",