2023-06-09 01:09:02 -04:00
|
|
|
package webhandlers
|
|
|
|
|
|
|
|
import (
|
2023-06-09 23:52:03 -04:00
|
|
|
"fmt"
|
2023-06-09 01:09:02 -04:00
|
|
|
"net/http"
|
|
|
|
|
2023-06-09 23:52:03 -04:00
|
|
|
"git.agecem.com/agecem/bottin-agenda/data"
|
|
|
|
"git.agecem.com/agecem/bottin-agenda/models"
|
2023-06-09 01:09:02 -04:00
|
|
|
"github.com/labstack/echo/v4"
|
2023-06-09 23:52:03 -04:00
|
|
|
"github.com/spf13/viper"
|
2023-06-09 01:09:02 -04:00
|
|
|
)
|
|
|
|
|
2023-06-09 23:52:03 -04:00
|
|
|
type PostTransactionResult struct {
|
|
|
|
Result string
|
|
|
|
}
|
|
|
|
|
2023-06-09 01:09:02 -04:00
|
|
|
func GetIndex(c echo.Context) error {
|
|
|
|
return c.Render(http.StatusOK, "index-html", nil)
|
|
|
|
}
|
2023-06-09 23:52:03 -04:00
|
|
|
|
|
|
|
func PostTransaction(c echo.Context) error {
|
|
|
|
var config models.Config
|
|
|
|
var result PostTransactionResult
|
|
|
|
|
|
|
|
err := viper.Unmarshal(&config)
|
|
|
|
if err != nil {
|
|
|
|
return c.Render(http.StatusInternalServerError, "index-html", PostTransactionResult{
|
|
|
|
Result: fmt.Sprintln("👎", err.Error()),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// apiClient := data.NewApiClientkey host protocol port
|
|
|
|
apiClient := data.NewApiClient(
|
|
|
|
config.Web.Api.Key,
|
|
|
|
config.Web.Api.Host,
|
|
|
|
config.Web.Api.Protocol,
|
|
|
|
config.Web.Api.Port,
|
|
|
|
)
|
|
|
|
|
|
|
|
apiHealth, err := apiClient.GetHealth()
|
|
|
|
if err != nil {
|
|
|
|
result.Result = fmt.Sprintf("👎 %s | %s", apiHealth, err)
|
|
|
|
|
|
|
|
return c.Render(http.StatusInternalServerError, "index-html", result)
|
|
|
|
}
|
|
|
|
|
|
|
|
isPerpetual := c.FormValue("is_perpetual") == "on"
|
|
|
|
|
|
|
|
membreId := c.FormValue("membre_id")
|
|
|
|
|
|
|
|
if membreId == "" {
|
|
|
|
result.Result = "👎 Aucun numéro étudiant sélectionné. Assurez-vous de cliquer sur la case 'Numéro étudiant:' avant de scanner."
|
|
|
|
|
|
|
|
return c.Render(http.StatusBadRequest, "index-html", result)
|
|
|
|
}
|
|
|
|
|
|
|
|
transactions, err := apiClient.InsertTransactions(
|
|
|
|
[]models.Transaction{{
|
|
|
|
MembreID: membreId,
|
|
|
|
IsPerpetual: isPerpetual,
|
|
|
|
}})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
result.Result = fmt.Sprintf("👎 Erreur lors de l'insertion: %s", err)
|
|
|
|
|
|
|
|
return c.Render(http.StatusInternalServerError, "index-html", result)
|
|
|
|
}
|
|
|
|
|
|
|
|
agenda := "non-perpétuel"
|
|
|
|
if transactions[0].IsPerpetual {
|
|
|
|
agenda = "perpétuel"
|
|
|
|
}
|
|
|
|
result.Result = fmt.Sprintf("👍 Membre %s peut recevoir son agenda %s", transactions[0].MembreID, agenda)
|
|
|
|
|
|
|
|
return c.Render(http.StatusOK, "index-html", result)
|
|
|
|
}
|