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
-