Merge branch 'feature/scan-post' into main
This commit is contained in:
commit
c22c20b306
3 changed files with 90 additions and 5 deletions
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
"git.agecem.com/agecem/bottin-ag/apiresponse"
|
||||
"git.agecem.com/agecem/bottin-ag/dbclient"
|
||||
"git.agecem.com/agecem/bottin-ag/dbstruct"
|
||||
bottindata "git.agecem.com/agecem/bottin/v5/data"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
@ -15,6 +16,7 @@ import (
|
|||
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)
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -97,6 +99,8 @@ func (a *APIHandler) ScanGET(c echo.Context) error {
|
|||
r.Message = fmt.Sprintf("%s est membre de l'AGECEM", membreID)
|
||||
}
|
||||
|
||||
//TODO remplir r.Data.IsScanned
|
||||
|
||||
return c.JSON(r.StatusCode, r)
|
||||
}
|
||||
|
||||
|
@ -104,7 +108,63 @@ func (a *APIHandler) ScanGET(c echo.Context) error {
|
|||
func (a *APIHandler) ScanPOST(c echo.Context) error {
|
||||
var r apiresponse.ScanPOST
|
||||
|
||||
_ = r
|
||||
membreID := c.Param("membre_id")
|
||||
|
||||
return nil
|
||||
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.Error = err.Error()
|
||||
r.StatusCode = http.StatusNotFound
|
||||
|
||||
return c.JSON(r.StatusCode, r)
|
||||
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()
|
||||
|
||||
return c.JSON(r.StatusCode, r)
|
||||
default:
|
||||
r.Error = err.Error()
|
||||
r.Message = "Erreur lors de BottinAPIClient.GetMembre"
|
||||
r.StatusCode = http.StatusInternalServerError
|
||||
|
||||
return c.JSON(r.StatusCode, r)
|
||||
}
|
||||
}
|
||||
|
||||
var presence dbstruct.Presence
|
||||
|
||||
presence.ID = membre.ID
|
||||
presence.ProgrammeID = membre.ProgrammeID
|
||||
|
||||
switch membre.PreferedName != "" {
|
||||
case true:
|
||||
presence.Name = membre.PreferedName
|
||||
case false:
|
||||
presence.Name = fmt.Sprintf("%s, %s", membre.LastName, membre.FirstName)
|
||||
}
|
||||
|
||||
insertedPresence, err := a.DBClient.InsertPresence(presence)
|
||||
if err != nil {
|
||||
r.Error = err.Error()
|
||||
r.Message = "Erreur lors de DBClient.InsertPresence"
|
||||
r.StatusCode = http.StatusInternalServerError
|
||||
|
||||
return c.JSON(r.StatusCode, r)
|
||||
}
|
||||
|
||||
if insertedPresence.ID == "" {
|
||||
r.Error = "Membre déjà enregistré·e"
|
||||
r.Message = fmt.Sprintf("Membre '%s' (%s) déjà enregistré·e.", presence.Name, presence.ID)
|
||||
r.StatusCode = http.StatusBadRequest
|
||||
|
||||
return c.JSON(r.StatusCode, r)
|
||||
}
|
||||
|
||||
r.StatusCode = http.StatusOK
|
||||
r.Message = fmt.Sprintf("Membre '%s' (%s) enregistré·e avec succès, veuillez lui donner son carton de vote. Bonne assemblée!", insertedPresence.Name, insertedPresence.ID)
|
||||
|
||||
return c.JSON(r.StatusCode, r)
|
||||
}
|
||||
|
|
|
@ -27,7 +27,4 @@ type ScanGET struct {
|
|||
// ScanPOST is the response type for `POST /v:version/scan/ http/1.1`
|
||||
type ScanPOST struct {
|
||||
Response
|
||||
Data struct {
|
||||
MembreID string
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"fmt"
|
||||
|
||||
"git.agecem.com/agecem/bottin-ag/dbschema"
|
||||
"git.agecem.com/agecem/bottin-ag/dbstruct"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
|
@ -44,3 +45,30 @@ func (d *DBClient) CreateTablesIfNotExist() error {
|
|||
_, err := d.DB.Exec(dbschema.Schema)
|
||||
return err
|
||||
}
|
||||
|
||||
/*
|
||||
InsertPresence inserts a dbstruct.Presence into the database, returning the
|
||||
values of the inserted presence and any error that occured.
|
||||
*/
|
||||
func (d *DBClient) InsertPresence(presence dbstruct.Presence) (dbstruct.Presence, error) {
|
||||
var insertedPresence dbstruct.Presence
|
||||
var rows *sqlx.Rows
|
||||
|
||||
rows, err := d.DB.NamedQuery(`
|
||||
INSERT INTO presences (id, at, name, programme_id)
|
||||
VALUES (:id, current_timestamp, :name, :programme_id)
|
||||
RETURNING id, at, name, programme_id
|
||||
`, presence)
|
||||
if err != nil {
|
||||
return insertedPresence, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
if err := rows.Scan(&insertedPresence.ID, &insertedPresence.At, &insertedPresence.Name, &insertedPresence.ProgrammeID); err != nil {
|
||||
return insertedPresence, err
|
||||
}
|
||||
}
|
||||
|
||||
return insertedPresence, nil
|
||||
}
|
||||
|
|
Reference in a new issue