diff --git a/src/main/java/com/example/demo/app/domain/service/AllocationService.java b/src/main/java/com/example/demo/app/domain/service/AllocationService.java index 753a356..1f922cc 100644 --- a/src/main/java/com/example/demo/app/domain/service/AllocationService.java +++ b/src/main/java/com/example/demo/app/domain/service/AllocationService.java @@ -8,6 +8,4 @@ public interface AllocationService { public List findAll(); - public List save(List allocationModelList); - } 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/AllocationServiceImpl.java b/src/main/java/com/example/demo/app/domain/service/impl/AllocationServiceImpl.java index 5f18d33..d2d836c 100644 --- a/src/main/java/com/example/demo/app/domain/service/impl/AllocationServiceImpl.java +++ b/src/main/java/com/example/demo/app/domain/service/impl/AllocationServiceImpl.java @@ -1,6 +1,5 @@ package com.example.demo.app.domain.service.impl; -import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Service; @@ -26,26 +25,5 @@ public class AllocationServiceImpl implements AllocationService { return AllocationEntity.createAllocationModelList(allocationRepository.findAll()); } - public List save(List allocationModelList) { - - List allocationEntityList = new ArrayList<>(); - - allocationModelList.forEach(allocationModel -> { - allocationEntityList.add( - AllocationModel.convertTo( - categoryRepository.findById(allocationModel.getCategoryId()).get(), - courseRepository.findById(allocationModel.getCourseId()).get(), - allocationModel.getAllocationRatio())); - - }); - - List result = new ArrayList<>(); - for (AllocationEntity allocationEntity : allocationEntityList) { - result.add(AllocationModel.convertFrom(allocationRepository.save(allocationEntity))); - } - - - return result; - } } 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/infra/repository/AllocationRepository.java b/src/main/java/com/example/demo/app/infra/repository/AllocationRepository.java index 28656c2..9546e16 100644 --- a/src/main/java/com/example/demo/app/infra/repository/AllocationRepository.java +++ b/src/main/java/com/example/demo/app/infra/repository/AllocationRepository.java @@ -3,6 +3,7 @@ package com.example.demo.app.infra.repository; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import com.example.demo.app.domain.model.CourseInfoModel; @@ -28,5 +29,9 @@ public interface AllocationRepository extends JpaRepository findAllJoinedAllocation(); + + @Modifying + @Query("delete from AllocationEntity a where a.courseEntity.courseId = :courseId") + void deleteByCourseId(Long courseId); } diff --git a/src/main/java/com/example/demo/app/infra/repository/CourseOwnerRepository.java b/src/main/java/com/example/demo/app/infra/repository/CourseOwnerRepository.java index 0d387cc..0f0cee9 100644 --- a/src/main/java/com/example/demo/app/infra/repository/CourseOwnerRepository.java +++ b/src/main/java/com/example/demo/app/infra/repository/CourseOwnerRepository.java @@ -1,9 +1,15 @@ package com.example.demo.app.infra.repository; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import com.example.demo.app.infra.entity.CourseOwnerEntity; public interface CourseOwnerRepository extends JpaRepository{ + @Modifying + @Query("delete from CourseOwnerEntity c where c.courseEntity.courseId = :courseId") + public void deleteByCourseId(Long courseId); + } 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 diff --git a/src/main/java/com/example/demo/app/presentation/controller/TransactionController.java b/src/main/java/com/example/demo/app/presentation/controller/TransactionController.java index 7a0f253..d1e79fb 100644 --- a/src/main/java/com/example/demo/app/presentation/controller/TransactionController.java +++ b/src/main/java/com/example/demo/app/presentation/controller/TransactionController.java @@ -297,6 +297,23 @@ public class TransactionController { transactionService.findMoneyRecord(transactionId), CategoryForm.convertFrom(categoryService.findByTransactionId(transactionId)).getTransactionType()); + List expenseList = new ArrayList(); + + List incomeList = new ArrayList(); + + for (CategoryForm categoryForm : categoryFormList) { + + if (categoryForm.getTransactionType() == 0) { + expenseList.add(categoryForm); + } else { + incomeList.add(categoryForm); + } + + } + + model.addAttribute(PageReturnAttributeKeyword.EXPENSE_LIST, expenseList); + model.addAttribute(PageReturnAttributeKeyword.INCOME_LIST, incomeList); + model.addAttribute(PageReturnAttributeKeyword.TRANSACTION_ID, transactionId); model.addAttribute(PageReturnAttributeKeyword.TRANSACTION_FORM, transactionForm); @@ -304,6 +321,8 @@ public class TransactionController { model.addAttribute(PageReturnAttributeKeyword.CATEGORY_FORM_LIST, categoryFormList); model.addAttribute(PageReturnAttributeKeyword.TODAY, today); + + session.setAttribute(SessionKeyword.T_TYPE, transactionForm.getTransactionType()); return TransitionTargetPageNameKeyword.TRANSACTION_UPDATE_HTML; diff --git a/src/main/resources/templates/course_create_ratio.html b/src/main/resources/templates/course_create_ratio.html index ad7df48..2af2034 100644 --- a/src/main/resources/templates/course_create_ratio.html +++ b/src/main/resources/templates/course_create_ratio.html @@ -24,60 +24,66 @@
- +

- +

- +

- +

- +

- +

- +

- +
- +
@@ -88,32 +94,40 @@
- + + + + +
+ + +
+
- + diff --git a/src/main/resources/templates/select_course.html b/src/main/resources/templates/select_course.html index 7eea6ee..e2a5b43 100644 --- a/src/main/resources/templates/select_course.html +++ b/src/main/resources/templates/select_course.html @@ -23,6 +23,13 @@
+ + + + + +
+
@@ -35,6 +42,8 @@ aria-controls="'collapse' + ${course.courseId}"> +

@@ -62,6 +71,43 @@
+ + diff --git a/src/main/resources/templates/transaction_create.html b/src/main/resources/templates/transaction_create.html index 53e3bd3..0c818e9 100644 --- a/src/main/resources/templates/transaction_create.html +++ b/src/main/resources/templates/transaction_create.html @@ -113,58 +113,66 @@
- + transactionType.addEventListener("change", function () { + + toggleCategory(); + + categoryId.value = ""; + + }); + + if (tType == "0") { + transactionType.value = "0"; + } else if (tType == "1") { + transactionType.value = "1"; + } + + toggleCategory(); + + diff --git a/src/main/resources/templates/transaction_update.html b/src/main/resources/templates/transaction_update.html index 3817ee4..845047c 100644 --- a/src/main/resources/templates/transaction_update.html +++ b/src/main/resources/templates/transaction_update.html @@ -83,21 +83,36 @@ カテゴリ - + +
@@ -141,6 +156,68 @@
+ + + \ No newline at end of file