[EDIT] CategoryのCRUD機能を追加しました。
This commit is contained in:
@@ -13,4 +13,12 @@ public interface CategoryService {
|
|||||||
public CategoryModel findOneByCategoryName(String categoryName);
|
public CategoryModel findOneByCategoryName(String categoryName);
|
||||||
|
|
||||||
public CategoryModel findByTransactionId(Long transactionId);
|
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();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.domain.service.CategoryService;
|
||||||
import com.example.demo.app.infra.entity.CategoryEntity;
|
import com.example.demo.app.infra.entity.CategoryEntity;
|
||||||
import com.example.demo.app.infra.repository.CategoryRepository;
|
import com.example.demo.app.infra.repository.CategoryRepository;
|
||||||
|
import com.example.demo.app.infra.repository.UserRepository;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
@@ -17,6 +18,7 @@ import lombok.RequiredArgsConstructor;
|
|||||||
public class CategoryServiceImpl implements CategoryService {
|
public class CategoryServiceImpl implements CategoryService {
|
||||||
|
|
||||||
private final CategoryRepository categoryRepository;
|
private final CategoryRepository categoryRepository;
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
public List<CategoryModel> findAll() {
|
public List<CategoryModel> findAll() {
|
||||||
|
|
||||||
@@ -51,6 +53,30 @@ public class CategoryServiceImpl implements CategoryService {
|
|||||||
public CategoryModel findByTransactionId(Long transactionId) {
|
public CategoryModel findByTransactionId(Long transactionId) {
|
||||||
return CategoryModel.convertFrom(categoryRepository.findByTransactionId(transactionId));
|
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());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.example.demo.app.infra.repository;
|
package com.example.demo.app.infra.repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.data.repository.query.Param;
|
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")
|
@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);
|
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();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user