Skip to content

Commit cfabbf4

Browse files
authored
feat: sort transactions from newest to oldest (#262)
1 parent 8d51e27 commit cfabbf4

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

pkg/controllers/transaction.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func CreateTransaction(c *gin.Context) {
152152
func GetTransactions(c *gin.Context) {
153153
var transactions []models.Transaction
154154

155-
database.DB.Find(&transactions)
155+
database.DB.Order("date(date) DESC").Find(&transactions)
156156

157157
// When there are no resources, we want an empty list, not null
158158
// Therefore, we use make to create a slice with zero elements

pkg/controllers/transaction_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"net/http"
66
"testing"
7+
"time"
78

89
"github.com/envelope-zero/backend/pkg/controllers"
910
"github.com/envelope-zero/backend/pkg/models"
@@ -107,6 +108,25 @@ func (suite *TestSuiteEnv) TestCreateTransaction() {
107108
_ = createTestTransaction(suite.T(), models.TransactionCreate{Note: "More tests something something", Amount: decimal.NewFromFloat(1253.17)})
108109
}
109110

111+
func (suite *TestSuiteEnv) TestTransactionSorting() {
112+
tFebrurary := createTestTransaction(suite.T(), models.TransactionCreate{Note: "Should be second in the list", Amount: decimal.NewFromFloat(1253.17), Date: time.Date(2022, 2, 15, 0, 0, 0, 0, time.UTC)})
113+
114+
tMarch := createTestTransaction(suite.T(), models.TransactionCreate{Note: "Should be first in the list", Amount: decimal.NewFromFloat(1253.17), Date: time.Date(2022, 3, 15, 0, 0, 0, 0, time.UTC)})
115+
116+
tJanuary := createTestTransaction(suite.T(), models.TransactionCreate{Note: "Should be third in the list", Amount: decimal.NewFromFloat(1253.17), Date: time.Date(2022, 1, 15, 0, 0, 0, 0, time.UTC)})
117+
118+
r := test.Request(suite.T(), http.MethodGet, "http://example.com/v1/transactions", "")
119+
test.AssertHTTPStatus(suite.T(), http.StatusOK, &r)
120+
121+
var transactions controllers.TransactionListResponse
122+
test.DecodeResponse(suite.T(), &r, &transactions)
123+
124+
assert.Len(suite.T(), transactions.Data, 3, "There are not exactly three transactions")
125+
assert.Equal(suite.T(), tMarch.Data.Date, transactions.Data[0].Date, "The first transaction is not the March transaction")
126+
assert.Equal(suite.T(), tFebrurary.Data.Date, transactions.Data[1].Date, "The second transaction is not the February transaction")
127+
assert.Equal(suite.T(), tJanuary.Data.Date, transactions.Data[2].Date, "The third transaction is not the January transaction")
128+
}
129+
110130
func (suite *TestSuiteEnv) TestCreateTransactionMissingReference() {
111131
budget := createTestBudget(suite.T(), models.BudgetCreate{})
112132
category := createTestCategory(suite.T(), models.CategoryCreate{BudgetID: budget.Data.ID})

0 commit comments

Comments
 (0)