bottin/handlers/insert.go

130 lines
3.7 KiB
Go
Raw Normal View History

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"
"git.agecem.com/agecem/bottin/v5/models"
"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 (h *Handler) PostMembres(c echo.Context) error {
var response responses.PostMembresResponse
2023-05-25 02:21:09 -04:00
var membres []models.Membre
2023-05-25 02:21:09 -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)
}
default:
response.StatusCode = http.StatusBadRequest
response.Message = "Invalid Content-Type"
return c.JSON(response.StatusCode, response)
2023-05-25 02:21:09 -04:00
}
if len(membres) == 0 {
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 := h.DataClient.InsertMembres(membres)
2023-05-25 02:21:09 -04:00
if err != nil {
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
}
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 (h *Handler) PostProgrammes(c echo.Context) error {
var response responses.PostProgrammesResponse
2023-05-25 03:44:41 -04:00
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":
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)
}
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 {
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 := h.DataClient.InsertProgrammes(programmes)
2023-05-25 03:44:41 -04:00
if err != nil {
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
}
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
}