[FIX] 目標金額が現在所持金より低かった場合、エラーを表示するように修正しました。
This commit is contained in:
@@ -18,6 +18,8 @@ public interface GoalService {
|
||||
|
||||
public void updateGoalComplete(Long userId);
|
||||
|
||||
public int getCurrentAmount(Long userId);
|
||||
|
||||
// void deleteGoal(Long goalId);
|
||||
|
||||
}
|
||||
@@ -47,7 +47,7 @@ public class GoalServiceImpl implements GoalService {
|
||||
|
||||
// 現在の資産 = 貯蓄金 + 収支合計
|
||||
int currentAmount = userEntity.getSaving() + transactionTotal;
|
||||
|
||||
|
||||
// 達成率(%) = 現在の資産 ÷ 目標金額 × 100
|
||||
|
||||
List<GoalModel> goalModelList = new ArrayList<GoalModel>();
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -37,6 +37,10 @@
|
||||
<div>
|
||||
<label>目標金額</label>
|
||||
<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>
|
||||
|
||||
Reference in New Issue
Block a user