Merge branch 'refactor/post-responses' into main
This commit is contained in:
commit
a13bb131f4
3 changed files with 98 additions and 44 deletions
|
@ -5,87 +5,111 @@ import (
|
||||||
|
|
||||||
"git.agecem.com/agecem/bottin/v5/data"
|
"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"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
func PostMembres(c echo.Context) error {
|
func PostMembres(c echo.Context) error {
|
||||||
|
var response responses.PostMembresResponse
|
||||||
client, err := data.NewDataClientFromViper()
|
client, err := data.NewDataClientFromViper()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
response.StatusCode = http.StatusInternalServerError
|
||||||
"message": "Could not establish database connection",
|
response.Message = "Could not establish database connection"
|
||||||
"error": err.Error(),
|
response.Error = err.Error()
|
||||||
})
|
|
||||||
|
return c.JSON(response.StatusCode, response)
|
||||||
}
|
}
|
||||||
defer client.DB.Close()
|
defer client.DB.Close()
|
||||||
|
|
||||||
var membres []models.Membre
|
var membres []models.Membre
|
||||||
|
|
||||||
|
switch c.Request().Header.Get("Content-Type") {
|
||||||
|
case "application/json":
|
||||||
if err := c.Bind(&membres); err != nil {
|
if err := c.Bind(&membres); err != nil {
|
||||||
return c.JSON(http.StatusBadRequest, map[string]string{
|
response.StatusCode = http.StatusBadRequest
|
||||||
"message": "Could not bind membres",
|
response.Message = "Could not bind membres"
|
||||||
"error": err.Error(),
|
response.Error = err.Error()
|
||||||
})
|
return c.JSON(response.StatusCode, response)
|
||||||
|
}
|
||||||
|
case "text/csv":
|
||||||
|
response.StatusCode = http.StatusNotImplemented
|
||||||
|
response.Message = "Not Implemented"
|
||||||
|
return c.JSON(response.StatusCode, response)
|
||||||
|
default:
|
||||||
|
response.StatusCode = http.StatusBadRequest
|
||||||
|
response.Message = "Invalid Content-Type"
|
||||||
|
return c.JSON(response.StatusCode, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(membres) == 0 {
|
if len(membres) == 0 {
|
||||||
return c.JSON(http.StatusOK, map[string]string{
|
response.StatusCode = http.StatusOK
|
||||||
"message": "Nothing to do",
|
response.Message = "Nothing to do"
|
||||||
})
|
return c.JSON(response.StatusCode, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
newMembres, err := client.InsertMembres(membres)
|
newMembres, err := client.InsertMembres(membres)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
response.StatusCode = http.StatusInternalServerError
|
||||||
"message": "Could not insert membres",
|
response.Message = "Could not insert membres"
|
||||||
"error": err.Error(),
|
response.Error = err.Error()
|
||||||
})
|
return c.JSON(response.StatusCode, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.JSON(http.StatusCreated, map[string]interface{}{
|
response.StatusCode = http.StatusCreated
|
||||||
"message": "Insert successful",
|
response.Message = "Insert successful"
|
||||||
"data": map[string]interface{}{
|
response.Data.MembresInserted = newMembres
|
||||||
"membres": newMembres,
|
return c.JSON(response.StatusCode, response)
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func PostProgrammes(c echo.Context) error {
|
func PostProgrammes(c echo.Context) error {
|
||||||
|
var response responses.PostProgrammesResponse
|
||||||
|
|
||||||
client, err := data.NewDataClientFromViper()
|
client, err := data.NewDataClientFromViper()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
response.StatusCode = http.StatusInternalServerError
|
||||||
"message": "Could not establish database connection",
|
response.Message = "Could not establish database connection"
|
||||||
"error": err.Error(),
|
response.Error = err.Error()
|
||||||
})
|
|
||||||
|
return c.JSON(response.StatusCode, response)
|
||||||
}
|
}
|
||||||
defer client.DB.Close()
|
defer client.DB.Close()
|
||||||
|
|
||||||
var programmes []models.Programme
|
var programmes []models.Programme
|
||||||
|
|
||||||
|
switch c.Request().Header.Get("Content-Type") {
|
||||||
|
case "application/json":
|
||||||
if err := c.Bind(&programmes); err != nil {
|
if err := c.Bind(&programmes); err != nil {
|
||||||
return c.JSON(http.StatusBadRequest, map[string]string{
|
response.StatusCode = http.StatusBadRequest
|
||||||
"message": "Could not bind programmes",
|
response.Message = "Could not bind programmes"
|
||||||
"error": err.Error(),
|
response.Error = err.Error()
|
||||||
})
|
return c.JSON(response.StatusCode, response)
|
||||||
|
}
|
||||||
|
case "text/csv":
|
||||||
|
response.StatusCode = http.StatusNotImplemented
|
||||||
|
response.Message = "Not Implemented"
|
||||||
|
return c.JSON(response.StatusCode, response)
|
||||||
|
default:
|
||||||
|
response.StatusCode = http.StatusBadRequest
|
||||||
|
response.Message = "Invalid Content-Type"
|
||||||
|
return c.JSON(response.StatusCode, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(programmes) == 0 {
|
if len(programmes) == 0 {
|
||||||
return c.JSON(http.StatusOK, map[string]string{
|
response.StatusCode = http.StatusOK
|
||||||
"message": "Nothing to do",
|
response.Message = "Nothing to do"
|
||||||
})
|
return c.JSON(response.StatusCode, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
newProgrammes, err := client.InsertProgrammes(programmes)
|
newProgrammes, err := client.InsertProgrammes(programmes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
response.StatusCode = http.StatusInternalServerError
|
||||||
"message": "Could not insert programmes",
|
response.Message = "Could not insert programmes"
|
||||||
"error": err.Error(),
|
response.Error = err.Error()
|
||||||
})
|
return c.JSON(response.StatusCode, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.JSON(http.StatusCreated, map[string]interface{}{
|
response.StatusCode = http.StatusCreated
|
||||||
"message": "Insert successful",
|
response.Message = "Insert successful"
|
||||||
"data": map[string]interface{}{
|
response.Data.ProgrammesInserted = newProgrammes
|
||||||
"programmes": newProgrammes,
|
return c.JSON(response.StatusCode, response)
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
15
responses/post.go
Normal file
15
responses/post.go
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
package responses
|
||||||
|
|
||||||
|
type PostMembresResponse struct {
|
||||||
|
Response
|
||||||
|
Data struct {
|
||||||
|
MembresInserted int64
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type PostProgrammesResponse struct {
|
||||||
|
Response
|
||||||
|
Data struct {
|
||||||
|
ProgrammesInserted int64
|
||||||
|
}
|
||||||
|
}
|
15
responses/root.go
Normal file
15
responses/root.go
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
package responses
|
||||||
|
|
||||||
|
type Response struct {
|
||||||
|
StatusCode int
|
||||||
|
Message string
|
||||||
|
Error string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Responder interface {
|
||||||
|
Respond() Responder
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Response) Respond() Responder {
|
||||||
|
return r
|
||||||
|
}
|
Loading…
Reference in a new issue