Implémenter client web #19

Merged
vlbeaudoin merged 4 commits from feature/web into main 2023-06-10 00:10:26 -04:00
5 changed files with 100 additions and 2 deletions
Showing only changes of commit 660d8826e2 - Show all commits

View file

@ -25,6 +25,7 @@ var apiCmd = &cobra.Command{
Short: "Démarrer le serveur API", Short: "Démarrer le serveur API",
Args: cobra.ExactArgs(0), Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// TODO migrer à viper.Unmarshal(&models.Config)
apiKey = viper.GetString("api.key") apiKey = viper.GetString("api.key")
apiPort = viper.GetInt("api.port") apiPort = viper.GetInt("api.port")

View file

@ -75,7 +75,7 @@ var webCmd = &cobra.Command{
// Routes // Routes
e.GET("/", webhandlers.GetIndex) e.GET("/", webhandlers.GetIndex)
//e.POST("/transaction", webhandlers.PostTransaction) e.POST("/transaction/", webhandlers.PostTransaction)
// Execution // Execution

View file

@ -1,6 +1,7 @@
package data package data
import ( import (
"bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -8,6 +9,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"git.agecem.com/agecem/bottin-agenda/models"
"git.agecem.com/agecem/bottin-agenda/responses" "git.agecem.com/agecem/bottin-agenda/responses"
) )
@ -90,3 +92,31 @@ func (a *ApiClient) GetHealth() (string, error) {
return response.Message, nil return response.Message, nil
} }
func (a *ApiClient) InsertTransactions(transactions []models.Transaction) ([]models.Transaction, error) {
var response responses.PostTransactionsResponse
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(transactions)
if err != nil {
return response.Data.Transactions, err
}
postHealthResponse, err := a.Call(http.MethodPost, "/v3/transactions", &buf, true)
defer postHealthResponse.Body.Close()
body, err := ioutil.ReadAll(postHealthResponse.Body)
if err != nil {
return response.Data.Transactions, err
}
if err := json.Unmarshal(body, &response); err != nil {
return response.Data.Transactions, err
}
if len(response.Data.Transactions) == 0 {
return response.Data.Transactions, fmt.Errorf(response.Message)
}
return response.Data.Transactions, nil
}

View file

@ -94,7 +94,9 @@ button {
4) Si aucune erreur ne survient, la personne est libre de partir avec son agenda 4) Si aucune erreur ne survient, la personne est libre de partir avec son agenda
</p> </p>
<form action="/transaction" method="post"> <hr>
<form action="/transaction/" method="post">
<ul> <ul>
<li> <li>
<label for="is_perpetual">Perpétuel?:</label> <label for="is_perpetual">Perpétuel?:</label>

View file

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