8 Commits

11 changed files with 306 additions and 169 deletions

View File

@@ -8,6 +8,4 @@ public interface AllocationService {
public List<AllocationModel> findAll();
public List<AllocationModel> save(List<AllocationModel> allocationModelList);
}

View File

@@ -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<CourseModel> findAllByUserId(Long userId);
public List<CourseModel> findAllByNull();
public String deleteCourse(Long courseId, Long userId);
}

View File

@@ -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<AllocationModel> save(List<AllocationModel> allocationModelList) {
List<AllocationEntity> allocationEntityList = new ArrayList<>();
allocationModelList.forEach(allocationModel -> {
allocationEntityList.add(
AllocationModel.convertTo(
categoryRepository.findById(allocationModel.getCategoryId()).get(),
courseRepository.findById(allocationModel.getCourseId()).get(),
allocationModel.getAllocationRatio()));
});
List<AllocationModel> result = new ArrayList<>();
for (AllocationEntity allocationEntity : allocationEntityList) {
result.add(AllocationModel.convertFrom(allocationRepository.save(allocationEntity)));
}
return result;
}
}

View File

@@ -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,11 +27,14 @@ 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;
private final CategoryRepository categoryRepository;
public List<CourseModel> findAll(){
public List<CourseModel> findAll() {
return CourseEntity.createCourseModelList(courseRepository.findAll());
}
@@ -49,11 +57,42 @@ public class CourseServiceImpl implements CourseService {
}
@Transactional
public CourseModel save(CourseModel courseModel, Long userId) {
public CourseModel save(CourseModel courseModel, Long userId, AllocationFormList allocationFormList) {
List<UserEntity> userEntityList = new ArrayList<>();
userEntityList.add(userRepository.findById(userId).get());
CourseEntity result = courseRepository.save( CourseModel.convertTo(courseModel));
List<UserEntity> userEntityList = new ArrayList<>();
userEntityList.add(userRepository.findById(userId).get());
CourseEntity result = courseRepository.save(CourseModel.convertTo(courseModel));
List<AllocationModel> 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);
@@ -67,8 +106,42 @@ public class CourseServiceImpl implements CourseService {
courseOwnerRepository.save(courseOwnerEntity);
List<AllocationEntity> allocationEntityList = new ArrayList<>();
allocationModelList.forEach(allocationModel -> {
allocationEntityList.add(
AllocationModel.convertTo(
categoryRepository.findById(allocationModel.getCategoryId()).get(),
courseRepository.findById(allocationModel.getCourseId()).get(),
allocationModel.getAllocationRatio()));
});
List<AllocationModel> 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";
}
}

View File

@@ -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;
@@ -29,4 +30,8 @@ public interface AllocationRepository extends JpaRepository<AllocationEntity, Al
+ " a.categoryEntity.categoryId ASC ")
List<CourseInfoModel> findAllJoinedAllocation();
@Modifying
@Query("delete from AllocationEntity a where a.courseEntity.courseId = :courseId")
void deleteByCourseId(Long courseId);
}

View File

@@ -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<CourseOwnerEntity, Long>{
@Modifying
@Query("delete from CourseOwnerEntity c where c.courseEntity.courseId = :courseId")
public void deleteByCourseId(Long courseId);
}

View File

@@ -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<AllocationModel> 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;
}
}

View File

@@ -24,58 +24,64 @@
<div class="mb-3">
<label class="form-label" for="allocationRatio4">固定費</label>
<input class="form-control" type="number" min="0" max="100" onchange="inputChange()" id="allocationRatio4" th:field="*{allocationRatio4}" required>
<input class="form-control" type="number" min="0" max="100" onchange="inputChange()"
id="allocationRatio4" th:field="*{allocationRatio4}" required>
</div>
<br>
<div class="mb-3">
<label class="form-label" for="allocationRatio5">食費</label>
<input class="form-control" type="number" min="0" max="100" onchange="inputChange()" id="allocationRatio5" th:field="*{allocationRatio5}" required>
<input class="form-control" type="number" min="0" max="100" onchange="inputChange()"
id="allocationRatio5" th:field="*{allocationRatio5}" required>
</div>
<br>
<div class="mb-3">
<label class="form-label" for="allocationRatio6">交通費</label>
<input class="form-control" type="number" min="0" max="100" onchange="inputChange()" id="allocationRatio6" th:field="*{allocationRatio6}" required>
<input class="form-control" type="number" min="0" max="100" onchange="inputChange()"
id="allocationRatio6" th:field="*{allocationRatio6}" required>
</div>
<br>
<div class="mb-3">
<label class="form-label" for="allocationRatio7">日用品費</label>
<input class="form-control" type="number" min="0" max="100" onchange="inputChange()" id="allocationRatio7" th:field="*{allocationRatio7}" required>
<input class="form-control" type="number" min="0" max="100" onchange="inputChange()"
id="allocationRatio7" th:field="*{allocationRatio7}" required>
</div>
<br>
<div class="mb-3">
<label class="form-label" for="allocationRatio8">被服/美容費</label>
<input class="form-control" type="number" min="0" max="100" onchange="inputChange()" id="allocationRatio8" th:field="*{allocationRatio8}" required>
<input class="form-control" type="number" min="0" max="100" onchange="inputChange()"
id="allocationRatio8" th:field="*{allocationRatio8}" required>
</div>
<br>
<div class="mb-3">
<label class="form-label" for="allocationRatio9">医療費</label>
<input class="form-control" type="number" min="0" max="100" onchange="inputChange()" id="allocationRatio9" th:field="*{allocationRatio9}" required>
<input class="form-control" type="number" min="0" max="100" onchange="inputChange()"
id="allocationRatio9" th:field="*{allocationRatio9}" required>
</div>
<br>
<div class="mb-3">
<label class="form-label" for="allocationRatio10">趣味/娯楽費</label>
<input class="form-control" type="number" min="0" max="100" onchange="inputChange()" id="allocationRatio10"
th:field="*{allocationRatio10}" required>
<input class="form-control" type="number" min="0" max="100" onchange="inputChange()"
id="allocationRatio10" th:field="*{allocationRatio10}" required>
</div>
<br>
<div class="mb-3">
<label class="form-label" for="allocationRatio11">その他</label>
<input class="form-control" type="number" min="0" max="100" onchange="inputChange()" id="allocationRatio11"
th:field="*{allocationRatio11}" required>
<input class="form-control" type="number" min="0" max="100" onchange="inputChange()"
id="allocationRatio11" th:field="*{allocationRatio11}" required>
</div>
<div class="mb-3">
@@ -88,23 +94,31 @@
<button type="submit" class="btn btn-primary" id="submit" disabled="true">登録</button>
</div>
</form>
<span th:object="${courseForm}">
<input type="hidden" id="courseName" th:field="*{courseName}" required>
<br>
<input type="hidden" id="description" th:field="*{description}">
</span>
</div>
</div>
</div>
</div>
<div th:replace="~{footer :: footer}"></div>
<script>
function inputChange(){
let total=0;
function inputChange() {
let total = 0;
for(let i=4; i<=11; i++){
for (let i = 4; i <= 11; i++) {
total += Number(document.getElementById(`allocationRatio${i}`).value || 0);
if(total == 100){
if (total == 100) {
document.getElementById('submit').disabled = false;
}else{
} else {
document.getElementById('submit').disabled = true;
}
}

View File

@@ -13,6 +13,7 @@
<div th:replace="~{header :: header}"></div>
<h1 class="page-title">コース選択</h1>
<<<<<<< HEAD
<div class="d-grid gap-2 mt-4">
<th:block th:if="${mode == 'change'}">
<form th:action="@{/course/create}" method="get">
@@ -23,6 +24,15 @@
</th:block>
<form th:action="${mode == 'regist'} ? @{/user/register/return}: @{/change}" method="post">
=======
<th:block th:if="${mode == 'change'}">
<form th:action="@{/course/create}" method="get">
<button type="submit">新規登録</button>
</form>
</th:block>
<form th:action="${mode == 'regist'} ? @{/user/register/return}: @{/change}" id="totalForm" method="post">
>>>>>>> 65b4a9e54a5bc5c1d1480fd4b9255addf73628e4
<button type="submit" class="btn btn-primary w-100">確定</button>
<div class="accordion" id="accordionExample">
@@ -35,6 +45,8 @@
aria-controls="'collapse' + ${course.courseId}">
<span th:text="${course.courseName}"></span>
</button>
<p style="width: 120px;"><button type="button" class="btn btn-danger w-100"
th:attr="data-course-id=${course.courseId}" onclick="deleteCourse(this)">削除</button></p>
</h2>
<div th:id="'collapse' + ${course.courseId}" class="accordion-collapse collapse"
data-bs-parent="#accordionExample" style="padding-bottom: 120px;">
@@ -62,6 +74,43 @@
<div th:replace="~{footer :: footer}"></div>
</th:block>
<script th:inline="javascript">
const message = /*[[${message}]]*/ null;
if (message != null) {
alert(message);
}
function deleteCourse(button) {
const courseId = button.dataset.courseId;
const totalForm = document.getElementById("totalForm");
const input = document.createElement("input");
const radioButtons = totalForm.querySelectorAll('input[name="courseId"]');
radioButtons.forEach(radio => {
radio.disabled = true;
});
input.type = "hidden";
input.name = "courseId";
input.value = courseId;
totalForm.appendChild(input);
totalForm.action = "/course/delete";
totalForm.method = "post";
courseId.value = courseId;
totalForm.submit();
}
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/js/bootstrap.bundle.min.js"></script>
</body>

View File

@@ -113,58 +113,66 @@
<div th:replace="~{footer :: footer}"></div>
<script inline="javascript">
const transactionType = document.getElementById("transactionType");
const categoryId = document.getElementById("categoryId");
<script th:inline="javascript">
const transactionType = document.getElementById("transactionType");
const categoryId = document.getElementById("categoryId");
const expenseList = document.getElementsByClassName("expenseListClass");
const incomeList = document.getElementsByClassName("incomeListClass");
const expenseList = document.getElementsByClassName("expenseListClass");
const incomeList = document.getElementsByClassName("incomeListClass");
const tType = '[[${session.tType}]]';
const tType = '[[${session.tType}]]';
if (tType == "0") {
transactionType.value = 0;
} else if (tType == "1") {
transactionType.value = 1;
}
function toggleCategory() {
if (transactionType.value === "0") {
transactionType.addEventListener("change", function () {
for (let i = 0; i < expenseList.length; i++) {
expenseList[i].style.display = "";
}
if (this.value === "0") {
for (let i = 0; i < incomeList.length; i++) {
incomeList[i].style.display = "none";
}
for (let i = 0; i < expenseList.length; i++) {
expenseList[i].style.display = "";
}
} else if (transactionType.value === "1") {
for (let i = 0; i < incomeList.length; i++) {
incomeList[i].style.display = "none";
}
for (let i = 0; i < expenseList.length; i++) {
expenseList[i].style.display = "none";
}
} else if (this.value === "1") {
for (let i = 0; i < incomeList.length; i++) {
incomeList[i].style.display = "";
}
for (let i = 0; i < expenseList.length; i++) {
expenseList[i].style.display = "none";
}
} else {
for (let i = 0; i < incomeList.length; i++) {
incomeList[i].style.display = "";
}
for (let i = 0; i < expenseList.length; i++) {
expenseList[i].style.display = "none";
}
} else {
for (let i = 0; i < expenseList.length; i++) {
expenseList[i].style.display = "none";
}
for (let i = 0; i < incomeList.length; i++) {
incomeList[i].style.display = "none";
for (let i = 0; i < incomeList.length; i++) {
incomeList[i].style.display = "none";
}
}
}
categoryId.value = "";
});
</script>
transactionType.addEventListener("change", function () {
toggleCategory();
categoryId.value = "";
});
if (tType == "0") {
transactionType.value = "0";
} else if (tType == "1") {
transactionType.value = "1";
}
toggleCategory();
</script>
</body>

View File

@@ -156,7 +156,7 @@
<div th:replace="~{footer :: footer}">
</div>
<script inline="javascript">
<script th:inline="javascript">
const transactionType = document.getElementById("transactionType");
const categoryId = document.getElementById("categoryId");
@@ -165,36 +165,29 @@
const tType = '[[${session.tType}]]';
if (tType == "0") {
transactionType.value = 0;
} else if (tType == "1") {
transactionType.value = 1;
}
function toggleCategory() {
if (transactionType.value === "0") {
transactionType.addEventListener("change", function () {
for (let i = 0; i < expenseList.length; i++) {
expenseList[i].style.display = "";
}
if (this.value === "0") {
for (let i = 0; i < incomeList.length; i++) {
incomeList[i].style.display = "none";
}
for (let i = 0; i < expenseList.length; i++) {
expenseList[i].style.display = "";
}
} else if (transactionType.value === "1") {
for (let i = 0; i < incomeList.length; i++) {
incomeList[i].style.display = "none";
}
for (let i = 0; i < expenseList.length; i++) {
expenseList[i].style.display = "none";
}
} else if (this.value === "1") {
for (let i = 0; i < incomeList.length; i++) {
incomeList[i].style.display = "";
}
for (let i = 0; i < expenseList.length; i++) {
expenseList[i].style.display = "none";
}
for (let i = 0; i < incomeList.length; i++) {
incomeList[i].style.display = "";
}
} else {
} else {
for (let i = 0; i < expenseList.length; i++) {
expenseList[i].style.display = "none";
@@ -204,9 +197,24 @@
incomeList[i].style.display = "none";
}
}
}
transactionType.addEventListener("change", function () {
toggleCategory();
categoryId.value = "";
categoryId.value = "";
});
if (tType == "0") {
transactionType.value = "0";
} else if (tType == "1") {
transactionType.value = "1";
}
toggleCategory();
</script>