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
|
@ -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
|
||||
}
|
||||
|
|
Reference in a new issue