4 Commits

7 changed files with 66 additions and 19 deletions

View File

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

View File

@@ -29,7 +29,34 @@ public class GoalServiceImpl implements GoalService {
UserEntity userEntity = userRepository.findById(userId).orElseThrow(); UserEntity userEntity = userRepository.findById(userId).orElseThrow();
GoalEntity goalEntity = userEntity.getGoalEntity(); 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 @Override

View File

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

View File

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

View File

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

View File

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

View File

@@ -109,7 +109,7 @@
<label>コース</label> <label>コース</label>
<input type="hidden" th:field="*{courseId}"> <input type="hidden" th:field="*{courseId}">
<span th:text="${selectedCourseName}">未選択</span> <span th:text="${selectedCourseName}">未選択</span>
<button type="button"onclick="location.href='/select-course'">コース選択</button> <button type="submit"onclick="location.href='/course/select/register'">コース選択</button>
</div> </div>
<div> <div>