bottin/handlers/insert.go

116 lines
3.3 KiB
Go
Raw Permalink Normal View History

2023-05-25 02:21:09 -04:00
package handlers
import (
"net/http"
"git.agecem.com/agecem/bottin/v5/data"
"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"
)
func PostMembres(c echo.Context) error {
var response responses.PostMembresResponse
client, err := data.NewDataClientFromViper()
2023-05-25 02:21:09 -04:00
if err != nil {
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
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":
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)
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 := client.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)
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 PostProgrammes(c echo.Context) error {
var response responses.PostProgrammesResponse
client, err := data.NewDataClientFromViper()
2023-05-25 03:44:41 -04:00
if err != nil {
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
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)
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 := client.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)
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
}