[EDIT] コースのコントローラとビュー、ユーザーサービスを修正しました。

This commit is contained in:
kkazuya
2026-09-18 14:16:00 +09:00
parent ad1121d694
commit 9259a53bad
4 changed files with 105 additions and 24 deletions

View File

@@ -3,5 +3,8 @@ package com.example.demo.app.domain.service;
import com.example.demo.app.presentation.form.UserForm; import com.example.demo.app.presentation.form.UserForm;
public interface UserService { public interface UserService {
void register(UserForm userForm); void register(UserForm userForm);
public void updateCourse(Long userId, Long courseId);
} }

View File

@@ -57,6 +57,18 @@ public class UserServiceImpl implements UserService {
userRepository.save(userEntity); userRepository.save(userEntity);
} }
@Override
public void updateCourse(Long userId, Long courseId) {
UserEntity userEntity = userRepository.findById(userId).orElseThrow();
CourseEntity courseEntity = courseRepository.findById(courseId).orElseThrow();
userEntity.setCourseEntity(courseEntity);
userRepository.save(userEntity);
}
} }

View File

@@ -1,13 +1,22 @@
package com.example.demo.app.presentation.controller; package com.example.demo.app.presentation.controller;
import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.example.demo.app.domain.model.LoginModel;
import com.example.demo.app.domain.model.UserModel;
import com.example.demo.app.domain.service.CourseInfoService; import com.example.demo.app.domain.service.CourseInfoService;
import com.example.demo.app.domain.service.CourseService; import com.example.demo.app.domain.service.CourseService;
import com.example.demo.app.domain.service.UserService;
import com.example.demo.app.presentation.form.CourseForm; import com.example.demo.app.presentation.form.CourseForm;
import com.example.demo.app.presentation.form.CourseInfoForm; import com.example.demo.app.presentation.form.CourseInfoForm;
@@ -15,6 +24,7 @@ import lombok.RequiredArgsConstructor;
@Controller @Controller
@RequestMapping("/course")
@RequiredArgsConstructor @RequiredArgsConstructor
public class CourseController { public class CourseController {
@@ -22,31 +32,70 @@ public class CourseController {
// エラーメッセージ用の定数フィールド // エラーメッセージ用の定数フィールド
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!"; private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
// private final HttpSession httpSession; private final HttpSession httpSession;
private final CourseService courseService; private final CourseService courseService;
private final CourseInfoService courseInfoService; private final CourseInfoService courseInfoService;
private final UserService userService;
@GetMapping("/select-course") @PostMapping("/select/register")
public String select(Model model) { public String showSelectCourse(@ModelAttribute UserModel userModel, Model model) {
//try { try {
model.addAttribute("mode", "select");
model.addAttribute("courseList", CourseForm.convertFrom(courseService.findAll())); model.addAttribute("courseList", CourseForm.convertFrom(courseService.findAll()));
model.addAttribute("courseInfoList", CourseInfoForm.convertFrom(courseInfoService.findAll())); model.addAttribute("courseInfoList", CourseInfoForm.convertFrom(courseInfoService.findAll()));
return "select_course"; return "select_course";
//} catch (Exception e) { } catch (Exception e) {
// // ログイン情報の破棄 // ログイン情報の破棄
// httpSession.invalidate(); httpSession.invalidate();
//
// e.printStackTrace(); e.printStackTrace();
//
// // エラーログ出力 // エラーログ出力
// LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e); LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e);
// // エラー画面遷移 // エラー画面遷移
// return null; return null;
// }
} }
}
@PostMapping("/select/change")
public String showChangeCourse(Model model) {
try {
model.addAttribute("mode", "change");
model.addAttribute("courseList", CourseForm.convertFrom(courseService.findAll()));
model.addAttribute("courseInfoList", CourseInfoForm.convertFrom(courseInfoService.findAll()));
return "select_course";
} catch (Exception e) {
// ログイン情報の破棄
httpSession.invalidate();
e.printStackTrace();
// エラーログ出力
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e);
// エラー画面遷移
return null;
}
}
@PostMapping("/change")
public String changeCourse(@RequestParam Long courseId) {
LoginModel loginUser = (LoginModel) httpSession.getAttribute("loginUser");
userService.updateCourse(loginUser.getUserId(), courseId);
return "redirect:/menu";
}
} }

View File

@@ -2,25 +2,41 @@
<html xmlns:th="http://www.thymeleaf.org"> <html xmlns:th="http://www.thymeleaf.org">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
<title>コース選択</title> <title>コース選択</title>
</head> </head>
<body> <body>
<h1>コース選択</h1>
<form th:action="@{/user/register}" th:object="${userForm}" method="post">
<input type="hidden" th:field="*{userName}">
<input type="hidden" th:field="*{mailAddress}">
<input type="hidden" th:field="*{birthDate}">
<input type="hidden" th:field="*{password}">
<input type="hidden" th:field="*{secretQuestionId}">
<input type="hidden" th:field="*{secretAnswer}">
<input type="hidden" th:field="*{saving}">
<input type="hidden" th:field="*{annualIncome}">
<input type="hidden" th:field="*{monthlyBudget}">
<input type="hidden" th:field="*{targetAmount}">
<button type="submit">確定</button>
<div class="accordion" id="accordionExample"> <div class="accordion" id="accordionExample">
<div class="accordion-item" th:each="course:${courseList}"> <div class="accordion-item" th:each="course:${courseList}">
<h2 class="accordion-header"> <h2 class="accordion-header d-flex align-items-center">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne"> <input type="radio" name="courseId" th:value="${course.courseId}" class="form-check-input ms-3 me-2">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" th:data-bs-target="'#collapse' + ${course.courseId}" aria-expanded="false" aria-controls="'collapse' + ${course.courseId}">
<span th:text="${course.courseName}"></span> <span th:text="${course.courseName}"></span>
</button> </button>
</h2> </h2>
<div id="collapseOne" class="accordion-collapse collapse show" data-bs-parent="#accordionExample"> <div th:id="'collapse' + ${course.courseId}" class="accordion-collapse collapse" data-bs-parent="#accordionExample">
<div class="accordion-body"> <div class="accordion-body">
<span th:text="${course.description}"></span> <span th:text="${course.description}"></span>
<table border="1"> <table border="1">
<tr> <tr>
<th>カテゴリ名</th><tr>配分(%)</tr> <th>カテゴリ名</th><th>配分(%)</th>
</tr> </tr>
<tr th:each="courseInfo:${courseInfoList}" th:if="course.courseId == courseInfo.courseId"> <tr th:each="courseInfo:${courseInfoList}" th:if="${course.courseId == courseInfo.courseId}">
<td th:text="${courseInfo.categoryName}"></td> <td th:text="${courseInfo.categoryName}"></td>
<td th:text="${courseInfo.allocationRatio}"></td> <td th:text="${courseInfo.allocationRatio}"></td>
</tr> </tr>
@@ -29,7 +45,8 @@
</div> </div>
</div> </div>
</div> </div>
</form>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/js/bootstrap.bundle.min.js"></script>
</body> </body>
</html> </html>