Implémenter responses POST pour application/json
This commit is contained in:
parent
6faca0e708
commit
59eeb7a38a
1 changed files with 68 additions and 44 deletions
|
@ -5,87 +5,111 @@ import (
|
|||
|
||||
"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"
|
||||
)
|
||||
|
||||
func PostMembres(c echo.Context) error {
|
||||
var response responses.PostMembresResponse
|
||||
client, err := data.NewDataClientFromViper()
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
||||
"message": "Could not establish database connection",
|
||||
"error": err.Error(),
|
||||
})
|
||||
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
|
||||
|
||||
if err := c.Bind(&membres); err != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{
|
||||
"message": "Could not bind membres",
|
||||
"error": err.Error(),
|
||||
})
|
||||
switch c.Request().Header.Get("Content-Type") {
|
||||
case "application/json":
|
||||
if err := c.Bind(&membres); err != nil {
|
||||
response.StatusCode = http.StatusBadRequest
|
||||
response.Message = "Could not bind membres"
|
||||
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 {
|
||||
return c.JSON(http.StatusOK, map[string]string{
|
||||
"message": "Nothing to do",
|
||||
})
|
||||
response.StatusCode = http.StatusOK
|
||||
response.Message = "Nothing to do"
|
||||
return c.JSON(response.StatusCode, response)
|
||||
}
|
||||
|
||||
newMembres, err := client.InsertMembres(membres)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
||||
"message": "Could not insert membres",
|
||||
"error": err.Error(),
|
||||
})
|
||||
response.StatusCode = http.StatusInternalServerError
|
||||
response.Message = "Could not insert membres"
|
||||
response.Error = err.Error()
|
||||
return c.JSON(response.StatusCode, response)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusCreated, map[string]interface{}{
|
||||
"message": "Insert successful",
|
||||
"data": map[string]interface{}{
|
||||
"membres": newMembres,
|
||||
},
|
||||
})
|
||||
response.StatusCode = http.StatusCreated
|
||||
response.Message = "Insert successful"
|
||||
response.Data.MembresInserted = newMembres
|
||||
return c.JSON(response.StatusCode, response)
|
||||
}
|
||||
|
||||
func PostProgrammes(c echo.Context) error {
|
||||
var response responses.PostProgrammesResponse
|
||||
|
||||
client, err := data.NewDataClientFromViper()
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
||||
"message": "Could not establish database connection",
|
||||
"error": err.Error(),
|
||||
})
|
||||
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
|
||||
|
||||
if err := c.Bind(&programmes); err != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{
|
||||
"message": "Could not bind programmes",
|
||||
"error": err.Error(),
|
||||
})
|
||||
switch c.Request().Header.Get("Content-Type") {
|
||||
case "application/json":
|
||||
if err := c.Bind(&programmes); err != nil {
|
||||
response.StatusCode = http.StatusBadRequest
|
||||
response.Message = "Could not bind programmes"
|
||||
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 {
|
||||
return c.JSON(http.StatusOK, map[string]string{
|
||||
"message": "Nothing to do",
|
||||
})
|
||||
response.StatusCode = http.StatusOK
|
||||
response.Message = "Nothing to do"
|
||||
return c.JSON(response.StatusCode, response)
|
||||
}
|
||||
|
||||
newProgrammes, err := client.InsertProgrammes(programmes)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
||||
"message": "Could not insert programmes",
|
||||
"error": err.Error(),
|
||||
})
|
||||
response.StatusCode = http.StatusInternalServerError
|
||||
response.Message = "Could not insert programmes"
|
||||
response.Error = err.Error()
|
||||
return c.JSON(response.StatusCode, response)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusCreated, map[string]interface{}{
|
||||
"message": "Insert successful",
|
||||
"data": map[string]interface{}{
|
||||
"programmes": newProgrammes,
|
||||
},
|
||||
})
|
||||
response.StatusCode = http.StatusCreated
|
||||
response.Message = "Insert successful"
|
||||
response.Data.ProgrammesInserted = newProgrammes
|
||||
return c.JSON(response.StatusCode, response)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue