2023-05-25 02:21:09 -04:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2023-09-05 18:03:28 -04:00
|
|
|
"encoding/csv"
|
|
|
|
"io"
|
2023-05-25 02:21:09 -04:00
|
|
|
"net/http"
|
|
|
|
|
2023-06-02 17:46:57 -04:00
|
|
|
"git.agecem.com/agecem/bottin/v5/data"
|
|
|
|
"git.agecem.com/agecem/bottin/v5/models"
|
2023-09-05 16:15:40 -04:00
|
|
|
"git.agecem.com/agecem/bottin/v5/responses"
|
2023-05-25 02:21:09 -04:00
|
|
|
"github.com/labstack/echo/v4"
|
2023-09-05 18:03:28 -04:00
|
|
|
|
|
|
|
"github.com/gocarina/gocsv"
|
2023-05-25 02:21:09 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func PostMembres(c echo.Context) error {
|
2023-09-05 16:15:40 -04:00
|
|
|
var response responses.PostMembresResponse
|
2023-06-03 19:33:56 -04:00
|
|
|
client, err := data.NewDataClientFromViper()
|
2023-05-25 02:21:09 -04:00
|
|
|
if err != nil {
|
2023-09-05 16:15:40 -04:00
|
|
|
response.StatusCode = http.StatusInternalServerError
|
|
|
|
response.Message = "Could not establish database connection"
|
|
|
|
response.Error = err.Error()
|
|
|
|
|
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-05-25 02:21:09 -04:00
|
|
|
}
|
2023-06-08 19:47:25 -04:00
|
|
|
defer client.DB.Close()
|
2023-05-25 02:21:09 -04:00
|
|
|
|
2023-05-25 03:23:07 -04:00
|
|
|
var membres []models.Membre
|
2023-05-25 02:21:09 -04:00
|
|
|
|
2023-09-05 16:15:40 -04:00
|
|
|
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":
|
2023-09-05 18:03:28 -04:00
|
|
|
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)
|
|
|
|
}
|
2023-09-05 16:15:40 -04:00
|
|
|
default:
|
|
|
|
response.StatusCode = http.StatusBadRequest
|
|
|
|
response.Message = "Invalid Content-Type"
|
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-05-25 02:21:09 -04:00
|
|
|
}
|
|
|
|
|
2023-05-25 03:23:07 -04:00
|
|
|
if len(membres) == 0 {
|
2023-09-05 16:15:40 -04:00
|
|
|
response.StatusCode = http.StatusOK
|
|
|
|
response.Message = "Nothing to do"
|
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-05-25 02:21:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
newMembres, err := client.InsertMembres(membres)
|
|
|
|
if err != nil {
|
2023-09-05 16:15:40 -04:00
|
|
|
response.StatusCode = http.StatusInternalServerError
|
|
|
|
response.Message = "Could not insert membres"
|
|
|
|
response.Error = err.Error()
|
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-05-25 02:21:09 -04:00
|
|
|
}
|
|
|
|
|
2023-09-05 16:15:40 -04:00
|
|
|
response.StatusCode = http.StatusCreated
|
|
|
|
response.Message = "Insert successful"
|
|
|
|
response.Data.MembresInserted = newMembres
|
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-05-25 02:21:09 -04:00
|
|
|
}
|
2023-05-25 03:44:41 -04:00
|
|
|
|
|
|
|
func PostProgrammes(c echo.Context) error {
|
2023-09-05 16:15:40 -04:00
|
|
|
var response responses.PostProgrammesResponse
|
|
|
|
|
2023-06-03 19:33:56 -04:00
|
|
|
client, err := data.NewDataClientFromViper()
|
2023-05-25 03:44:41 -04:00
|
|
|
if err != nil {
|
2023-09-05 16:15:40 -04:00
|
|
|
response.StatusCode = http.StatusInternalServerError
|
|
|
|
response.Message = "Could not establish database connection"
|
|
|
|
response.Error = err.Error()
|
|
|
|
|
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-05-25 03:44:41 -04:00
|
|
|
}
|
2023-06-08 19:47:25 -04:00
|
|
|
defer client.DB.Close()
|
2023-05-25 03:44:41 -04:00
|
|
|
|
|
|
|
var programmes []models.Programme
|
|
|
|
|
2023-09-05 16:15:40 -04:00
|
|
|
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":
|
2023-09-05 18:03:28 -04:00
|
|
|
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)
|
|
|
|
}
|
2023-09-05 16:15:40 -04:00
|
|
|
default:
|
|
|
|
response.StatusCode = http.StatusBadRequest
|
|
|
|
response.Message = "Invalid Content-Type"
|
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-05-25 03:44:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(programmes) == 0 {
|
2023-09-05 16:15:40 -04:00
|
|
|
response.StatusCode = http.StatusOK
|
|
|
|
response.Message = "Nothing to do"
|
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-05-25 03:44:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
newProgrammes, err := client.InsertProgrammes(programmes)
|
|
|
|
if err != nil {
|
2023-09-05 16:15:40 -04:00
|
|
|
response.StatusCode = http.StatusInternalServerError
|
|
|
|
response.Message = "Could not insert programmes"
|
|
|
|
response.Error = err.Error()
|
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-05-25 03:44:41 -04:00
|
|
|
}
|
|
|
|
|
2023-09-05 16:15:40 -04:00
|
|
|
response.StatusCode = http.StatusCreated
|
|
|
|
response.Message = "Insert successful"
|
|
|
|
response.Data.ProgrammesInserted = newProgrammes
|
|
|
|
return c.JSON(response.StatusCode, response)
|
2023-05-25 03:44:41 -04:00
|
|
|
}
|