Skip to content

Commit 22dcfa7

Browse files
authored
Merge pull request #39 from felipemelozx/develop
Develop
2 parents 080aa69 + d47632e commit 22dcfa7

File tree

26 files changed

+371
-37
lines changed

26 files changed

+371
-37
lines changed

src/main/java/fun/trackmoney/account/controller/AccountController.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import fun.trackmoney.account.dtos.AccountResponseDTO;
55
import fun.trackmoney.account.dtos.AccountUpdateRequestDTO;
66
import fun.trackmoney.account.service.AccountService;
7+
import fun.trackmoney.utils.AuthUtils;
78
import fun.trackmoney.utils.response.ApiResponse;
89
import org.springframework.http.HttpStatus;
910
import org.springframework.http.ResponseEntity;
@@ -17,15 +18,18 @@
1718
import org.springframework.web.bind.annotation.RestController;
1819

1920
import java.util.List;
21+
import java.util.UUID;
2022

2123
@RestController
2224
@RequestMapping("/accounts")
2325
public class AccountController {
2426

2527
private final AccountService accountService;
28+
private final AuthUtils authUtils;
2629

27-
public AccountController(AccountService accountService) {
30+
public AccountController(AccountService accountService, AuthUtils authUtils) {
2831
this.accountService = accountService;
32+
this.authUtils = authUtils;
2933
}
3034

3135
@PostMapping
@@ -35,13 +39,16 @@ public ResponseEntity<ApiResponse<AccountResponseDTO>> createAccount(@RequestBod
3539
true, "Account successfully created.", createdAccount, null), HttpStatus.CREATED);
3640
}
3741

42+
3843
@GetMapping
3944
public ResponseEntity<ApiResponse<List<AccountResponseDTO>>> findAllAccounts() {
40-
List<AccountResponseDTO> accounts = accountService.findAllAccount();
45+
UUID email = authUtils.getCurrentUser().getUserId();
46+
List<AccountResponseDTO> accounts = accountService.findAllAccount(email);
4147
return ResponseEntity.ok(new ApiResponse<>(
4248
true, "Account list retrieved successfully.", accounts, null));
4349
}
4450

51+
4552
@GetMapping("/{id}")
4653
public ResponseEntity<ApiResponse<AccountResponseDTO>> findAccountById(@PathVariable Integer id) {
4754
AccountResponseDTO account = accountService.findAccountById(id);

src/main/java/fun/trackmoney/account/mapper/AccountMapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ public interface AccountMapper {
1919
List<AccountResponseDTO> accountEntityListToAccountResponseList(List<AccountEntity> entities);
2020

2121
AccountEntity accountResponseToEntity(AccountResponseDTO accountById);
22+
23+
AccountUpdateRequestDTO toAccountRequest(AccountEntity account);
2224
}

src/main/java/fun/trackmoney/account/repository/AccountRepository.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
import fun.trackmoney.account.entity.AccountEntity;
44
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Query;
6+
import org.springframework.data.repository.query.Param;
7+
8+
import java.util.List;
9+
import java.util.UUID;
510

611
public interface AccountRepository extends JpaRepository<AccountEntity, Integer> {
12+
@Query("SELECT a FROM AccountEntity a WHERE a.user.userId = :userId")
13+
List<AccountEntity> findAllByUserEmail(@Param("userId") UUID userId);
714
}

src/main/java/fun/trackmoney/account/service/AccountService.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
import fun.trackmoney.user.repository.UserRepository;
1212
import org.springframework.stereotype.Service;
1313

14+
import java.math.BigDecimal;
1415
import java.util.List;
16+
import java.util.UUID;
1517

1618
@Service
1719
public class AccountService {
@@ -38,8 +40,8 @@ public AccountResponseDTO createAccount(AccountRequestDTO dto) {
3840
.save(account));
3941
}
4042

41-
public List<AccountResponseDTO> findAllAccount() {
42-
return accountMapper.accountEntityListToAccountResponseList(accountRepository.findAll());
43+
public List<AccountResponseDTO> findAllAccount(UUID userId) {
44+
return accountMapper.accountEntityListToAccountResponseList(accountRepository.findAllByUserEmail(userId));
4345
}
4446

4547
public AccountResponseDTO findAccountById(Integer id) {
@@ -60,4 +62,17 @@ public AccountResponseDTO updateAccountById(Integer id, AccountUpdateRequestDTO
6062
public void deleteById(Integer id) {
6163
accountRepository.deleteById(id);
6264
}
65+
66+
public void updateAccountBalance(BigDecimal balance, Integer accountId, Boolean isCredit) {
67+
var account = accountRepository.findById(accountId)
68+
.orElseThrow(() -> new AccountNotFoundException("Account not found!"));
69+
70+
if (isCredit) {
71+
account.setBalance(account.getBalance().add(balance));
72+
} else{
73+
account.setBalance(account.getBalance().subtract(balance));
74+
}
75+
76+
accountRepository.save(account);
77+
}
6378
}

src/main/java/fun/trackmoney/auth/controller/AuthController.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import fun.trackmoney.utils.response.ApiResponse;
99
import jakarta.validation.Valid;
1010
import org.springframework.http.ResponseEntity;
11+
import org.springframework.web.bind.annotation.GetMapping;
1112
import org.springframework.web.bind.annotation.PostMapping;
1213
import org.springframework.web.bind.annotation.RequestBody;
1314
import org.springframework.web.bind.annotation.RequestMapping;
@@ -18,7 +19,7 @@
1819
* Provides endpoints for user registration.
1920
*/
2021
@RestController
21-
@RequestMapping("auth/")
22+
@RequestMapping("auth")
2223
public class AuthController {
2324

2425
private final AuthService authService;
@@ -39,7 +40,7 @@ public AuthController(AuthService authService) {
3940
* @return A ResponseEntity containing an ApiResponse
4041
* with the result of the registration operation.
4142
*/
42-
@PostMapping("register")
43+
@PostMapping("/register")
4344
public ResponseEntity<ApiResponse<UserResponseDTO>> register(
4445
@RequestBody @Valid UserRequestDTO userDto) {
4546
return ResponseEntity.ok().body(
@@ -52,7 +53,7 @@ public ResponseEntity<ApiResponse<UserResponseDTO>> register(
5253
);
5354
}
5455

55-
@PostMapping("login")
56+
@PostMapping("/login")
5657
public ResponseEntity<ApiResponse<LoginResponseDTO>> login(@RequestBody LoginRequestDTO loginDto) {
5758
return ResponseEntity.ok().body(
5859
new ApiResponse<>(
@@ -63,4 +64,16 @@ public ResponseEntity<ApiResponse<LoginResponseDTO>> login(@RequestBody LoginReq
6364
)
6465
);
6566
}
67+
68+
@GetMapping("/verify")
69+
public ResponseEntity<ApiResponse<Boolean>> verify() {
70+
return ResponseEntity.ok().body(
71+
new ApiResponse<>(
72+
true,
73+
"Token is valid!",
74+
true,
75+
null
76+
)
77+
);
78+
}
6679
}

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: 2 additions & 1 deletion
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-
Integer resetDay) {
12+
Integer resetDay,
13+
BigDecimal currentAmount) {
1314
}

src/main/java/fun/trackmoney/budget/entity/BudgetsEntity.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public class BudgetsEntity {
3838
@Column(name = "reset_day")
3939
private Integer resetDay;
4040

41+
private BigDecimal currentAmount;
42+
4143
public AccountEntity getAccount() {
4244
return account;
4345
}
@@ -58,6 +60,18 @@ public BudgetsEntity(Integer budgetId,
5860
this.resetDay = resetDay;
5961
}
6062

63+
public BudgetsEntity(Integer budgetId,
64+
CategoryEntity category,
65+
AccountEntity account,
66+
UserEntity userEntity,
67+
BigDecimal targetAmount,
68+
Integer resetDay,
69+
BigDecimal currentAmount) {
70+
this(budgetId, category, userEntity, targetAmount, resetDay);
71+
this.currentAmount = currentAmount;
72+
this.account = account;
73+
}
74+
6175
public void setAccount(AccountEntity account) {
6276
this.account = account;
6377
}
@@ -105,4 +119,12 @@ public UserEntity getUserEntity() {
105119
public void setUserEntity(UserEntity userEntity) {
106120
this.userEntity = userEntity;
107121
}
122+
123+
public BigDecimal getCurrentAmount() {
124+
return currentAmount;
125+
}
126+
127+
public void setCurrentAmount(BigDecimal currentAmount) {
128+
this.currentAmount = currentAmount;
129+
}
108130
}

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> findAllByAccountAccountId(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.findAllByAccountAccountId(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+
budget.resetDay(),
66+
currentAmount
67+
);
68+
})
69+
.toList();
5270
}
5371

5472
public BudgetResponseDTO findById(Integer id) {

0 commit comments

Comments
 (0)