Merge branch 'feature/apihandlers'
This commit is contained in:
commit
5f4c95b1d4
7 changed files with 42 additions and 101 deletions
34
cmd/api.go
34
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)))
|
||||
},
|
||||
}
|
||||
|
|
11
handlers/handlers.go
Normal file
11
handlers/handlers.go
Normal 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}
|
||||
}
|
|
@ -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()
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
Reference in a new issue