Ajouter mécanisme de liste de transactions #7
4 changed files with 58 additions and 1 deletions
|
@ -61,6 +61,7 @@ var apiCmd = &cobra.Command{
|
|||
|
||||
e.GET("/v2/membres/:membre_id/", handlers.GetMembre)
|
||||
|
||||
e.GET("/v2/transactions/", handlers.GetTransactions)
|
||||
e.POST("/v2/transactions/", handlers.PostTransactions)
|
||||
|
||||
// Check bottin is ready
|
||||
|
|
|
@ -129,6 +129,15 @@ func (d *DataClient) GetTransaction(membreID string, is_perpetual bool) (models.
|
|||
return transaction, nil
|
||||
}
|
||||
|
||||
func (d *DataClient) ListTransactions() ([]models.Transaction, error) {
|
||||
var transactions []models.Transaction
|
||||
if err := d.DB.Select(&transactions, "SELECT * FROM transactions LIMIT 20000;"); err != nil {
|
||||
return transactions, err
|
||||
}
|
||||
|
||||
return transactions, nil
|
||||
}
|
||||
|
||||
/*
|
||||
// InsertMembres inserts a slice of Membre into a database, returning the amount inserted and any error encountered
|
||||
func (d *DataClient) InsertMembres(membres []models.Membre) (int64, error) {
|
||||
|
|
|
@ -10,6 +10,41 @@ import (
|
|||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// GetTransactions lists transactions on GET /v2/transactions http/1.1
|
||||
func GetTransactions(c echo.Context) error {
|
||||
var statusCode int = http.StatusInternalServerError
|
||||
var response responses.GetTransactionsResponse
|
||||
|
||||
client, err := data.NewDataClientFromViper()
|
||||
if err != nil {
|
||||
response.Message = fmt.Sprintf("Error during data.NewDataClientFromViper(): %s", err)
|
||||
|
||||
return c.JSON(statusCode, response)
|
||||
}
|
||||
defer client.DB.Close()
|
||||
|
||||
transactions, err := client.ListTransactions()
|
||||
response.Data.Transactions = transactions
|
||||
if err != nil {
|
||||
response.Message = fmt.Sprintf("Error during client.ListTransactions(): %s", err)
|
||||
|
||||
return c.JSON(statusCode, response)
|
||||
}
|
||||
|
||||
if len(transactions) == 0 {
|
||||
response.Message = "No transactions found"
|
||||
statusCode = http.StatusNotFound
|
||||
|
||||
return c.JSON(statusCode, response)
|
||||
}
|
||||
|
||||
response.Message = "List successful"
|
||||
statusCode = http.StatusOK
|
||||
|
||||
return c.JSON(statusCode, response)
|
||||
}
|
||||
|
||||
// PostTransactions creates transactions on POST /v2/transactions http/1.1
|
||||
func PostTransactions(c echo.Context) error {
|
||||
var statusCode int = http.StatusInternalServerError
|
||||
var response responses.PostTransactionsResponse
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package responses
|
||||
|
||||
import bottinmodels "git.agecem.com/agecem/bottin/v5/models"
|
||||
import (
|
||||
"git.agecem.com/agecem/bottin-agenda/models"
|
||||
bottinmodels "git.agecem.com/agecem/bottin/v5/models"
|
||||
)
|
||||
|
||||
type PostSeedResponse struct {
|
||||
Message string `json:"message"`
|
||||
|
@ -23,3 +26,12 @@ type PostTransactionsResponse struct {
|
|||
Message string `json:"message"`
|
||||
Data PostTransactionsResponseData `json:"data"`
|
||||
}
|
||||
|
||||
type GetTransactionsResponseData struct {
|
||||
Transactions []models.Transaction `json:"transactions"`
|
||||
}
|
||||
|
||||
type GetTransactionsResponse struct {
|
||||
Message string `json:"message"`
|
||||
Data GetTransactionsResponseData `json:"data"`
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue