From 6c09f1fa65b9451cc2ce270af797d16c35b6bf93 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Tue, 19 Sep 2023 18:25:12 -0400 Subject: [PATCH] =?UTF-8?q?Ajouter=20d=C3=A9compte?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apiclient/apiclient.go | 5 +++- apihandler/apihandler.go | 24 ++++++++++++++++ apiresponse/apiresponse.go | 8 ++++++ dbclient/dbclient.go | 6 ++++ webcontent/html/decompte.html | 4 +++ webcontent/html/index.html | 20 ++++++++++++++ webcontent/html/scan.html | 1 + webhandler/webhandler.go | 52 +++++++++++++++++++++++++++++++++++ webresponse/webresponse.go | 7 +++++ 9 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 webcontent/html/decompte.html diff --git a/apiclient/apiclient.go b/apiclient/apiclient.go index 081d95e..bc05013 100644 --- a/apiclient/apiclient.go +++ b/apiclient/apiclient.go @@ -14,6 +14,9 @@ type APIClient struct { } func (a *APIClient) Scan(membreID string) (response apiresponse.ScanPOST, err error) { - //TODO implement api key return response, a.Voki.Unmarshal(http.MethodPost, fmt.Sprintf("/v0/scan/%s", membreID), nil, true, &response) } + +func (a *APIClient) GetDecompte() (response apiresponse.DecompteGET, err error) { + return response, a.Voki.Unmarshal(http.MethodGet, "/v0/decompte", nil, true, &response) +} diff --git a/apihandler/apihandler.go b/apihandler/apihandler.go index 634cae5..ce231b8 100644 --- a/apihandler/apihandler.go +++ b/apihandler/apihandler.go @@ -17,6 +17,7 @@ func DeclareRoutes(e *echo.Group, h *APIHandler) { e.GET("/health/", h.HealthGET) e.GET("/scan/:membre_id/", h.ScanGET) e.POST("/scan/:membre_id/", h.ScanPOST) + e.GET("/decompte/", h.DecompteGET) } /* @@ -168,3 +169,26 @@ func (a *APIHandler) ScanPOST(c echo.Context) error { return c.JSON(r.StatusCode, r) } + +// DecompteGET is the handler for `GET /v:version/decompte/ http/1.1` +func (a *APIHandler) DecompteGET(c echo.Context) error { + var r apiresponse.DecompteGET + + r.Message = "not ok" + + decompte, err := a.DBClient.GetDecompte() + if err != nil { + r.Error = err.Error() + r.StatusCode = http.StatusInternalServerError + r.Message = "Error during a.DBClient.GetDecompte" + + return c.JSON(r.StatusCode, r) + } + + r.Data.Decompte = decompte + + r.StatusCode = http.StatusOK + r.Message = "ok" + + return c.JSON(r.StatusCode, r) +} diff --git a/apiresponse/apiresponse.go b/apiresponse/apiresponse.go index 6f5afe5..e507dfe 100644 --- a/apiresponse/apiresponse.go +++ b/apiresponse/apiresponse.go @@ -23,3 +23,11 @@ type ScanGET struct { type ScanPOST struct { response.ResponseWithError } + +// DecompteGET is the response type for `GET /v:version/decompte/ http/1.1` +type DecompteGET struct { + response.ResponseWithError + Data struct { + Decompte int + } +} diff --git a/dbclient/dbclient.go b/dbclient/dbclient.go index d6731e3..421585b 100644 --- a/dbclient/dbclient.go +++ b/dbclient/dbclient.go @@ -72,3 +72,9 @@ func (d *DBClient) InsertPresence(presence dbstruct.Presence) (dbstruct.Presence return insertedPresence, nil } + +// GetDecompte returns the row count of the `presences` table and any error encountered +func (d *DBClient) GetDecompte() (decompte int, err error) { + err = d.DB.Get(&decompte, "SELECT COUNT(*) FROM presences;") + return decompte, err +} diff --git a/webcontent/html/decompte.html b/webcontent/html/decompte.html new file mode 100644 index 0000000..bfd86ff --- /dev/null +++ b/webcontent/html/decompte.html @@ -0,0 +1,4 @@ +{{ define "decompte-html" }} + Décompte: {{ .Data.Decompte }} + +{{ end }} diff --git a/webcontent/html/index.html b/webcontent/html/index.html index c5f0e50..4b79e8b 100644 --- a/webcontent/html/index.html +++ b/webcontent/html/index.html @@ -6,6 +6,19 @@ AGECEM | Assemblée Générale +

Présences en Assemblée Générale

@@ -18,8 +31,15 @@ + + + +
+ + {{ end }} diff --git a/webcontent/html/scan.html b/webcontent/html/scan.html index d0b8c91..297cb4e 100644 --- a/webcontent/html/scan.html +++ b/webcontent/html/scan.html @@ -1,3 +1,4 @@ {{ define "scan-html" }}

{{ .Message }}

+ {{ if .Error }}

{{ .Error }}

{{ end }} {{ end }} diff --git a/webhandler/webhandler.go b/webhandler/webhandler.go index 36cff22..10ac173 100644 --- a/webhandler/webhandler.go +++ b/webhandler/webhandler.go @@ -13,6 +13,8 @@ import ( func DeclareRoutes(e *echo.Echo, h *WebHandler) { e.GET("/", h.IndexGET) e.POST("/scan/", h.ScanPOST) + e.GET("/decompte/", h.DecompteGET) + e.GET("/nothing/", h.NothingGET) } type WebHandler struct { @@ -31,6 +33,19 @@ func (w *WebHandler) IndexGET(c echo.Context) error { r.Message = "foo" r.StatusCode = http.StatusOK + /* + 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 + */ + return c.Render(r.StatusCode, "index-html", r) } @@ -44,6 +59,19 @@ func (w *WebHandler) ScanPOST(c echo.Context) error { r.StatusCode = http.StatusOK + /* + 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 { @@ -54,3 +82,27 @@ func (w *WebHandler) ScanPOST(c echo.Context) error { return c.Render(r.StatusCode, "scan-html", r) } + +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) +} diff --git a/webresponse/webresponse.go b/webresponse/webresponse.go index b2672b2..e95ff93 100644 --- a/webresponse/webresponse.go +++ b/webresponse/webresponse.go @@ -9,3 +9,10 @@ type IndexGET struct { type ScanPOST struct { response.ResponseWithError } + +type DecompteGET struct { + response.ResponseWithError + Data struct { + Decompte int + } +}