From fa0bfa7c90d62b83d539cba6f1ad7abbcf2a399e Mon Sep 17 00:00:00 2001 From: s-choi Date: Tue, 29 Sep 2026 15:07:58 +0900 Subject: [PATCH] =?UTF-8?q?[FIX]=20=E9=85=8D=E5=88=86=E3=81=BE=E3=81=A7?= =?UTF-8?q?=E5=85=A5=E5=8A=9B=E3=81=97=E3=81=A6=E3=82=82=E3=82=89=E3=81=A3?= =?UTF-8?q?=E3=81=9F=E3=81=82=E3=81=A8=E3=80=81=E4=BF=9D=E5=AD=98=E3=81=95?= =?UTF-8?q?=E3=82=8C=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F=E3=80=82=E3=82=B3=E3=83=BC?= =?UTF-8?q?=E3=82=B9=E5=89=8A=E9=99=A4=E6=A9=9F=E8=83=BD=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0=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 --- .../app/domain/service/CourseService.java | 5 +- .../service/impl/CourseServiceImpl.java | 119 ++++++++++++++---- .../controller/CourseController.java | 63 +++++----- 3 files changed, 129 insertions(+), 58 deletions(-) diff --git a/src/main/java/com/example/demo/app/domain/service/CourseService.java b/src/main/java/com/example/demo/app/domain/service/CourseService.java index f49459c..a7cbd11 100644 --- a/src/main/java/com/example/demo/app/domain/service/CourseService.java +++ b/src/main/java/com/example/demo/app/domain/service/CourseService.java @@ -3,6 +3,7 @@ package com.example.demo.app.domain.service; import java.util.List; import com.example.demo.app.domain.model.CourseModel; +import com.example.demo.app.presentation.form.AllocationFormList; public interface CourseService { @@ -12,12 +13,14 @@ public interface CourseService { public Long findByName(String courName); - public CourseModel save(CourseModel courseModel, Long userId); + public CourseModel save(CourseModel courseModel, Long userId, AllocationFormList allocationFormList); public List findAllByUserId(Long userId); public List findAllByNull(); + public String deleteCourse(Long courseId, Long userId); + } diff --git a/src/main/java/com/example/demo/app/domain/service/impl/CourseServiceImpl.java b/src/main/java/com/example/demo/app/domain/service/impl/CourseServiceImpl.java index cdba07c..59e0f99 100644 --- a/src/main/java/com/example/demo/app/domain/service/impl/CourseServiceImpl.java +++ b/src/main/java/com/example/demo/app/domain/service/impl/CourseServiceImpl.java @@ -7,14 +7,19 @@ import jakarta.transaction.Transactional; import org.springframework.stereotype.Service; +import com.example.demo.app.domain.model.AllocationModel; import com.example.demo.app.domain.model.CourseModel; import com.example.demo.app.domain.service.CourseService; +import com.example.demo.app.infra.entity.AllocationEntity; import com.example.demo.app.infra.entity.CourseEntity; import com.example.demo.app.infra.entity.CourseOwnerEntity; import com.example.demo.app.infra.entity.UserEntity; +import com.example.demo.app.infra.repository.AllocationRepository; +import com.example.demo.app.infra.repository.CategoryRepository; import com.example.demo.app.infra.repository.CourseOwnerRepository; import com.example.demo.app.infra.repository.CourseRepository; import com.example.demo.app.infra.repository.UserRepository; +import com.example.demo.app.presentation.form.AllocationFormList; import lombok.RequiredArgsConstructor; @@ -22,53 +27,121 @@ import lombok.RequiredArgsConstructor; @RequiredArgsConstructor public class CourseServiceImpl implements CourseService { + private final AllocationRepository allocationRepository; + private final CourseRepository courseRepository; private final UserRepository userRepository; private final CourseOwnerRepository courseOwnerRepository; - - public List findAll(){ - + private final CategoryRepository categoryRepository; + + public List findAll() { + return CourseEntity.createCourseModelList(courseRepository.findAll()); } - - public List findAllByUserId(Long userId) { - + + public List findAllByUserId(Long userId) { + return CourseModel.convertFrom(courseRepository.findAllByUserId(userId)); } - + public List findAllByNull() { return CourseModel.convertFrom(courseRepository.findAllByNull()); } - + public CourseModel findById(Long courseId) { return CourseModel.convertFrom(courseRepository.findByCourseId(courseId)); } - + public Long findByName(String courseName) { return courseRepository.findByCourseName(courseName).getCourseId(); } - + @Transactional - public CourseModel save(CourseModel courseModel, Long userId) { - - List userEntityList = new ArrayList<>(); - userEntityList.add(userRepository.findById(userId).get()); - CourseEntity result = courseRepository.save( CourseModel.convertTo(courseModel)); - + public CourseModel save(CourseModel courseModel, Long userId, AllocationFormList allocationFormList) { + + List userEntityList = new ArrayList<>(); + userEntityList.add(userRepository.findById(userId).get()); + CourseEntity result = courseRepository.save(CourseModel.convertTo(courseModel)); + + List allocationModelList = new ArrayList<>(); + + allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId4(), + result.getCourseId(), + allocationFormList.getAllocationRatio4())); + allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId5(), + result.getCourseId(), + allocationFormList.getAllocationRatio5())); + allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId6(), + result.getCourseId(), + allocationFormList.getAllocationRatio6())); + allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId7(), + result.getCourseId(), + allocationFormList.getAllocationRatio7())); + allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId8(), + result.getCourseId(), + allocationFormList.getAllocationRatio8())); + allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId9(), + result.getCourseId(), + allocationFormList.getAllocationRatio9())); + allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId10(), + result.getCourseId(), + allocationFormList.getAllocationRatio10())); + allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId11(), + result.getCourseId(), + allocationFormList.getAllocationRatio11())); + + allocationModelList.stream().forEach(allocation -> { + System.out.println(allocation.getCourseId()); + }); + UserEntity user = userRepository.findById(userId).orElse(null); - + user.setCourseEntity(result); - + UserEntity userResult = userRepository.save(user); - + CourseOwnerEntity courseOwnerEntity = new CourseOwnerEntity(); courseOwnerEntity.setUserEntity(userResult); courseOwnerEntity.setCourseEntity(result); - + courseOwnerRepository.save(courseOwnerEntity); - + + List allocationEntityList = new ArrayList<>(); + + allocationModelList.forEach(allocationModel -> { + allocationEntityList.add( + AllocationModel.convertTo( + categoryRepository.findById(allocationModel.getCategoryId()).get(), + courseRepository.findById(allocationModel.getCourseId()).get(), + allocationModel.getAllocationRatio())); + + }); + + List resultList = new ArrayList<>(); + for (AllocationEntity allocationEntity : allocationEntityList) { + resultList.add(AllocationModel.convertFrom(allocationRepository.save(allocationEntity))); + } + return CourseModel.convertFrom(result); - - + + } + + @Transactional + public String deleteCourse(Long courseId, Long userId) { + + UserEntity userEntity = userRepository.findById(userId).orElse(null); + System.out.println("***********************ここでエラー1"); + if (userEntity.getCourseEntity().getCourseId() == courseId) { + return "selected"; + } + System.out.println("***********************ここでエラー2"); + allocationRepository.deleteByCourseId(courseId); + System.out.println("***********************ここでエラー3"); + courseOwnerRepository.deleteByCourseId(courseId); + System.out.println("***********************ここでエラー4"); + courseRepository.deleteById(courseId); + System.out.println("***********************ここでエラー5"); + return "ok"; + } } 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 a9b92e4..7f047ba 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 @@ -13,7 +13,6 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import com.example.demo.aop.aspect.SessionCheck; -import com.example.demo.app.domain.model.AllocationModel; import com.example.demo.app.domain.model.CourseModel; import com.example.demo.app.domain.model.LoginModel; import com.example.demo.app.domain.service.AllocationService; @@ -111,48 +110,44 @@ public class CourseController { LoginModel loginUser = (LoginModel) session.getAttribute(SessionKeyword.LOGIN_USER); - CourseForm form = CourseForm - .convertFrom(courseService.save(CourseForm.convertTo(courseForm), loginUser.getUserId())); - - session.setAttribute(PageReturnAttributeKeyword.COURSE_ID, form.getCourseId()); + model.addAttribute("courseForm", courseForm); return TransitionTargetPageNameKeyword.COURSE_CREATE_RATIO_HTML; } @SessionCheck @PostMapping(TransitionTargetPageNameKeyword.COURSE_CREATE) - public String createCoursePost(@ModelAttribute AllocationFormList allocationFormList, HttpSession session) { + public String createCoursePost(@ModelAttribute CourseForm courseForm, + @ModelAttribute AllocationFormList allocationFormList, HttpSession session) { + LoginModel loginUser = (LoginModel) session.getAttribute(SessionKeyword.LOGIN_USER); - List allocationModelList = new ArrayList<>(); - - allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId4(), - (Long) session.getAttribute(PageReturnAttributeKeyword.COURSE_ID), - allocationFormList.getAllocationRatio4())); - allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId5(), - (Long) session.getAttribute(PageReturnAttributeKeyword.COURSE_ID), - allocationFormList.getAllocationRatio5())); - allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId6(), - (Long) session.getAttribute(PageReturnAttributeKeyword.COURSE_ID), - allocationFormList.getAllocationRatio6())); - allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId7(), - (Long) session.getAttribute(PageReturnAttributeKeyword.COURSE_ID), - allocationFormList.getAllocationRatio7())); - allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId8(), - (Long) session.getAttribute(PageReturnAttributeKeyword.COURSE_ID), - allocationFormList.getAllocationRatio8())); - allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId9(), - (Long) session.getAttribute(PageReturnAttributeKeyword.COURSE_ID), - allocationFormList.getAllocationRatio9())); - allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId10(), - (Long) session.getAttribute(PageReturnAttributeKeyword.COURSE_ID), - allocationFormList.getAllocationRatio10())); - allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId11(), - (Long) session.getAttribute(PageReturnAttributeKeyword.COURSE_ID), - allocationFormList.getAllocationRatio11())); - - allocationService.save(allocationModelList); + courseService.save(CourseForm.convertTo(courseForm), loginUser.getUserId(), allocationFormList); return TransitionTargetPageNameKeyword.REDIRECT + TransitionTargetPageNameKeyword.COURSE; } + @SessionCheck + @PostMapping("/course/delete") + public String createCoursePost(@RequestParam Long courseId, HttpSession session, Model model) { + + LoginModel loginUser = (LoginModel) session.getAttribute(SessionKeyword.LOGIN_USER); + + System.out.println("**********************cousrId: " + courseId); + String condition = courseService.deleteCourse(courseId, loginUser.getUserId()); + + if (condition.equals("selected")) { + + model.addAttribute("message", "適用中のコースは選べません"); + + return TransitionTargetPageNameKeyword.REDIRECT + TransitionTargetPageNameKeyword.COURSE; + + } else if (condition.equals("ok")) { + return TransitionTargetPageNameKeyword.REDIRECT + TransitionTargetPageNameKeyword.COURSE; + } + model.addAttribute("message", "エラーが発生しました"); + + return TransitionTargetPageNameKeyword.REDIRECT + TransitionTargetPageNameKeyword.COURSE; + + } + } \ No newline at end of file