Ajouter apihandler.ScanGET
Ajouter *bottin.Data#ApiClient à APIHandler Implémenter BottinStatus dans apihandler.HealthGET
This commit is contained in:
parent
916a83fc02
commit
309942921d
5 changed files with 93 additions and 5 deletions
|
@ -2,19 +2,22 @@
|
|||
package apihandler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"git.agecem.com/agecem/bottin-ag/apierror"
|
||||
"git.agecem.com/agecem/bottin-ag/apiresponse"
|
||||
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) {
|
||||
func New() (handler APIHandler) {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -22,14 +25,22 @@ func New() (handler *APIHandler) {
|
|||
APIHandler is the struct that implements the actual logic for the API server
|
||||
routes
|
||||
*/
|
||||
type APIHandler struct{}
|
||||
type APIHandler struct {
|
||||
BottinAPIClient *bottindata.ApiClient
|
||||
}
|
||||
|
||||
// HealthGET is the handler for `GET /v:version/health/ http/1.1`
|
||||
func (a *APIHandler) HealthGET(c echo.Context) error {
|
||||
var r apiresponse.HealthGET
|
||||
|
||||
// TODO
|
||||
r.Data.BottinStatus = "not implemented"
|
||||
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
|
||||
}
|
||||
|
||||
// TODO
|
||||
r.Data.DBStatus = "not implemented"
|
||||
|
@ -45,3 +56,41 @@ func (a *APIHandler) HealthGET(c echo.Context) error {
|
|||
|
||||
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)
|
||||
}
|
||||
|
|
Reference in a new issue