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 @@
+ +