Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import fun.trackmoney.account.dtos.AccountResponseDTO;
import fun.trackmoney.account.dtos.AccountUpdateRequestDTO;
import fun.trackmoney.account.service.AccountService;
import fun.trackmoney.utils.AuthUtils;
import fun.trackmoney.utils.response.ApiResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -17,15 +18,18 @@
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/accounts")
public class AccountController {

private final AccountService accountService;
private final AuthUtils authUtils;

public AccountController(AccountService accountService) {
public AccountController(AccountService accountService, AuthUtils authUtils) {
this.accountService = accountService;
this.authUtils = authUtils;
}

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


@GetMapping
public ResponseEntity<ApiResponse<List<AccountResponseDTO>>> findAllAccounts() {
List<AccountResponseDTO> accounts = accountService.findAllAccount();
UUID email = authUtils.getCurrentUser().getUserId();
List<AccountResponseDTO> accounts = accountService.findAllAccount(email);
return ResponseEntity.ok(new ApiResponse<>(
true, "Account list retrieved successfully.", accounts, null));
}


@GetMapping("/{id}")
public ResponseEntity<ApiResponse<AccountResponseDTO>> findAccountById(@PathVariable Integer id) {
AccountResponseDTO account = accountService.findAccountById(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ public interface AccountMapper {
List<AccountResponseDTO> accountEntityListToAccountResponseList(List<AccountEntity> entities);

AccountEntity accountResponseToEntity(AccountResponseDTO accountById);

AccountUpdateRequestDTO toAccountRequest(AccountEntity account);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

import fun.trackmoney.account.entity.AccountEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.UUID;

public interface AccountRepository extends JpaRepository<AccountEntity, Integer> {
@Query("SELECT a FROM AccountEntity a WHERE a.user.userId = :userId")
List<AccountEntity> findAllByUserEmail(@Param("userId") UUID userId);
}
19 changes: 17 additions & 2 deletions src/main/java/fun/trackmoney/account/service/AccountService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import fun.trackmoney.user.repository.UserRepository;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

@Service
public class AccountService {
Expand All @@ -38,8 +40,8 @@ public AccountResponseDTO createAccount(AccountRequestDTO dto) {
.save(account));
}

public List<AccountResponseDTO> findAllAccount() {
return accountMapper.accountEntityListToAccountResponseList(accountRepository.findAll());
public List<AccountResponseDTO> findAllAccount(UUID userId) {
return accountMapper.accountEntityListToAccountResponseList(accountRepository.findAllByUserEmail(userId));
}

public AccountResponseDTO findAccountById(Integer id) {
Expand All @@ -60,4 +62,17 @@ public AccountResponseDTO updateAccountById(Integer id, AccountUpdateRequestDTO
public void deleteById(Integer id) {
accountRepository.deleteById(id);
}

public void updateAccountBalance(BigDecimal balance, Integer accountId, Boolean isCredit) {
var account = accountRepository.findById(accountId)
.orElseThrow(() -> new AccountNotFoundException("Account not found!"));

if (isCredit) {
account.setBalance(account.getBalance().add(balance));
} else{
account.setBalance(account.getBalance().subtract(balance));
}

accountRepository.save(account);
}
}
19 changes: 16 additions & 3 deletions src/main/java/fun/trackmoney/auth/controller/AuthController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import fun.trackmoney.utils.response.ApiResponse;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -18,7 +19,7 @@
* Provides endpoints for user registration.
*/
@RestController
@RequestMapping("auth/")
@RequestMapping("auth")
public class AuthController {

private final AuthService authService;
Expand All @@ -39,7 +40,7 @@ public AuthController(AuthService authService) {
* @return A ResponseEntity containing an ApiResponse
* with the result of the registration operation.
*/
@PostMapping("register")
@PostMapping("/register")
public ResponseEntity<ApiResponse<UserResponseDTO>> register(
@RequestBody @Valid UserRequestDTO userDto) {
return ResponseEntity.ok().body(
Expand All @@ -52,7 +53,7 @@ public ResponseEntity<ApiResponse<UserResponseDTO>> register(
);
}

@PostMapping("login")
@PostMapping("/login")
public ResponseEntity<ApiResponse<LoginResponseDTO>> login(@RequestBody LoginRequestDTO loginDto) {
return ResponseEntity.ok().body(
new ApiResponse<>(
Expand All @@ -63,4 +64,16 @@ public ResponseEntity<ApiResponse<LoginResponseDTO>> login(@RequestBody LoginReq
)
);
}

@GetMapping("/verify")
public ResponseEntity<ApiResponse<Boolean>> verify() {
return ResponseEntity.ok().body(
new ApiResponse<>(
true,
"Token is valid!",
true,
null
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public ResponseEntity<ApiResponse<BudgetResponseDTO>> create(@RequestBody Budget
new ApiResponse<>(true, "Budget created", budgetsService.create(dto), null));
}

@GetMapping
public ResponseEntity<ApiResponse<List<BudgetResponseDTO>>> findAll() {
var list = budgetsService.findAll();
@GetMapping("/findAll/{accountId}")
public ResponseEntity<ApiResponse<List<BudgetResponseDTO>>> findAllByAccountId(@PathVariable Integer accountId) {
var list = budgetsService.findAllByAccountId(accountId);
return ResponseEntity.status(HttpStatus.OK).body(
new ApiResponse<>(true, "Get all Budget",list , null));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public record BudgetResponseDTO (Integer budgetId,
CategoryEntity category,
AccountResponseDTO account,
BigDecimal targetAmount,
Integer resetDay) {
Integer resetDay,
BigDecimal currentAmount) {
}
22 changes: 22 additions & 0 deletions src/main/java/fun/trackmoney/budget/entity/BudgetsEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class BudgetsEntity {
@Column(name = "reset_day")
private Integer resetDay;

private BigDecimal currentAmount;

public AccountEntity getAccount() {
return account;
}
Expand All @@ -58,6 +60,18 @@ public BudgetsEntity(Integer budgetId,
this.resetDay = resetDay;
}

public BudgetsEntity(Integer budgetId,
CategoryEntity category,
AccountEntity account,
UserEntity userEntity,
BigDecimal targetAmount,
Integer resetDay,
BigDecimal currentAmount) {
this(budgetId, category, userEntity, targetAmount, resetDay);
this.currentAmount = currentAmount;
this.account = account;
}

public void setAccount(AccountEntity account) {
this.account = account;
}
Expand Down Expand Up @@ -105,4 +119,12 @@ public UserEntity getUserEntity() {
public void setUserEntity(UserEntity userEntity) {
this.userEntity = userEntity;
}

public BigDecimal getCurrentAmount() {
return currentAmount;
}

public void setCurrentAmount(BigDecimal currentAmount) {
this.currentAmount = currentAmount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
import fun.trackmoney.budget.entity.BudgetsEntity;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface BudgetsRepository extends JpaRepository<BudgetsEntity, Integer> {
List<BudgetsEntity> findAllByAccountAccountId(Integer accountId);
}
22 changes: 20 additions & 2 deletions src/main/java/fun/trackmoney/budget/service/BudgetsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import fun.trackmoney.user.service.UserService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
Expand Down Expand Up @@ -47,8 +48,25 @@ public BudgetResponseDTO create(BudgetCreateDTO dto) {
return budgetMapper.entityToResponseDTO(budgetsRepository.save(budgets));
}

public List<BudgetResponseDTO> findAll() {
return budgetMapper.entityListToResponseList(budgetsRepository.findAll());
public List<BudgetResponseDTO> findAllByAccountId(Integer accountId) {
return budgetMapper.entityListToResponseList(
budgetsRepository.findAllByAccountAccountId(accountId)
).stream()
.map(budget -> {
BigDecimal currentAmount = budget.currentAmount() == null
? BigDecimal.valueOf(100)
: budget.currentAmount();

return new BudgetResponseDTO(
budget.budgetId(),
budget.category(),
budget.account(),
budget.targetAmount(),
budget.resetDay(),
currentAmount
);
})
.toList();
}

public BudgetResponseDTO findById(Integer id) {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/fun/trackmoney/category/entity/CategoryEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class CategoryEntity {
*/
private String name;

private String color;
/**
* Default constructor for creating an instance of {@link CategoryEntity}.
* This constructor is required for JPA persistence.
Expand All @@ -46,6 +47,19 @@ public CategoryEntity(Integer categoryId, String name) {
this.name = name;
}

public CategoryEntity(Integer categoryId, String name, String color) {
this(categoryId,name);
this.color = color;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

/**
* Gets the category ID.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@RestController
Expand Down Expand Up @@ -48,6 +49,18 @@ public ResponseEntity<ApiResponse<TransactionResponseDTO>> findTransactionById(@
new ApiResponse<>(true,"Get transaction by id", transactionService.findById(id),null));
}

@GetMapping("/income/{id}")
public ResponseEntity<ApiResponse<BigDecimal>> getIncome(@PathVariable Integer id) {
return ResponseEntity.ok().body(
new ApiResponse<>(true,"Get income", transactionService.getIncome(id),null));
}

@GetMapping("/expense/{id}")
public ResponseEntity<ApiResponse<BigDecimal>> getExpense(@PathVariable Integer id) {
return ResponseEntity.ok().body(
new ApiResponse<>(true,"Get expense", transactionService.getExpense(id),null));
}

@PutMapping("/{id}")
public ResponseEntity<ApiResponse<TransactionResponseDTO>> updateTransaction(@PathVariable Integer id,
@RequestBody TransactionUpdateDTO dto) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package fun.trackmoney.transaction.dto;

import fun.trackmoney.enums.TransactionType;

import java.math.BigDecimal;

public record TransactionUpdateDTO(String description,
BigDecimal amount,
Integer accountId,
Integer categoryId) {
Integer categoryId,
TransactionType transactionType) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

import fun.trackmoney.transaction.entity.TransactionEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface TransactionRepository extends JpaRepository<TransactionEntity, Integer> {
@Query("SELECT a FROM TransactionEntity a WHERE a.account.accountId = :accountId")
List<TransactionEntity> findAllByUserEmail(@Param("accountId") Integer accountId);
}
Loading