Skip to content

Commit e0a915c

Browse files
committed
chore: add getBill endpoint
1 parent d47632e commit e0a915c

File tree

5 files changed

+60
-5
lines changed

5 files changed

+60
-5
lines changed

src/main/java/fun/trackmoney/transaction/controller/TransactionController.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package fun.trackmoney.transaction.controller;
22

3+
import fun.trackmoney.transaction.dto.BillResponseDTO;
34
import fun.trackmoney.transaction.dto.CreateTransactionDTO;
45
import fun.trackmoney.transaction.dto.TransactionResponseDTO;
56
import fun.trackmoney.transaction.dto.TransactionUpdateDTO;
@@ -61,6 +62,12 @@ public ResponseEntity<ApiResponse<BigDecimal>> getExpense(@PathVariable Integer
6162
new ApiResponse<>(true,"Get expense", transactionService.getExpense(id),null));
6263
}
6364

65+
@GetMapping("/bill/{id}")
66+
public ResponseEntity<ApiResponse<BillResponseDTO>> getBill(@PathVariable Integer id) {
67+
return ResponseEntity.ok().body(
68+
new ApiResponse<>(true,"Get expense", transactionService.getBill(id),null));
69+
}
70+
6471
@PutMapping("/{id}")
6572
public ResponseEntity<ApiResponse<TransactionResponseDTO>> updateTransaction(@PathVariable Integer id,
6673
@RequestBody TransactionUpdateDTO dto) {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package fun.trackmoney.transaction.dto;
2+
3+
import java.math.BigDecimal;
4+
5+
public record BillResponseDTO(BigDecimal bill,
6+
BigDecimal totalUpcoming,
7+
BigDecimal bueSoon) {
8+
}

src/main/java/fun/trackmoney/transaction/repository/TransactionRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99

1010
public interface TransactionRepository extends JpaRepository<TransactionEntity, Integer> {
1111
@Query("SELECT a FROM TransactionEntity a WHERE a.account.accountId = :accountId")
12-
List<TransactionEntity> findAllByUserEmail(@Param("accountId") Integer accountId);
12+
List<TransactionEntity> findAllByAccountId(@Param("accountId") Integer accountId);
1313
}

src/main/java/fun/trackmoney/transaction/service/TransactionService.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import fun.trackmoney.category.entity.CategoryEntity;
77
import fun.trackmoney.category.service.CategoryService;
88
import fun.trackmoney.enums.TransactionType;
9+
import fun.trackmoney.transaction.dto.BillResponseDTO;
910
import fun.trackmoney.transaction.dto.CreateTransactionDTO;
1011
import fun.trackmoney.transaction.dto.TransactionResponseDTO;
1112
import fun.trackmoney.transaction.dto.TransactionUpdateDTO;
@@ -16,6 +17,7 @@
1617
import org.springframework.stereotype.Service;
1718

1819
import java.math.BigDecimal;
20+
import java.time.LocalDate;
1921
import java.util.List;
2022
import java.util.Objects;
2123

@@ -82,7 +84,7 @@ public void delete(Integer id) {
8284
}
8385

8486
public BigDecimal getIncome(Integer accountId) {
85-
return transactionRepository.findAllByUserEmail(accountId)
87+
return transactionRepository.findAllByAccountId(accountId)
8688
.stream()
8789
.filter(t -> TransactionType.INCOME.equals(t.getTransactionType()))
8890
.map(TransactionEntity::getAmount)
@@ -91,11 +93,49 @@ public BigDecimal getIncome(Integer accountId) {
9193
}
9294

9395
public BigDecimal getExpense(Integer accountId) {
94-
return transactionRepository.findAllByUserEmail(accountId)
96+
return transactionRepository.findAllByAccountId(accountId)
9597
.stream()
9698
.filter(t -> TransactionType.EXPENSE.equals(t.getTransactionType()))
9799
.map(TransactionEntity::getAmount)
98100
.filter(Objects::nonNull)
99101
.reduce(BigDecimal.ZERO, BigDecimal::add);
100102
}
103+
104+
public BillResponseDTO getBill(Integer id) {
105+
var result = transactionRepository.findAllByAccountId(id);
106+
107+
BigDecimal totalBillsBeforeToday = result.stream()
108+
.filter(transaction ->
109+
transaction.getCategory().getName().equalsIgnoreCase("bill") &&
110+
transaction.getTransactionDate().toLocalDateTime().toLocalDate().isBefore(LocalDate.now())
111+
)
112+
.map(TransactionEntity::getAmount)
113+
.filter(Objects::nonNull)
114+
.reduce(BigDecimal.ZERO, BigDecimal::add);
115+
116+
117+
BigDecimal totalUpComing = result.stream()
118+
.filter(transaction ->
119+
transaction.getCategory().getName().equalsIgnoreCase("bill") &&
120+
transaction.getTransactionDate().toLocalDateTime().toLocalDate().isAfter(LocalDate.now())
121+
)
122+
.map(TransactionEntity::getAmount)
123+
.filter(Objects::nonNull)
124+
.reduce(BigDecimal.ZERO, BigDecimal::add);
125+
126+
BigDecimal totalBueSoon = result.stream()
127+
.filter(transaction -> {
128+
LocalDate date = transaction.getTransactionDate().toLocalDateTime().toLocalDate();
129+
return transaction.getCategory().getName().equalsIgnoreCase("bill")
130+
&& !date.isBefore(LocalDate.now())
131+
&& !date.isAfter(LocalDate.now().plusDays(7));
132+
})
133+
134+
.map(TransactionEntity::getAmount)
135+
.filter(Objects::nonNull)
136+
.reduce(BigDecimal.ZERO, BigDecimal::add);
137+
138+
139+
return new BillResponseDTO(totalBillsBeforeToday, totalUpComing, totalBueSoon);
140+
}
101141
}

src/test/java/fun/trackmoney/transaction/service/TransactionServiceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ void getIncome_shouldReturnTotalIncome() {
151151
tr2.setTransactionType(TransactionType.INCOME);
152152

153153
var accountId = 1;
154-
when(transactionRepository.findAllByUserEmail(accountId)).thenReturn(List.of(tr1, tr2));
154+
when(transactionRepository.findAllByAccountId(accountId)).thenReturn(List.of(tr1, tr2));
155155

156156
var result = transactionService.getIncome(accountId);
157157

@@ -169,7 +169,7 @@ void getExpense_shouldReturnTotalExpense() {
169169
tr2.setTransactionType(TransactionType.EXPENSE);
170170

171171
var accountId = 1;
172-
when(transactionRepository.findAllByUserEmail(accountId)).thenReturn(List.of(tr1, tr2));
172+
when(transactionRepository.findAllByAccountId(accountId)).thenReturn(List.of(tr1, tr2));
173173

174174
var result = transactionService.getExpense(accountId);
175175

0 commit comments

Comments
 (0)