94 lines
2.3 KiB
Go
94 lines
2.3 KiB
Go
// Package apihandler provides handlers for API routes
|
|
package apihandler
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"git.agecem.com/agecem/bottin-ag/apiresponse"
|
|
"git.agecem.com/agecem/bottin-ag/dbclient"
|
|
bottindata "git.agecem.com/agecem/bottin/v5/data"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
// DeclareRoutes declares the API server endpoints for the specified Group
|
|
func DeclareRoutes(e *echo.Group, h *APIHandler) {
|
|
e.GET("/health/", h.HealthGET)
|
|
e.GET("/scan/:membre_id/", h.ScanGET)
|
|
}
|
|
|
|
func New() (handler APIHandler) {
|
|
return
|
|
}
|
|
|
|
/*
|
|
APIHandler is the struct that implements the actual logic for the API server
|
|
routes
|
|
*/
|
|
type APIHandler struct {
|
|
BottinAPIClient *bottindata.ApiClient
|
|
DBClient *dbclient.DBClient
|
|
}
|
|
|
|
// HealthGET is the handler for `GET /v:version/health/ http/1.1`
|
|
func (a *APIHandler) HealthGET(c echo.Context) error {
|
|
var r apiresponse.HealthGET
|
|
|
|
bottinStatus, err := a.BottinAPIClient.GetHealth()
|
|
if err != nil {
|
|
r.Message = "not ok"
|
|
r.StatusCode = http.StatusInternalServerError
|
|
r.Data.BottinStatus = err.Error()
|
|
} else {
|
|
r.Data.BottinStatus = bottinStatus
|
|
}
|
|
|
|
return c.JSON(r.StatusCode, r)
|
|
}
|
|
|
|
/*
|
|
ScanGET is the handler for `GET /v{version}/scan/{membre_id}/ http/1.1`
|
|
|
|
It returns the scanned status of a membre, without affecting the database.
|
|
*/
|
|
func (a *APIHandler) ScanGET(c echo.Context) error {
|
|
var r apiresponse.ScanGET
|
|
|
|
r.StatusCode = http.StatusOK
|
|
|
|
membreID := c.Param("membre_id")
|
|
|
|
membre, err := a.BottinAPIClient.GetMembre(membreID)
|
|
if err != nil {
|
|
switch err.Error() {
|
|
case "Ce numéro étudiant ne correspond à aucunE membre":
|
|
r.Message = fmt.Sprintf("%s n'est pas membre de l'AGECEM", membreID)
|
|
r.StatusCode = http.StatusNotFound
|
|
case "Veuillez fournir un numéro étudiant à rechercher":
|
|
r.Error = "membre_id ne peut pas être vide"
|
|
r.StatusCode = http.StatusBadRequest
|
|
r.Message = err.Error()
|
|
default:
|
|
r.Error = err.Error()
|
|
r.Message = "Erreur lors de BottinAPIClient.GetMembre"
|
|
r.StatusCode = http.StatusInternalServerError
|
|
|
|
return c.JSON(r.StatusCode, r)
|
|
}
|
|
}
|
|
|
|
if membre.ID == membreID && membre.ID != "" {
|
|
r.Message = fmt.Sprintf("%s est membre de l'AGECEM", membreID)
|
|
}
|
|
|
|
return c.JSON(r.StatusCode, r)
|
|
}
|
|
|
|
// ScanPOST is the handler for `POST /v{version}/scan/{membre_id}/ http/1.1`
|
|
func (a *APIHandler) ScanPOST(c echo.Context) error {
|
|
var r apiresponse.ScanPOST
|
|
|
|
_ = r
|
|
|
|
return nil
|
|
}
|