diff --git a/src/main/java/com/example/demo/app/domain/service/GoalService.java b/src/main/java/com/example/demo/app/domain/service/GoalService.java index 05e9e89..ce3bf35 100644 --- a/src/main/java/com/example/demo/app/domain/service/GoalService.java +++ b/src/main/java/com/example/demo/app/domain/service/GoalService.java @@ -18,6 +18,8 @@ public interface GoalService { public void updateGoalComplete(Long userId); + public int getCurrentAmount(Long userId); + // void deleteGoal(Long goalId); } \ No newline at end of file diff --git a/src/main/java/com/example/demo/app/domain/service/impl/GoalServiceImpl.java b/src/main/java/com/example/demo/app/domain/service/impl/GoalServiceImpl.java index 5e30fed..3f60f9e 100644 --- a/src/main/java/com/example/demo/app/domain/service/impl/GoalServiceImpl.java +++ b/src/main/java/com/example/demo/app/domain/service/impl/GoalServiceImpl.java @@ -47,7 +47,7 @@ public class GoalServiceImpl implements GoalService { // 現在の資産 = 貯蓄金 + 収支合計 int currentAmount = userEntity.getSaving() + transactionTotal; - + // 達成率(%) = 現在の資産 ÷ 目標金額 × 100 List goalModelList = new ArrayList(); @@ -59,13 +59,28 @@ public class GoalServiceImpl implements GoalService { : (int) ((currentAmount * 100.0) / goalEntity.getTargetAmount()), currentAmount))); - return goalModelList; } @Override public GoalModel createGoal(GoalModel goalModel, Long userId) { + UserEntity userEntity = userRepository.findById(userId).orElseThrow(); + + int transactionTotal = userEntity.getTransactionEntityList().stream() + .mapToInt(t -> { + int sign = t.getCategoryEntity().getTransactionType() == 1 ? 1 : -1; + return sign * t.getAmount(); + }) + .sum(); + + // 現在の資産 = 貯蓄金 + 収支合計 + int currentAmount = userEntity.getSaving() + transactionTotal; + + if (currentAmount >= goalModel.getTargetAmount()) { + return null; + } + GoalEntity goalEntity = GoalModel.convertTo(goalModel, userRepository.findById(userId).get()); GoalEntity savedGoalEntity = goalRepository.save(goalEntity); @@ -82,7 +97,7 @@ public class GoalServiceImpl implements GoalService { return GoalModel.convertFrom(updatedGoalEntity, 0, 0); } - + public void delete(Long goalId) { goalRepository.deleteById(goalId); } @@ -100,12 +115,29 @@ public class GoalServiceImpl implements GoalService { .sum(); int currentAmount = userEntity.getSaving() + transactionTotal; - + goalEntityList.stream().forEach(goalEntity -> { if (goalEntity.getTargetAmount() <= currentAmount) { goalRepository.updateCompleteByGoalId(goalEntity.getGoalId()); } }); } + + public int getCurrentAmount(Long userId) { + + UserEntity userEntity = userRepository.findById(userId).orElseThrow(); + + int transactionTotal = userEntity.getTransactionEntityList().stream() + .mapToInt(t -> { + int sign = t.getCategoryEntity().getTransactionType() == 1 ? 1 : -1; + return sign * t.getAmount(); + }) + .sum(); + + int currentAmount = userEntity.getSaving() + transactionTotal; + + return currentAmount; + + } } \ No newline at end of file diff --git a/src/main/java/com/example/demo/app/presentation/controller/GoalController.java b/src/main/java/com/example/demo/app/presentation/controller/GoalController.java index 585b1e8..cb7ee49 100644 --- a/src/main/java/com/example/demo/app/presentation/controller/GoalController.java +++ b/src/main/java/com/example/demo/app/presentation/controller/GoalController.java @@ -267,20 +267,31 @@ public class GoalController { @SessionCheck @GetMapping(TransitionTargetPageNameKeyword.CREATE) - public String createGoalGet(@ModelAttribute GoalForm goalForm, HttpSession session) { + public String createGoalGet(@ModelAttribute GoalForm goalForm, HttpSession session, Model model) { + LoginModel loginUser = (LoginModel) session.getAttribute(SessionKeyword.LOGIN_USER); + + Long userId = loginUser.getUserId(); + + int currentAmount = goalService.getCurrentAmount(userId); + + model.addAttribute("currentAmount", currentAmount); + return TransitionTargetPageNameKeyword.GOAL_CREATE_HTML; } @SessionCheck @PostMapping(TransitionTargetPageNameKeyword.CREATE) - public String createGoalPost(@ModelAttribute GoalForm goalForm, HttpSession session) { + public String createGoalPost(@ModelAttribute GoalForm goalForm, HttpSession session, Model model) { LoginModel loginUser = (LoginModel) session.getAttribute(SessionKeyword.LOGIN_USER); Long userId = loginUser.getUserId(); - goalService.createGoal(GoalModel.convertFrom(goalForm), userId); + if (goalService.createGoal(GoalModel.convertFrom(goalForm), userId) == null) { + model.addAttribute(PageReturnAttributeKeyword.MESSAGE, "現在所持金より高い金額を設定してください。"); + return TransitionTargetPageNameKeyword.GOAL_CREATE_HTML; + } return TransitionTargetPageNameKeyword.REDIRECT + TransitionTargetPageNameKeyword.GOAL; } diff --git a/src/main/resources/templates/goal_create.html b/src/main/resources/templates/goal_create.html index 6a23ef3..2057470 100644 --- a/src/main/resources/templates/goal_create.html +++ b/src/main/resources/templates/goal_create.html @@ -37,6 +37,10 @@
+ + + +