[EDIT] コースのコントローラとビュー、ユーザーサービスを修正しました。
This commit is contained in:
@@ -3,5 +3,8 @@ package com.example.demo.app.domain.service;
|
||||
import com.example.demo.app.presentation.form.UserForm;
|
||||
|
||||
public interface UserService {
|
||||
|
||||
void register(UserForm userForm);
|
||||
|
||||
public void updateCourse(Long userId, Long courseId);
|
||||
}
|
||||
|
||||
@@ -57,6 +57,18 @@ public class UserServiceImpl implements UserService {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,13 +1,22 @@
|
||||
package com.example.demo.app.presentation.controller;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
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.CourseService;
|
||||
import com.example.demo.app.domain.service.UserService;
|
||||
import com.example.demo.app.presentation.form.CourseForm;
|
||||
import com.example.demo.app.presentation.form.CourseInfoForm;
|
||||
|
||||
@@ -15,6 +24,7 @@ import lombok.RequiredArgsConstructor;
|
||||
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/course")
|
||||
@RequiredArgsConstructor
|
||||
public class CourseController {
|
||||
|
||||
@@ -22,31 +32,70 @@ public class CourseController {
|
||||
// エラーメッセージ用の定数フィールド
|
||||
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
|
||||
|
||||
// private final HttpSession httpSession;
|
||||
private final HttpSession httpSession;
|
||||
private final CourseService courseService;
|
||||
private final CourseInfoService courseInfoService;
|
||||
private final UserService userService;
|
||||
|
||||
|
||||
|
||||
@GetMapping("/select-course")
|
||||
public String select(Model model) {
|
||||
@PostMapping("/select/register")
|
||||
public String showSelectCourse(@ModelAttribute UserModel userModel, Model model) {
|
||||
|
||||
//try {
|
||||
try {
|
||||
|
||||
model.addAttribute("mode", "select");
|
||||
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;
|
||||
// }
|
||||
} catch (Exception e) {
|
||||
// ログイン情報の破棄
|
||||
httpSession.invalidate();
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
// エラーログ出力
|
||||
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e);
|
||||
// エラー画面遷移
|
||||
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";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -2,25 +2,41 @@
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<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>
|
||||
</head>
|
||||
<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-item" th:each="course:${courseList}">
|
||||
<h2 class="accordion-header">
|
||||
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
|
||||
<h2 class="accordion-header d-flex align-items-center">
|
||||
<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>
|
||||
</button>
|
||||
</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">
|
||||
<span th:text="${course.description}"></span>
|
||||
<table border="1">
|
||||
<tr>
|
||||
<th>カテゴリ名</th><tr>配分(%)</tr>
|
||||
<th>カテゴリ名</th><th>配分(%)</th>
|
||||
</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.allocationRatio}"></td>
|
||||
</tr>
|
||||
@@ -29,7 +45,8 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/js/bootstrap.bundle.min.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user