From afb944cbb34a876b8ca03f938c94b0fae8c19682 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Thu, 8 Jun 2023 00:53:05 -0400 Subject: [PATCH 1/2] Ajouter route de List Transactions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ajouter GET /v2/transactions http/1.1 Ajouter DataClient.ListTransactions() Ajouter handlers et models associƩs --- cmd/api.go | 1 + data/data.go | 17 +++++++++++++++++ handlers/transaction.go | 35 +++++++++++++++++++++++++++++++++++ responses/responses.go | 14 +++++++++++++- 4 files changed, 66 insertions(+), 1 deletion(-) diff --git a/cmd/api.go b/cmd/api.go index 95d232b..018fbc1 100644 --- a/cmd/api.go +++ b/cmd/api.go @@ -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 diff --git a/data/data.go b/data/data.go index b930332..f1e7a3e 100644 --- a/data/data.go +++ b/data/data.go @@ -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) { diff --git a/handlers/transaction.go b/handlers/transaction.go index d594c2c..b9141e1 100644 --- a/handlers/transaction.go +++ b/handlers/transaction.go @@ -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 diff --git a/responses/responses.go b/responses/responses.go index f703f97..1a9a212 100644 --- a/responses/responses.go +++ b/responses/responses.go @@ -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"` +} From 7946c0a606725357e21b41e324f0fa834e71ce42 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Thu, 8 Jun 2023 00:54:45 -0400 Subject: [PATCH 2/2] Retirer commentaire --- data/data.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/data/data.go b/data/data.go index f1e7a3e..be809fb 100644 --- a/data/data.go +++ b/data/data.go @@ -136,14 +136,6 @@ func (d *DataClient) ListTransactions() ([]models.Transaction, error) { } return transactions, nil - - /* - rows, err := d.DB.Queryx("SELECT * FROM transactions LIMIT 20000;") - if err != nil { - return transactions, err - } - */ - } /*