5 Commits

12 changed files with 368 additions and 14 deletions

View File

@@ -3,6 +3,7 @@ package com.example.demo.app.domain.model;
import java.util.List;
import com.example.demo.app.infra.entity.CategoryEntity;
import com.example.demo.app.infra.entity.UserEntity;
import lombok.AllArgsConstructor;
import lombok.Getter;
@@ -27,4 +28,15 @@ public class CategoryModel {
return categoryEntityList.stream().map(CategoryModel::convertFrom).toList();
}
public static CategoryEntity convertTo(CategoryModel categoryModel, UserEntity userEntity) {
return new CategoryEntity(
categoryModel.getCategoryId(),
categoryModel.getCategoryName(),
categoryModel.getTransactionType(),
userEntity,
0,
null,
null);
}
}

View File

@@ -13,4 +13,12 @@ public interface CategoryService {
public CategoryModel findOneByCategoryName(String categoryName);
public CategoryModel findByTransactionId(Long transactionId);
public List<CategoryModel> findAllByuserId(Long userId);
public void delete(Long categoryId);
public CategoryModel save(CategoryModel categoryModel, Long userId);
public List<CategoryModel> findAllExceptCustom();
}

View File

@@ -9,6 +9,7 @@ import com.example.demo.app.domain.model.CategoryModel;
import com.example.demo.app.domain.service.CategoryService;
import com.example.demo.app.infra.entity.CategoryEntity;
import com.example.demo.app.infra.repository.CategoryRepository;
import com.example.demo.app.infra.repository.UserRepository;
import lombok.RequiredArgsConstructor;
@@ -17,6 +18,7 @@ import lombok.RequiredArgsConstructor;
public class CategoryServiceImpl implements CategoryService {
private final CategoryRepository categoryRepository;
private final UserRepository userRepository;
public List<CategoryModel> findAll() {
@@ -52,5 +54,29 @@ public class CategoryServiceImpl implements CategoryService {
return CategoryModel.convertFrom(categoryRepository.findByTransactionId(transactionId));
}
public List<CategoryModel> findAllByuserId(Long userId) {
return CategoryModel.convertFrom(categoryRepository.findByUserId(userId));
}
public void delete(Long categoryId) {
categoryRepository.deleteById(categoryId);
}
public CategoryModel save(CategoryModel categoryModel, Long userId) {
return CategoryModel.convertFrom(
categoryRepository.save(CategoryModel.convertTo(categoryModel, userRepository.findById(userId).get())));
}
public List<CategoryModel> findAllExceptCustom() {
return CategoryModel.convertFrom(categoryRepository.findAllExceptCustom());
}
}

View File

@@ -4,9 +4,12 @@ import java.util.List;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
@@ -32,6 +35,13 @@ public class CategoryEntity {
@Column(name = "transaction_type", nullable = false)
private Integer transactionType;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private UserEntity userEntity;
@Column(name = "delete_flag")
private Integer deleteFlag;
@OneToMany(mappedBy = "categoryEntity")
private List<TransactionEntity> transactionEntity;

View File

@@ -1,5 +1,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.Query;
import org.springframework.data.repository.query.Param;
@@ -15,4 +17,10 @@ public interface CategoryRepository extends JpaRepository<CategoryEntity, Long>
@Query("select c from CategoryEntity c inner join TransactionEntity t on c.categoryId = t.categoryEntity.categoryId where t.transactionId = :transactionId")
public CategoryEntity findByTransactionId(Long transactionId);
@Query("select c from CategoryEntity c where c.userEntity.userId = :userId")
public List<CategoryEntity> findByUserId(Long userId);
@Query("select c from CategoryEntity c where c.userEntity.userId is null")
public List<CategoryEntity> findAllExceptCustom();
}

View File

@@ -0,0 +1,87 @@
package com.example.demo.app.presentation.controller;
import java.util.List;
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 com.example.demo.app.domain.model.LoginModel;
import com.example.demo.app.domain.service.CategoryService;
import com.example.demo.app.presentation.form.CategoryForm;
import lombok.RequiredArgsConstructor;
@Controller
@RequiredArgsConstructor
public class CategoryController {
private final CategoryService categoryService;
@GetMapping("/category/list")
public String categoryListGet(Model model, HttpSession session) {
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
List<CategoryForm> categoryFormList = CategoryForm.convertFrom(categoryService.findAllByuserId(loginUser.getUserId()));
model.addAttribute("categoryFormList", categoryFormList);
return "category-list";
}
@GetMapping("/category/create")
public String categoryCreateGet(@ModelAttribute CategoryForm categoryForm) {
return "category-create";
}
@PostMapping("/category/create")
public String categoryCreatePost(@ModelAttribute CategoryForm categoryForm, HttpSession session) {
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
categoryService.save(CategoryForm.convertTo(categoryForm), loginUser.getUserId());
return "redirect:/category/list";
}
@GetMapping("/category/update")
public String categoryUpdateGet(Model model, Long categoryId) {
CategoryForm categoryForm = CategoryForm.convertFrom(categoryService.findOne(categoryId));
model.addAttribute("categoryId", categoryId);
model.addAttribute("categoryForm", categoryForm);
return "category-update";
}
@PostMapping("/category/update")
public String categoryUpdatePost(@ModelAttribute CategoryForm categoryForm, HttpSession session, Long categoryId) {
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
categoryForm.setCategoryId(categoryId);
categoryService.save(CategoryForm.convertTo(categoryForm), loginUser.getUserId());
return "redirect:/category/list";
}
@PostMapping("/category/delete")
public String categoryDeletePost(Long categoryId) {
categoryService.delete(categoryId);
return "redirect:/category/list";
}
}

View File

@@ -3,6 +3,7 @@ package com.example.demo.app.presentation.controller;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -185,8 +186,9 @@ public class TransactionController {
@GetMapping("/transaction/create")
public String createOneGet(@RequestParam(defaultValue = "") LocalDate today,
Long fixedCostId,
Model model) {
Model model, HttpSession session) {
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
if (today == null) {
today = LocalDate.now();
}
@@ -196,11 +198,21 @@ public class TransactionController {
FixedCostForm fixedCostForm = FixedCostForm.convertFrom(fixedCostService.findById(fixedCostId));
transactionForm.setCategoryId(fixedCostForm.getCategoryId());
transactionForm.setAmount(fixedCostForm.getAmount());
transactionForm.setTransactionType(CategoryForm.convertFrom(categoryService.findOne(fixedCostForm.getCategoryId())).getTransactionType());
transactionForm.setTransactionType(CategoryForm
.convertFrom(categoryService.findOne(fixedCostForm.getCategoryId())).getTransactionType());
}
List<CategoryForm> categoryFormList = CategoryForm.convertFrom(categoryService.findAll());
List<CategoryForm> categoryFormList = new ArrayList<>(
CategoryForm.convertFrom(categoryService.findAllExceptCustom()));
List<CategoryForm> categoryFormCustomList = CategoryForm
.convertFrom(categoryService.findAllByuserId(loginUser.getUserId()));
if (!categoryFormCustomList.isEmpty()) {
for (CategoryForm form : categoryFormCustomList) {
categoryFormList.add(form);
}
}
System.out.println(transactionForm.getCategoryName() + " " + transactionForm.getAmount());
@@ -232,13 +244,24 @@ public class TransactionController {
@SessionCheck
@GetMapping("/transaction/update")
public String updateOneGet(@RequestParam Long transactionId, @RequestParam(defaultValue = "") LocalDate today,
Model model) {
Model model, HttpSession session) {
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
if (today == null) {
today = LocalDate.now();
}
List<CategoryForm> categoryFormList = CategoryForm.convertFrom(categoryService.findAll());
List<CategoryForm> categoryFormList = new ArrayList<>(
CategoryForm.convertFrom(categoryService.findAllExceptCustom()));
List<CategoryForm> categoryFormCustomList = CategoryForm
.convertFrom(categoryService.findAllByuserId(loginUser.getUserId()));
if (!categoryFormCustomList.isEmpty()) {
for (CategoryForm form : categoryFormCustomList) {
categoryFormList.add(form);
}
}
TransactionForm transactionForm = TransactionForm.convertFrom(
transactionService.findByTransactionId(transactionId),

View File

@@ -25,4 +25,11 @@ public class CategoryForm {
return categoryModelList.stream().map(CategoryForm::convertFrom).toList();
}
public static CategoryModel convertTo(CategoryForm categoryForm) {
return new CategoryModel(
categoryForm.getCategoryId(),
categoryForm.getCategoryName(),
categoryForm.getTransactionType());
}
}

View File

@@ -0,0 +1,52 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" th:href="@{/css/menu.css}">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="pb-5 pt-5">
<div th:replace="~{header :: header}"></div>
<h1>カテゴリ登録</h1>
<form th:action="@{/category/create}" th:object="${categoryForm}" method="post">
<div>
<label for="categoryName">カテゴリ名</label>
<input type="text" id="categoryName" th:field="*{categoryName}">
</div>
<br>
<div>
<label for="transactionType">収入・支出</label>
<select id="transactionType" th:field="*{transactionType}">
<option value="">選択してください</option>
<option value="0">支出</option>
<option value="1">収入</option>
</select>
</div>
<br>
<button type="submit">登録</button>
</form>
<div th:replace="~{footer :: footer}"></div>
</body>
</html>

View File

@@ -0,0 +1,63 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>カテゴリ一覧</title>
<link rel="stylesheet" th:href="@{/css/transaction_detail.css}">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="pb-5 pt-5">
<div th:replace="~{header :: header}"></div>
<div>
<ul>
<li th:each="categoryForm : ${categoryFormList}" class="transaction-item">
<div>
<p>
カテゴリ名:
<span th:text="${categoryForm.categoryName}"></span>
<br>
収支区分:
<span th:if="${categoryForm.transactionType == 0}" th:text="|支出|"></span>
<span th:if="${categoryForm.transactionType == 1}" th:text="|収入|"></span>
</p>
</div>
<div class="transaction-buttons">
<form th:action="@{/category/delete}" method="post">
<input type="hidden" name="today" th:value="${today}">
<input type="hidden" name="categoryId" th:value="${categoryForm.categoryId}">
<button type="submit">削除</button>
</form>
<form th:action="@{/category/update}" method="get">
<input type="hidden" name="today" th:value="${today}">
<input type="hidden" name="categoryId" th:value="${categoryForm.categoryId}">
<button type="submit">編集</button>
</form>
</div>
<hr>
<hr>
</li>
</ul>
</div>
<form th:action="@{/category/create}" method="get">
<input type="hidden" name="today" id="today" th:value="${today}">
<button type="submit">新規登録</button>
</form>
<div th:replace="~{footer :: footer}"></div>
</body>
</html>

View File

@@ -0,0 +1,54 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" th:href="@{/css/menu.css}">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="pb-5 pt-5">
<div th:replace="~{header :: header}"></div>
<h1>カテゴリ編集</h1>
<form th:action="@{/category/update}" th:object="${categoryForm}" method="post">
<div>
<label for="categoryName">カテゴリ名</label>
<input type="text" id="categoryName" th:field="*{categoryName}">
</div>
<br>
<div>
<label for="transactionType">収入・支出</label>
<select id="transactionType" th:field="*{transactionType}">
<option value="">選択してください</option>
<option value="0">支出</option>
<option value="1">収入</option>
</select>
</div>
<br>
<input type="hidden" name="categoryId" th:value="${categoryId}">
<button type="submit">登録</button>
</form>
<div th:replace="~{footer :: footer}"></div>
</body>
</html>

View File

@@ -60,6 +60,10 @@
<input type="hidden" name="today" th:value="${today}">
<button type="submit">固定費一覧</button>
</form>
<form th:action="@{/category/list}" method="get">
<input type="hidden" name="today" th:value="${today}">
<button type="submit">カテゴリ一覧</button>
</form>
<div th:replace="~{footer :: footer}"></div>
</body>