Skip to content

Commit

Permalink
Feat (change float32 to float64, implement get summary)
Browse files Browse the repository at this point in the history
  • Loading branch information
larb26656 committed May 19, 2024
1 parent 7232e81 commit af5b6de
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 21 deletions.
2 changes: 1 addition & 1 deletion api/transaction/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestHandler_GetAll(t *testing.T) {
expected := []Transaction{
{
ID: 1,
Amount: float32(2000.0),
Amount: 2000.0,
Date: nil,
Category: "food",
ImageUrl: "http://img.png",
Expand Down
5 changes: 2 additions & 3 deletions api/transaction/middlewareService.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ func (m middlewareService) SetFilter(queryParams map[string][]string) Filter {

switch key {
case "date":
loc := time.Now().Location()
parseDate, err := time.ParseInLocation("2006-01-02", value, loc)
parseDate, err := time.ParseInLocation("2006-01-02", value, time.Now().Location())
if err == nil {
filter.Date = &parseDate
}
case "amount":
amount, err := strconv.ParseFloat(value, 32)
if err == nil {
filter.Amount = float32(amount)
filter.Amount = amount
}
case "category":
filter.Category = value
Expand Down
4 changes: 2 additions & 2 deletions api/transaction/middlewareService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestSetFilter(t *testing.T) {
"amount": {amount},
},
expected: Filter{
Amount: float32(expectedAmount),
Amount: expectedAmount,
},
}, {
test: "category is set in query params",
Expand Down Expand Up @@ -66,7 +66,7 @@ func TestSetFilter(t *testing.T) {
},
expected: Filter{
Date: &expectedDate,
Amount: float32(expectedAmount),
Amount: expectedAmount,
Category: expectedCategory,
},
},
Expand Down
1 change: 1 addition & 0 deletions api/transaction/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (r repository) GetAll(filter Filter, paginate Pagination) ([]Transaction, e
conditions = append(conditions, fmt.Sprintf("date = $%d", len(args)+1))
args = append(args, filter.Date)
}

if filter.Amount != 0 {
conditions = append(conditions, fmt.Sprintf("amount = $%d", len(args)+1))
args = append(args, filter.Amount)
Expand Down
2 changes: 1 addition & 1 deletion api/transaction/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestGetAll_ShouldReturnData_WhenCorrectInput(t *testing.T) {
mockDate := time.Date(2020, time.April,
11, 21, 34, 01, 0, time.UTC)

mockAmount := float32(10.0)
mockAmount := 10.0
mockCategory := "mock category"

mockFilter := Filter{
Expand Down
28 changes: 27 additions & 1 deletion api/transaction/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,33 @@ func (s service) DeleteExpense(id int) error {
}

func (s service) GetSummary(spenderId int, txnType string) (SummaryResponse, error) {
return SummaryResponse{}, nil
summaries, err := s.repository.GetSummary(spenderId, []string{txnType})

if err != nil {
return SummaryResponse{}, err
}

if len(summaries) == 0 {
return SummaryResponse{}, err
}

var totalAmount float64
spendDaysMap := make(map[string]int)

for _, v := range summaries {
totalAmount += v.Amount

dateKey := v.Date.Format("2016-02-01")
spendDaysMap[dateKey] = spendDaysMap[dateKey] + 1
}

spendDays := len(spendDaysMap)

return SummaryResponse{
TotalAmount: totalAmount,
AvgAmountPerDay: totalAmount / float64(spendDays),
Total: len(summaries),
}, nil
}

func (s service) GetExpenses(spenderId int) ([]Transaction, error) {
Expand Down
6 changes: 3 additions & 3 deletions api/transaction/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestService_GetAll_ShouldReturnError_WhenRepositoryReturnsError(t *testing.
service := NewService(mockRepo)

mockDate := time.Date(2020, time.April, 11, 21, 34, 01, 0, time.UTC)
mockAmount := float32(200.2)
mockAmount := 200.2
mockCategory := "category1"

mockFilter := Filter{
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestService_GetAll_ShouldReturnData_WhenRepositoryReturnsData(t *testing.T)
service := NewService(mockRepo)

mockDate := time.Date(2020, time.April, 11, 21, 34, 01, 0, time.UTC)
mockAmount := float32(200.2)
mockAmount := 200.2
mockCategory := "category1"

mockFilter := Filter{
Expand Down Expand Up @@ -118,6 +118,6 @@ func TestService_PassCoverage(t *testing.T) {
_ = service.DeleteExpense(0)

assert.Equal(t, createRes, CreateTransactionResponse{})
assert.Equal(t, sumRes, SummaryResponse{})
assert.Equal(t, SummaryResponse{}, sumRes)
assert.Equal(t, balRes, BalanceResponse{})
}
20 changes: 10 additions & 10 deletions api/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "time"

type Filter struct {
Date *time.Time `json:"date"`
Amount float32 `json:"amount"`
Amount float64 `json:"amount"`
Category string `json:"category"`
}

Expand All @@ -16,7 +16,7 @@ type Pagination struct {
type Transaction struct {
ID int `json:"id"`
Date *time.Time `json:"date"`
Amount float32 `json:"amount"`
Amount float64 `json:"amount"`
Category string `json:"category"`
ImageUrl string `json:"image_url"`
Note string `json:"note"`
Expand All @@ -25,7 +25,7 @@ type Transaction struct {

type CreateTransactionRequest struct {
Date *time.Time `json:"date"`
Amount float32 `json:"amount"`
Amount float64 `json:"amount"`
Category string `json:"category"`
ImageUrl string `json:"image_url"`
Note string `json:"note"`
Expand All @@ -38,21 +38,21 @@ type CreateTransactionResponse struct {
}

type SummaryResponse struct {
TotalAmountSpend float32 `json:"total_amount_spend"`
AvgAmountSpendPerDay float32 `json:"average_amount_spend_per_day"`
Total int `json:"total"`
TotalAmount float64 `json:"total_amount"`
AvgAmountPerDay float64 `json:"avg_amount_per_day"`
Total int `json:"total"`
}

type BalanceResponse struct {
TotalAmountEarned float32 `json:"total_amount_earned"`
TotalAmountSpend float32 `json:"total_amount_spend"`
TotalAmountSaved float32 `json:"total_amount_saved"`
TotalAmountEarned float64 `json:"total_amount_earned"`
TotalAmountSpend float64 `json:"total_amount_spend"`
TotalAmountSaved float64 `json:"total_amount_saved"`
}

type GetTransactionResponse struct {
ID int `json:"id"`
Date *time.Time `json:"date"`
Amount float32 `json:"amount"`
Amount float64 `json:"amount"`
Category string `json:"category"`
ImageUrl string `json:"image_url"`
Note string `json:"note"`
Expand Down

0 comments on commit af5b6de

Please sign in to comment.