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..be809fb 100644 --- a/data/data.go +++ b/data/data.go @@ -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) { 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"` +}