Victor Lacasse-Beaudoin
a8dcdd0388
Tag v6.0.0 est sorti mais n'était pas réflété dans le code. BREAKING: API est maintenant exposé sur `/v6` et non `/v5`
129 lines
3.7 KiB
Go
129 lines
3.7 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/csv"
|
|
"io"
|
|
"net/http"
|
|
|
|
"git.agecem.com/agecem/bottin/v6/models"
|
|
"git.agecem.com/agecem/bottin/v6/responses"
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/gocarina/gocsv"
|
|
)
|
|
|
|
func (h *Handler) PostMembres(c echo.Context) error {
|
|
var response responses.PostMembresResponse
|
|
|
|
var membres []models.Membre
|
|
|
|
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":
|
|
body := c.Request().Body
|
|
if body == nil {
|
|
response.StatusCode = http.StatusBadRequest
|
|
response.Message = "Request body is empty"
|
|
return c.JSON(response.StatusCode, response)
|
|
}
|
|
defer body.Close()
|
|
|
|
// Parse the CSV data from the request body using gocsv.
|
|
if err := gocsv.Unmarshal(body, &membres); err != nil {
|
|
response.StatusCode = http.StatusBadRequest
|
|
response.Message = "Could not unmarshal into membres"
|
|
response.Error = err.Error()
|
|
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 {
|
|
response.StatusCode = http.StatusOK
|
|
response.Message = "Nothing to do"
|
|
return c.JSON(response.StatusCode, response)
|
|
}
|
|
|
|
newMembres, err := h.DataClient.InsertMembres(membres)
|
|
if err != nil {
|
|
response.StatusCode = http.StatusInternalServerError
|
|
response.Message = "Could not insert membres"
|
|
response.Error = err.Error()
|
|
return c.JSON(response.StatusCode, response)
|
|
}
|
|
|
|
response.StatusCode = http.StatusCreated
|
|
response.Message = "Insert successful"
|
|
response.Data.MembresInserted = newMembres
|
|
return c.JSON(response.StatusCode, response)
|
|
}
|
|
|
|
func (h *Handler) PostProgrammes(c echo.Context) error {
|
|
var response responses.PostProgrammesResponse
|
|
|
|
var programmes []models.Programme
|
|
|
|
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":
|
|
body := c.Request().Body
|
|
if body == nil {
|
|
response.StatusCode = http.StatusBadRequest
|
|
response.Message = "Request body is empty"
|
|
return c.JSON(response.StatusCode, response)
|
|
}
|
|
defer body.Close()
|
|
|
|
gocsv.SetCSVReader(func(in io.Reader) gocsv.CSVReader {
|
|
r := csv.NewReader(in)
|
|
r.Comma = ';'
|
|
return r // Allows use ; as delimiter
|
|
})
|
|
|
|
// Parse the CSV data from the request body using gocsv.
|
|
if err := gocsv.Unmarshal(body, &programmes); err != nil {
|
|
response.StatusCode = http.StatusBadRequest
|
|
response.Message = "Could not unmarshal into programmes"
|
|
response.Error = err.Error()
|
|
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 {
|
|
response.StatusCode = http.StatusOK
|
|
response.Message = "Nothing to do"
|
|
return c.JSON(response.StatusCode, response)
|
|
}
|
|
|
|
newProgrammes, err := h.DataClient.InsertProgrammes(programmes)
|
|
if err != nil {
|
|
response.StatusCode = http.StatusInternalServerError
|
|
response.Message = "Could not insert programmes"
|
|
response.Error = err.Error()
|
|
return c.JSON(response.StatusCode, response)
|
|
}
|
|
|
|
response.StatusCode = http.StatusCreated
|
|
response.Message = "Insert successful"
|
|
response.Data.ProgrammesInserted = newProgrammes
|
|
return c.JSON(response.StatusCode, response)
|
|
}
|