From 660d8826e2402b1bfbce3595950c2e5c8149c061 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Fri, 9 Jun 2023 23:52:03 -0400 Subject: [PATCH] =?UTF-8?q?Impl=C3=A9menter=20POST=20/transaction?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ajouter data#ApiClient.InsertTransactions() Fix form action Ajouter séparateur entre description et formulaire Ajouter webhandlers#PostTransaction et PostTransactionResult --- cmd/api.go | 1 + cmd/web.go | 2 +- data/apiclient.go | 30 +++++++++++++++++ web/templates/index.html | 4 ++- web/webhandlers/handlers.go | 65 +++++++++++++++++++++++++++++++++++++ 5 files changed, 100 insertions(+), 2 deletions(-) diff --git a/cmd/api.go b/cmd/api.go index 224df3c..ad702dc 100644 --- a/cmd/api.go +++ b/cmd/api.go @@ -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") diff --git a/cmd/web.go b/cmd/web.go index 45274da..ed1d7b3 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -75,7 +75,7 @@ var webCmd = &cobra.Command{ // Routes e.GET("/", webhandlers.GetIndex) - //e.POST("/transaction", webhandlers.PostTransaction) + e.POST("/transaction/", webhandlers.PostTransaction) // Execution diff --git a/data/apiclient.go b/data/apiclient.go index f56979f..0971ba3 100644 --- a/data/apiclient.go +++ b/data/apiclient.go @@ -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 +} diff --git a/web/templates/index.html b/web/templates/index.html index af30688..9750a38 100644 --- a/web/templates/index.html +++ b/web/templates/index.html @@ -94,7 +94,9 @@ button { 4) Si aucune erreur ne survient, la personne est libre de partir avec son agenda

-
+
+ +