Implémenter POST /transaction
Ajouter data#ApiClient.InsertTransactions() Fix form action Ajouter séparateur entre description et formulaire Ajouter webhandlers#PostTransaction et PostTransactionResult
This commit is contained in:
parent
2287f00e29
commit
660d8826e2
5 changed files with 100 additions and 2 deletions
|
@ -25,6 +25,7 @@ var apiCmd = &cobra.Command{
|
|||
Short: "Démarrer le serveur API",
|
||||
Args: cobra.ExactArgs(0),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// TODO migrer à viper.Unmarshal(&models.Config)
|
||||
apiKey = viper.GetString("api.key")
|
||||
apiPort = viper.GetInt("api.port")
|
||||
|
||||
|
|
|
@ -75,7 +75,7 @@ var webCmd = &cobra.Command{
|
|||
// Routes
|
||||
|
||||
e.GET("/", webhandlers.GetIndex)
|
||||
//e.POST("/transaction", webhandlers.PostTransaction)
|
||||
e.POST("/transaction/", webhandlers.PostTransaction)
|
||||
|
||||
// Execution
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package data
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
@ -8,6 +9,7 @@ import (
|
|||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"git.agecem.com/agecem/bottin-agenda/models"
|
||||
"git.agecem.com/agecem/bottin-agenda/responses"
|
||||
)
|
||||
|
||||
|
@ -90,3 +92,31 @@ func (a *ApiClient) GetHealth() (string, error) {
|
|||
|
||||
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
|
||||
}
|
||||
|
|
|
@ -94,7 +94,9 @@ button {
|
|||
4) Si aucune erreur ne survient, la personne est libre de partir avec son agenda
|
||||
</p>
|
||||
|
||||
<form action="/transaction" method="post">
|
||||
<hr>
|
||||
|
||||
<form action="/transaction/" method="post">
|
||||
<ul>
|
||||
<li>
|
||||
<label for="is_perpetual">Perpétuel?:</label>
|
||||
|
|
|
@ -1,11 +1,76 @@
|
|||
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)
|
||||
}
|
||||
|
|
Reference in a new issue