diff --git a/apiclient/apiclient.go b/apiclient/apiclient.go index bc05013..41a7a64 100644 --- a/apiclient/apiclient.go +++ b/apiclient/apiclient.go @@ -20,3 +20,7 @@ func (a *APIClient) Scan(membreID string) (response apiresponse.ScanPOST, err er func (a *APIClient) GetDecompte() (response apiresponse.DecompteGET, err error) { return response, a.Voki.Unmarshal(http.MethodGet, "/v0/decompte", nil, true, &response) } + +func (a *APIClient) GetPresences() (response apiresponse.PresencesGET, err error) { + return response, a.Voki.Unmarshal(http.MethodGet, "/v0/presences", nil, true, &response) +} diff --git a/apihandler/apihandler.go b/apihandler/apihandler.go index ce231b8..8e73ef6 100644 --- a/apihandler/apihandler.go +++ b/apihandler/apihandler.go @@ -18,6 +18,7 @@ func DeclareRoutes(e *echo.Group, h *APIHandler) { e.GET("/scan/:membre_id/", h.ScanGET) e.POST("/scan/:membre_id/", h.ScanPOST) e.GET("/decompte/", h.DecompteGET) + e.GET("/presences/", h.PresencesGET) } /* @@ -192,3 +193,24 @@ func (a *APIHandler) DecompteGET(c echo.Context) error { return c.JSON(r.StatusCode, r) } + +// PresencesGET is the handler for `GET /v:version/presences/ http/1.1` +func (a *APIHandler) PresencesGET(c echo.Context) error { + var r apiresponse.PresencesGET + + presences, err := a.DBClient.GetPresences() + if err != nil { + r.Error = err.Error() + r.StatusCode = http.StatusInternalServerError + r.Message = "Error during a.DBClient.GetPresences" + + return c.JSON(r.StatusCode, r) + } + + r.Data.Presences = presences + + r.StatusCode = http.StatusOK + r.Message = "ok" + + return c.JSON(r.StatusCode, r) +} diff --git a/apiresponse/apiresponse.go b/apiresponse/apiresponse.go index e507dfe..f7a1e5f 100644 --- a/apiresponse/apiresponse.go +++ b/apiresponse/apiresponse.go @@ -1,7 +1,10 @@ // Package apiresponse provides response types for API routes package apiresponse -import "codeberg.org/vlbeaudoin/voki/response" +import ( + "codeberg.org/vlbeaudoin/voki/response" + "git.agecem.com/agecem/bottin-ag/dbstruct" +) // HealthGET is the response type for `GET /v:version/health/ http/1.1` type HealthGET struct { @@ -31,3 +34,11 @@ type DecompteGET struct { Decompte int } } + +// PresencesGET is the response type for `GET /v:version/presences/ http/1.1` +type PresencesGET struct { + response.ResponseWithError + Data struct { + Presences []dbstruct.Presence + } +} diff --git a/dbclient/dbclient.go b/dbclient/dbclient.go index 421585b..7dcf738 100644 --- a/dbclient/dbclient.go +++ b/dbclient/dbclient.go @@ -78,3 +78,12 @@ func (d *DBClient) GetDecompte() (decompte int, err error) { err = d.DB.Get(&decompte, "SELECT COUNT(*) FROM presences;") return decompte, err } + +/* +GetPresences returns all rows in the `presences` table and any error encountered + +Can be used to export the database content at the end of the event. +*/ +func (d *DBClient) GetPresences() (presences []dbstruct.Presence, err error) { + return presences, d.DB.Select(&presences, "SELECT * FROM presences;") +}