bottin-ag/webhandler/webhandler.go

109 lines
2.3 KiB
Go
Raw Normal View History

// Package webhandler provides handlers for the web app routes
package webhandler
2023-09-17 01:12:46 -04:00
import (
"log"
2023-09-17 01:12:46 -04:00
"net/http"
"git.agecem.com/agecem/bottin-ag/apiclient"
2023-09-17 01:12:46 -04:00
"git.agecem.com/agecem/bottin-ag/webresponse"
"github.com/labstack/echo/v4"
)
func DeclareRoutes(e *echo.Echo, h *WebHandler) {
e.GET("/", h.IndexGET)
e.POST("/scan/", h.ScanPOST)
2023-09-19 18:25:12 -04:00
e.GET("/decompte/", h.DecompteGET)
e.GET("/nothing/", h.NothingGET)
2023-09-17 01:12:46 -04:00
}
type WebHandler struct {
APIClient *apiclient.APIClient
2023-09-17 01:12:46 -04:00
}
func New(apiClient *apiclient.APIClient) WebHandler {
return WebHandler{
APIClient: apiClient,
}
2023-09-17 01:12:46 -04:00
}
func (w *WebHandler) IndexGET(c echo.Context) error {
var r webresponse.IndexGET
r.Message = "foo"
r.StatusCode = http.StatusOK
2023-09-19 18:25:12 -04:00
/*
decompteResponse, err := w.APIClient.GetDecompte()
if err != nil {
r.Error = err.Error()
r.Message = "Impossible d'obtenir le décompte"
r.StatusCode = http.StatusInternalServerError
return c.Render(r.StatusCode, "index-html", r)
}
r.Data.Decompte = decompteResponse.Data.Decompte
*/
2023-09-17 01:12:46 -04:00
return c.Render(r.StatusCode, "index-html", r)
}
func (w *WebHandler) ScanPOST(c echo.Context) error {
var r webresponse.ScanPOST
membreID := c.FormValue("membre_id")
//TODO
log.Println("membreID:", membreID)
r.StatusCode = http.StatusOK
2023-09-19 18:25:12 -04:00
/*
decompteResponse, err := w.APIClient.GetDecompte()
if err != nil {
r.Error = err.Error()
r.Message = "Impossible d'obtenir le décompte"
r.StatusCode = http.StatusInternalServerError
return c.Render(r.StatusCode, "index-html", r)
}
r.Data.Decompte = decompteResponse.Data.Decompte
*/
scanResponse, err := w.APIClient.Scan(membreID)
r.Error = scanResponse.Error
if err != nil {
r.Error = err.Error()
}
r.Message = scanResponse.Message
return c.Render(r.StatusCode, "scan-html", r)
}
2023-09-19 18:25:12 -04:00
func (w *WebHandler) DecompteGET(c echo.Context) error {
var r webresponse.DecompteGET
decompteResponse, err := w.APIClient.GetDecompte()
if err != nil {
r.Error = err.Error()
r.Message = "Impossible d'obtenir le décompte"
r.StatusCode = http.StatusInternalServerError
return c.Render(r.StatusCode, "decompte-html", r)
}
r.Data.Decompte = decompteResponse.Data.Decompte
r.StatusCode = http.StatusOK
r.Message = "ok"
return c.Render(r.StatusCode, "decompte-html", r)
}
func (w *WebHandler) NothingGET(c echo.Context) error {
return c.JSON(http.StatusOK, nil)
}