Ajouter route de List Transactions

Ajouter GET /v2/transactions http/1.1

Ajouter DataClient.ListTransactions()

Ajouter handlers et models associés
This commit is contained in:
Victor Lacasse-Beaudoin 2023-06-08 00:53:05 -04:00
parent 547833cfdb
commit afb944cbb3
4 changed files with 66 additions and 1 deletions

View file

@ -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

View file

@ -129,6 +129,23 @@ 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
/*
rows, err := d.DB.Queryx("SELECT * FROM transactions LIMIT 20000;")
if err != nil {
return transactions, err
}
*/
}
/*
// 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) {

View file

@ -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

View file

@ -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"`
}