bottin/routes.go
Victor Lacasse-Beaudoin c7c64674c7 rework: change api prefix to /api/v7/
- add and test GetMembre
- add `IsMembreID(string) bool` function

BREAKING: Rename routes to `/api/v7/...` scheme
2024-06-18 19:44:20 -04:00

170 lines
4.6 KiB
Go

package main
import (
"fmt"
"net/http"
"codeberg.org/vlbeaudoin/pave/v2"
"codeberg.org/vlbeaudoin/voki/v3"
"github.com/labstack/echo/v4"
)
func addRoutes(e *echo.Echo, db *PostgresClient) error {
_ = db
apiPath := "/api/v7"
apiGroup := e.Group(apiPath)
p := pave.New()
if err := pave.EchoRegister[HealthGETRequest](
apiGroup,
&p,
apiPath,
http.MethodGet,
"/health/",
"Get API server health",
"HealthGET", func(c echo.Context) error {
var request, response = HealthGETRequest{}, HealthGETResponse{}
if !request.Complete() {
var response voki.ResponseBadRequest
response.Message = "Incomplete HealthGET request received"
return c.JSON(response.StatusCode(), response)
}
if err := response.SetStatusCode(http.StatusOK); err != nil {
var response voki.ResponseInternalServerError
response.Message = fmt.Sprintf("handler: %s", err)
return c.JSON(response.StatusCode(), response)
}
response.Message = "ok"
return c.JSON(response.StatusCode(), response)
}); err != nil {
return err
}
if err := pave.EchoRegister[ProgrammesPOSTRequest](
apiGroup,
&p,
apiPath,
http.MethodPost,
"/programme/",
"Insert programmes",
"ProgrammesPOST", func(c echo.Context) error {
var request, response = ProgrammesPOSTRequest{}, ProgrammesPOSTResponse{}
if err := c.Bind(&request.Data); err != nil {
var response voki.ResponseBadRequest
response.Message = fmt.Sprintf("parse request body: %s", err)
return c.JSON(response.StatusCode(), response)
}
if !request.Complete() {
var response voki.ResponseBadRequest
response.Message = "Incomplete ProgrammesPOST request received"
return c.JSON(response.StatusCode(), response)
}
amountInserted, err := db.InsertProgrammes(request.Data.Programmes...)
if err != nil {
var response voki.ResponseBadRequest
response.Message = fmt.Sprintf("db: %s", err)
return c.JSON(response.StatusCode(), response)
}
response.Data.ProgrammesInserted = amountInserted
if err := response.SetStatusCode(http.StatusOK); err != nil {
var response voki.ResponseInternalServerError
response.Message = fmt.Sprintf("handler: %s", err)
return c.JSON(response.StatusCode(), response)
}
response.Message = "ok"
return c.JSON(response.StatusCode(), response)
}); err != nil {
return err
}
if err := pave.EchoRegister[MembresPOSTRequest](
apiGroup,
&p,
apiPath,
http.MethodPost,
"/membre/",
"Insert membres",
"MembresPOST", func(c echo.Context) error {
var request, response = MembresPOSTRequest{}, MembresPOSTResponse{}
if err := c.Bind(&request.Data); err != nil {
var response voki.ResponseBadRequest
response.Message = fmt.Sprintf("parse request body: %s", err)
return c.JSON(response.StatusCode(), response)
}
if !request.Complete() {
var response voki.ResponseBadRequest
response.Message = "Incomplete MembresPOST request received"
return c.JSON(response.StatusCode(), response)
}
amountInserted, err := db.InsertMembres(request.Data.Membres...)
if err != nil {
var response voki.ResponseBadRequest
response.Message = fmt.Sprintf("db: %s", err)
return c.JSON(response.StatusCode(), response)
}
response.Data.MembresInserted = amountInserted
if err := response.SetStatusCode(http.StatusOK); err != nil {
var response voki.ResponseInternalServerError
response.Message = fmt.Sprintf("handler: %s", err)
return c.JSON(response.StatusCode(), response)
}
response.Message = "ok"
return c.JSON(response.StatusCode(), response)
}); err != nil {
return err
}
if err := pave.EchoRegister[MembreGETRequest](
apiGroup,
&p,
apiPath,
http.MethodGet,
"/membre/:membre_id/",
"Get membre",
"MembreGET", func(c echo.Context) error {
var request, response = MembreGETRequest{}, MembreGETResponse{}
request.Param.MembreID = c.Param("membre_id")
if !request.Complete() {
var response voki.ResponseBadRequest
response.Message = "Incomplete MembreGET request received"
return c.JSON(response.StatusCode(), response)
}
membre, err := db.GetMembre(request.Param.MembreID)
if err != nil {
var response voki.ResponseBadRequest
response.Message = fmt.Sprintf("db: %s", err)
return c.JSON(response.StatusCode(), response)
}
response.Data.Membre = membre
if err := response.SetStatusCode(http.StatusOK); err != nil {
var response voki.ResponseInternalServerError
response.Message = fmt.Sprintf("handler: %s", err)
return c.JSON(response.StatusCode(), response)
}
response.Message = "ok"
return c.JSON(response.StatusCode(), response)
}); err != nil {
return err
}
return nil
}