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 @@