bottin-agenda/web/webhandlers/handlers.go
Victor Lacasse-Beaudoin 660d8826e2 Implémenter POST /transaction
Ajouter data#ApiClient.InsertTransactions()

Fix form action

Ajouter séparateur entre description et formulaire

Ajouter webhandlers#PostTransaction et PostTransactionResult
2023-06-09 23:52:03 -04:00

76 lines
1.9 KiB
Go

package webhandlers
import (
"fmt"
"net/http"
"git.agecem.com/agecem/bottin-agenda/data"
"git.agecem.com/agecem/bottin-agenda/models"
"github.com/labstack/echo/v4"
"github.com/spf13/viper"
)
type PostTransactionResult struct {
Result string
}
func GetIndex(c echo.Context) error {
return c.Render(http.StatusOK, "index-html", nil)
}
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)
}