[FIX] 目標金額が現在所持金より低かった場合、エラーを表示するように修正しました。
This commit is contained in:
@@ -18,6 +18,8 @@ public interface GoalService {
|
|||||||
|
|
||||||
public void updateGoalComplete(Long userId);
|
public void updateGoalComplete(Long userId);
|
||||||
|
|
||||||
|
public int getCurrentAmount(Long userId);
|
||||||
|
|
||||||
// void deleteGoal(Long goalId);
|
// void deleteGoal(Long goalId);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -59,13 +59,28 @@ public class GoalServiceImpl implements GoalService {
|
|||||||
: (int) ((currentAmount * 100.0) / goalEntity.getTargetAmount()),
|
: (int) ((currentAmount * 100.0) / goalEntity.getTargetAmount()),
|
||||||
currentAmount)));
|
currentAmount)));
|
||||||
|
|
||||||
|
|
||||||
return goalModelList;
|
return goalModelList;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public GoalModel createGoal(GoalModel goalModel, Long userId) {
|
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 goalEntity = GoalModel.convertTo(goalModel, userRepository.findById(userId).get());
|
||||||
|
|
||||||
GoalEntity savedGoalEntity = goalRepository.save(goalEntity);
|
GoalEntity savedGoalEntity = goalRepository.save(goalEntity);
|
||||||
@@ -108,4 +123,21 @@ public class GoalServiceImpl implements GoalService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -267,20 +267,31 @@ public class GoalController {
|
|||||||
|
|
||||||
@SessionCheck
|
@SessionCheck
|
||||||
@GetMapping(TransitionTargetPageNameKeyword.CREATE)
|
@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;
|
return TransitionTargetPageNameKeyword.GOAL_CREATE_HTML;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SessionCheck
|
@SessionCheck
|
||||||
@PostMapping(TransitionTargetPageNameKeyword.CREATE)
|
@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);
|
LoginModel loginUser = (LoginModel) session.getAttribute(SessionKeyword.LOGIN_USER);
|
||||||
|
|
||||||
Long userId = loginUser.getUserId();
|
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;
|
return TransitionTargetPageNameKeyword.REDIRECT + TransitionTargetPageNameKeyword.GOAL;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,10 @@
|
|||||||
<div>
|
<div>
|
||||||
<label>目標金額</label>
|
<label>目標金額</label>
|
||||||
<input type="number" th:field="*{targetAmount}" required min="1">
|
<input type="number" th:field="*{targetAmount}" required min="1">
|
||||||
|
<span th:text="|所持金:${currentAmount}円|"></span>
|
||||||
|
<span th:if="${message != null}">
|
||||||
|
<span th:text="${message}" style="color: red;"></span>
|
||||||
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
|
|||||||
Reference in New Issue
Block a user