3 Commits

6 changed files with 65 additions and 18 deletions

View File

@@ -24,6 +24,8 @@ public class GoalModel {
private Integer goalComplete;
private Integer achievementRate; // 達成率
public static GoalModel convertFrom(GoalEntity goalEntity) {
return new GoalModel(
goalEntity.getGoalId(),
@@ -31,7 +33,8 @@ public class GoalModel {
goalEntity.getGoalSubtitle(),
goalEntity.getTargetAmount(),
goalEntity.getDeadline(),
goalEntity.getGoalComplete());
goalEntity.getGoalComplete(),
null);
}
public static GoalEntity convertTo(GoalModel goalModel) {
@@ -52,7 +55,8 @@ public class GoalModel {
goalForm.getGoalSubtitle(),
goalForm.getTargetAmount(),
goalForm.getDeadline(),
goalForm.getGoalComplete());
goalForm.getGoalComplete()
,null);
}
}

View File

@@ -29,7 +29,34 @@ public class GoalServiceImpl implements GoalService {
UserEntity userEntity = userRepository.findById(userId).orElseThrow();
GoalEntity goalEntity = userEntity.getGoalEntity();
return GoalModel.convertFrom(goalEntity);
// 収支の合計を計算(収入はプラス、支出はマイナス)
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;
// 達成率(%) = 現在の資産 ÷ 目標金額 × 100
int achievementRate =
(goalEntity.getTargetAmount() == null)
? 0 :
(int) ((currentAmount * 100.0) / goalEntity.getTargetAmount());
GoalModel goalModel = GoalModel.convertFrom(goalEntity);
return new GoalModel(
goalModel.getGoalId(),
goalModel.getGoalTitle(),
goalModel.getGoalSubtitle(),
goalModel.getTargetAmount(),
goalModel.getDeadline(),
goalModel.getGoalComplete(),
achievementRate);
}
@Override

View File

@@ -29,8 +29,6 @@ public class GoalController {
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
Long userId = loginUser.getUserId();
//テスト用
// Long userId = 1L;
GoalModel goalModel = goalService.getGoalByUserId(userId);
GoalForm goalForm = GoalForm.convertFrom(goalModel);
@@ -45,9 +43,6 @@ public class GoalController {
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
Long userId = loginUser.getUserId();
//テスト用
// Long userId = 1L;
GoalModel goalModel = goalService.getGoalByUserId(userId);
GoalForm goalForm = GoalForm.convertFrom(goalModel);
@@ -61,9 +56,6 @@ public class GoalController {
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
Long userId = loginUser.getUserId();
//テスト用
// Long userId = 1L;
// 本人の目標かどうかを確認してから更新する
GoalModel currentGoal = goalService.getGoalByUserId(userId);

View File

@@ -25,6 +25,8 @@ public class GoalForm {
private Integer goalComplete;
private Integer achievementRate; // 達成率
public static GoalForm convertFrom(GoalModel goalModel) {
return new GoalForm(
goalModel.getGoalId(),
@@ -32,7 +34,8 @@ public class GoalForm {
goalModel.getGoalSubtitle(),
goalModel.getTargetAmount(),
goalModel.getDeadline(),
goalModel.getGoalComplete());
goalModel.getGoalComplete(),
goalModel.getAchievementRate());
}
// public static List<GoalForm> convertFrom(List<GoalModel> goalModelList) {

View File

@@ -3,8 +3,12 @@
<head>
<meta charset="UTF-8">
<title>目標編集</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div th:replace="~{header :: header}"></div>
<h1>目標編集</h1>
@@ -13,26 +17,29 @@
<div>
<label>目標名</label>
<input type="text" th:field="*{goalTitle}">
<input type="text" th:field="*{goalTitle}" required>
</div>
<div>
<label>目標説明</label>
<textarea th:field="*{goalSubtitle}"></textarea>
<textarea th:field="*{goalSubtitle}" required></textarea>
</div>
<div>
<label>目標金額</label>
<input type="number" th:field="*{targetAmount}">
<input type="number" th:field="*{targetAmount}" required min="1">
</div>
<div>
<label>目標達成期限</label>
<input type="date" th:field="*{deadline}">
<input type="date" th:field="*{deadline}" th:min="${#temporals.format(#temporals.createNow(), 'yyyy-MM-dd')}" required>
</div>
<button type="submit">更新する</button>
</form>
<div th:replace="~{footer :: footer}"></div>
</body>
</html>

View File

@@ -3,8 +3,13 @@
<head>
<meta charset="UTF-8">
<title>目標詳細</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div th:replace="~{header :: header}"></div>
<h1>目標詳細</h1>
@@ -27,9 +32,18 @@
<th>目標達成期限</th>
<td th:text="${goalForm.deadline}"></td>
</tr>
<tr>
<th>達成率</th>
<td th:text="${goalForm.achievementRate} + '%'"></td>
</tr>
</table>
<a th:href="@{/goal/edit}">編集する</a>
<div th:replace="~{footer :: footer}"></div>
</body>
</html>