From 9259a53badb54010dcb1afec04336ceb5626fea1 Mon Sep 17 00:00:00 2001 From: kkazuya Date: Fri, 18 Sep 2026 14:16:00 +0900 Subject: [PATCH] =?UTF-8?q?[EDIT]=20=E3=82=B3=E3=83=BC=E3=82=B9=E3=81=AE?= =?UTF-8?q?=E3=82=B3=E3=83=B3=E3=83=88=E3=83=AD=E3=83=BC=E3=83=A9=E3=81=A8?= =?UTF-8?q?=E3=83=93=E3=83=A5=E3=83=BC=E3=80=81=E3=83=A6=E3=83=BC=E3=82=B6?= =?UTF-8?q?=E3=83=BC=E3=82=B5=E3=83=BC=E3=83=93=E3=82=B9=E3=82=92=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/app/domain/service/UserService.java | 5 +- .../domain/service/impl/UserServiceImpl.java | 12 +++ .../controller/CourseController.java | 81 +++++++++++++++---- .../resources/templates/select_course.html | 31 +++++-- 4 files changed, 105 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/example/demo/app/domain/service/UserService.java b/src/main/java/com/example/demo/app/domain/service/UserService.java index 6dfecaa..6ad3c04 100644 --- a/src/main/java/com/example/demo/app/domain/service/UserService.java +++ b/src/main/java/com/example/demo/app/domain/service/UserService.java @@ -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); + + void register(UserForm userForm); + + public void updateCourse(Long userId, Long courseId); } diff --git a/src/main/java/com/example/demo/app/domain/service/impl/UserServiceImpl.java b/src/main/java/com/example/demo/app/domain/service/impl/UserServiceImpl.java index edb6bb4..d93f36b 100644 --- a/src/main/java/com/example/demo/app/domain/service/impl/UserServiceImpl.java +++ b/src/main/java/com/example/demo/app/domain/service/impl/UserServiceImpl.java @@ -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); + } + } diff --git a/src/main/java/com/example/demo/app/presentation/controller/CourseController.java b/src/main/java/com/example/demo/app/presentation/controller/CourseController.java index ff520ae..7602769 100644 --- a/src/main/java/com/example/demo/app/presentation/controller/CourseController.java +++ b/src/main/java/com/example/demo/app/presentation/controller/CourseController.java @@ -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"; + } + + } diff --git a/src/main/resources/templates/select_course.html b/src/main/resources/templates/select_course.html index bd50a1e..7083240 100644 --- a/src/main/resources/templates/select_course.html +++ b/src/main/resources/templates/select_course.html @@ -2,25 +2,41 @@ + + コース選択 +

コース選択

+
+ + + + + + + + + + +
-

-

-
+
- 配分(%) + - + @@ -29,7 +45,8 @@ + - + \ No newline at end of file
カテゴリ名
カテゴリ名配分(%)