Skip to content

Commit c93b2f1

Browse files
committed
chore: add the GET all budget by account Id
1 parent 7ff79bc commit c93b2f1

File tree

6 files changed

+35
-13
lines changed

6 files changed

+35
-13
lines changed

src/main/java/fun/trackmoney/budget/controller/BudgetsController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public ResponseEntity<ApiResponse<BudgetResponseDTO>> create(@RequestBody Budget
3333
new ApiResponse<>(true, "Budget created", budgetsService.create(dto), null));
3434
}
3535

36-
@GetMapping
37-
public ResponseEntity<ApiResponse<List<BudgetResponseDTO>>> findAll() {
38-
var list = budgetsService.findAll();
36+
@GetMapping("/findAll/{accountId}")
37+
public ResponseEntity<ApiResponse<List<BudgetResponseDTO>>> findAllByAccountId(@PathVariable Integer accountId) {
38+
var list = budgetsService.findAllByAccountId(accountId);
3939
return ResponseEntity.status(HttpStatus.OK).body(
4040
new ApiResponse<>(true, "Get all Budget",list , null));
4141
}

src/main/java/fun/trackmoney/budget/dtos/BudgetResponseDTO.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ public record BudgetResponseDTO (Integer budgetId,
99
CategoryEntity category,
1010
AccountResponseDTO account,
1111
BigDecimal targetAmount,
12+
BigDecimal currentAmount,
1213
Integer resetDay) {
1314
}

src/main/java/fun/trackmoney/budget/repository/BudgetsRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
import fun.trackmoney.budget.entity.BudgetsEntity;
44
import org.springframework.data.jpa.repository.JpaRepository;
55

6+
import java.util.List;
7+
68
public interface BudgetsRepository extends JpaRepository<BudgetsEntity, Integer> {
9+
List<BudgetsEntity> findAllByAccount_AccountId(Integer accountId);
710
}

src/main/java/fun/trackmoney/budget/service/BudgetsService.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import fun.trackmoney.user.service.UserService;
1313
import org.springframework.stereotype.Service;
1414

15+
import java.math.BigDecimal;
1516
import java.util.List;
1617

1718
@Service
@@ -47,8 +48,25 @@ public BudgetResponseDTO create(BudgetCreateDTO dto) {
4748
return budgetMapper.entityToResponseDTO(budgetsRepository.save(budgets));
4849
}
4950

50-
public List<BudgetResponseDTO> findAll() {
51-
return budgetMapper.entityListToResponseList(budgetsRepository.findAll());
51+
public List<BudgetResponseDTO> findAllByAccountId(Integer accountId) {
52+
return budgetMapper.entityListToResponseList(
53+
budgetsRepository.findAllByAccount_AccountId(accountId)
54+
).stream()
55+
.map(budget -> {
56+
BigDecimal currentAmount = budget.currentAmount() == null
57+
? BigDecimal.valueOf(100)
58+
: budget.currentAmount();
59+
60+
return new BudgetResponseDTO(
61+
budget.budgetId(),
62+
budget.category(),
63+
budget.account(),
64+
budget.targetAmount(),
65+
currentAmount,
66+
budget.resetDay()
67+
);
68+
})
69+
.toList();
5270
}
5371

5472
public BudgetResponseDTO findById(Integer id) {

src/test/java/fun/trackmoney/budget/controller/BudgetsControllerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,17 @@ void create_shouldReturnCreatedAndBudgetResponse() {
6262
@Test
6363
void findAll_shouldReturnOkAndListOfBudgetResponses() {
6464
List<BudgetResponseDTO> budgetList = Collections.singletonList(responseDTO);
65-
when(budgetsService.findAll()).thenReturn(budgetList);
65+
when(budgetsService.findAllByAccountId(createDTO.accountId())).thenReturn(budgetList);
6666

67-
ResponseEntity<ApiResponse<List<BudgetResponseDTO>>> response = budgetsController.findAll();
67+
ResponseEntity<ApiResponse<List<BudgetResponseDTO>>> response = budgetsController.findAllByAccountId(createDTO.accountId());
6868

6969
assertEquals(HttpStatus.OK, response.getStatusCode());
7070
assertNotNull(response.getBody());
7171
assertTrue(response.getBody().isSuccess());
7272
assertEquals("Get all Budget", response.getBody().getMessage());
7373
assertEquals(budgetList, response.getBody().getData());
7474
assertNull(response.getBody().getErrors());
75-
verify(budgetsService, times(1)).findAll();
75+
verify(budgetsService, times(1)).findAllByAccountId(createDTO.accountId());
7676
}
7777

7878
@Test

src/test/java/fun/trackmoney/budget/service/BudgetsServiceTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ void findAll_shouldReturnEmptyList_whenNoBudgetsExist() {
114114
when(budgetsRepository.findAll()).thenReturn(Collections.emptyList());
115115
when(budgetMapper.entityListToResponseList(Collections.emptyList())).thenReturn(Collections.emptyList());
116116

117-
List<BudgetResponseDTO> result = budgetsService.findAll();
117+
List<BudgetResponseDTO> result = budgetsService.findAllByAccountId(accountEntity.getAccountId());
118118

119119
assertNotNull(result);
120120
assertTrue(result.isEmpty());
121-
verify(budgetsRepository, times(1)).findAll();
121+
verify(budgetsRepository, times(1)).findAllByAccount_AccountId(accountEntity.getAccountId());
122122
verify(budgetMapper, times(1)).entityListToResponseList(Collections.emptyList());
123123
}
124124

@@ -131,15 +131,15 @@ void findAll_shouldReturnListOfBudgetResponseDTO() {
131131
List<BudgetsEntity> entities = List.of(entity);
132132
List<BudgetResponseDTO> dtos = List.of(dto);
133133

134-
when(budgetsRepository.findAll()).thenReturn(entities);
134+
when(budgetsRepository.findAllByAccount_AccountId(accountEntity.getAccountId())).thenReturn(entities);
135135
when(budgetMapper.entityListToResponseList(entities)).thenReturn(dtos);
136136

137-
List<BudgetResponseDTO> result = budgetsService.findAll();
137+
List<BudgetResponseDTO> result = budgetsService.findAllByAccountId(accountEntity.getAccountId());
138138

139139
assertNotNull(result);
140140
assertEquals(1, result.size());
141141
assertEquals(dtos, result);
142-
verify(budgetsRepository, times(1)).findAll();
142+
verify(budgetsRepository, times(1)).findAllByAccount_AccountId(accountEntity.getAccountId());
143143
verify(budgetMapper, times(1)).entityListToResponseList(entities);
144144
}
145145

0 commit comments

Comments
 (0)