-
쇼핑몰프로젝트 --상품, 카테고리 등록, 조회 (api기반)쇼핑몰 프로젝트 2024. 6. 15. 13:01
아이템
itemRepository
@Repository @RequiredArgsConstructor public class ItemRepository { private final EntityManager em; //상품저장 public void saveItem(Item item) { em.persist(item); } //상품 하나 조회 public Item findOne(Long ItemId) { return em.find(Item.class, ItemId); }
itemservice
@Service @Transactional(readOnly = true) @RequiredArgsConstructor public class ItemService { private final ItemRepository itemRepository; private final CategoryRepository categoryRepository; //상품등록 @Transactional public Item saveItem(ItemRequestDto itemRequestDto) { Category category = categoryRepository.findOneCategory(itemRequestDto.getCategoryId()); Item item = new Item( category, itemRequestDto.getItemName(), itemRequestDto.getItemContent(), itemRequestDto.getItemPrice(), itemRequestDto.getItemStock()); itemRepository.saveItem(item); return item; } //단건상품 조회 public Item findOneItem(Long ItemId) { return itemRepository.findOne(ItemId); }
itemRequestDto
package ypjs.project.dto.itemdto; import lombok.Getter; import java.util.List; @Getter public class ItemRequestDto { private Long categoryId; private String itemName; private String itemContent; private int itemPrice; private int itemStock; public ItemRequestDto() {} public ItemRequestDto(Long categoryId, String itemName, String itemContent, int itemPrice, int itemStock) { this.categoryId = categoryId; this.itemName = itemName; this.itemContent = itemContent; this.itemPrice = itemPrice; this.itemStock = itemStock; } }
itemResponseDto
package ypjs.project.dto.itemdto; import lombok.Getter; import ypjs.project.domain.Item; import java.util.List; @Getter public class ItemResponseDto { private Long categoryId; private Long itemId; private String itemName; private String itemContent; private int itemPrice; private int itemStock; public ItemResponseDto() {} public ItemResponseDto(Long categoryId, Long itemId, String itemName, String itemContent, int itemPrice, int itemStock ) { this.categoryId = categoryId; this.itemId = itemId; this.itemName = itemName; this.itemContent = itemContent; this.itemPrice = itemPrice; this.itemStock = itemStock; } }
itemController
@RestController @RequiredArgsConstructor public class ItemController { private final ItemService itemService; private final ItemReviewService itemReviewService; private final CategoryService categoryService; //item등록 @PostMapping("/ypjs/item/post") public ItemResponseDto saveItem(@RequestBody @Valid ItemRequestDto requestDto) { Item item= itemService.saveItem(requestDto); return new ItemResponseDto(item.getCategory().getCategoryId(), item.getItemId(), item.getItemName(), item.getItemContent(), item.getItemPrice(), item.getItemStock()); } //item1개 조회 @GetMapping("/ypjs/item/get/{itemId}") public ItemOneDto getOneItem (@PathVariable("itemId") Long itemId) { Item item = itemService.findOneItem(itemId); //조회수 itemService.increaseItemCnt(itemId); //리뷰리스트 itemReviewService.findAllItemReview(itemId); return new ItemOneDto(item); }
조회수
itemRepository
//조회수 @Transactional public void increaseCnt(Long itemId) { em.createQuery("update Item i set i.itemCnt = i.itemCnt + 1 where i.itemId = :itemId") .setParameter("itemId", itemId) .executeUpdate(); }
itemService
//조회수 @Transactional public void increaseItemCnt(Long itemId) { itemRepository.increaseCnt(itemId); }
카테고리
categoryRepository
@Repository @RequiredArgsConstructor public class CategoryRepository { private final EntityManager em; //카테고리 저장 public void saveCategory(Category category) { em.persist(category); } //categoryId 단건조회 // public Category findOneCategory(Long categoryId) { // return em.find(Category.class, categoryId); // } //categoryId 단건조회 public Category findOneCategory(Long categoryId) { if (categoryId == null) { throw new IllegalArgumentException("Category ID must not be null"); } Category category = em.find(Category.class, categoryId); if (category == null) { throw new IllegalArgumentException("Category not found for ID: " + categoryId); } return category; } //CategoryParentId통해서 조회 public List<Category> findByParentId(Category categoryParent) { return em.createQuery("select c from Category c where c.categoryParent = :categoryParent", Category.class) .setParameter("categoryParent", categoryParent) .getResultList(); } //category전체 조회 // public List<Category> findAll() { // return em.createQuery("select c from Category c", Category.class) // .getResultList(); // } //패치조인 (category전체 조회) public List<Category> findAllWithItem() { return em.createQuery( "select distinct c from Category c" + " join fetch c.items i" + " join fetch c.categoryParent cp", Category.class) .getResultList(); }
categoryService
@Service @Transactional(readOnly = true) @RequiredArgsConstructor public class CategoryService { private final CategoryRepository categoryRepository; //category등록 // @Transactional // public void saveCategory(Category category) { // categoryRepository.saveCategory(category); // } //category등록 @Transactional public Category saveCategory(CategoryRequestDto categoryRequestDto) { Category parentCategory = categoryRepository.findOneCategory(categoryRequestDto.getCategoryParent()); Category category = new Category( parentCategory, categoryRequestDto.getCategoryName() ); categoryRepository.saveCategory(category); return category; } //categoryId단건조회 public Category findOneCategory(Long categoryId) { return categoryRepository.findOneCategory(categoryId); } //categoryParentId조회 public List<Category> findCategoryParent(Category categoryParent) { return categoryRepository.findByParentId(categoryParent); } //category전체 조회 public List<CategoryListDto> findAllCategory(CategoryListDto categoryListDto) { // List<Category> categories = categoryRepository.findAll(); List<Category> categories = categoryRepository.findAllWithItem(); List<CategoryListDto> result = categories.stream() .map(c -> new CategoryListDto(c)) .collect(Collectors.toList()); return result; }
categoryRequestDto
package ypjs.project.dto.categorydto; import jakarta.validation.constraints.NotNull; import lombok.Getter; import ypjs.project.domain.Category; @Getter public class CategoryRequestDto { @NotNull private Long categoryParent; private String categoryName; public CategoryRequestDto() {} public CategoryRequestDto(Long categoryParent, String categoryName) { this.categoryParent = categoryParent; this.categoryName = categoryName; } }
categoryResponseDto
package ypjs.project.dto.categorydto; import lombok.Getter; import ypjs.project.domain.Category; @Getter public class CategoryRespnseDto { private Long categoryId; private Category categoryParent; private String categoryName; public CategoryRespnseDto() {} public CategoryRespnseDto(Long categoryId, Category categoryParent, String categoryName) { this.categoryId = categoryId; this.categoryParent = categoryParent; this.categoryName = categoryName; } }
categoryListDto
package ypjs.project.dto.categorydto; import lombok.Getter; import ypjs.project.domain.Category; import ypjs.project.domain.Item; import java.util.List; import java.util.stream.Collectors; @Getter public class CategoryListDto { private Long categoryId; private Long categoryParent; private String categoryName; private List<ItemDto> items; public CategoryListDto() {} public CategoryListDto(Category category) { categoryId = category.getCategoryId(); // categoryParent가 null이 아닐 때만 값을 설정 if (category.getCategoryParent() != null) { categoryParent = category.getCategoryParent().getCategoryId(); } else { categoryParent = null; } categoryName = category.getCategoryName(); items = category.getItems().stream() .map(item -> new ItemDto(item)) .collect(Collectors.toList()); } @Getter static class ItemDto{ private String itemName; private String itemContent; private int itemPrice; private int itemStoock; public ItemDto(Item item) { itemName = item.getItemName(); itemContent = item.getItemContent(); itemPrice = item.getItemPrice(); itemStoock = item.getItemStock(); } } }
categoryController
@RestController @RequiredArgsConstructor public class CategoryController { private final CategoryService categoryService; private final ItemService itemService; //category등록 @PostMapping("/ypjs/category/post") public CategoryRespnseDto saveCategory(@RequestBody @Valid CategoryRequestDto categoryRequestDtorequest){ Category category = categoryService.saveCategory(categoryRequestDtorequest); return new CategoryRespnseDto(category.getCategoryId(), category.getCategoryParent(), category.getCategoryName()); } //categoryList보기 @GetMapping("/ypjs/category/get") public List<CategoryListDto> getCategoryList(CategoryListDto categoryListDto){ return categoryService.findAllCategory(categoryListDto); }
'쇼핑몰 프로젝트' 카테고리의 다른 글
쇼핑몰 프로젝트 --아이템 등록, 수정 (화면구현) (0) 2024.06.17 쇼핑몰 프로젝트 --아이템 리뷰 등록, 조회, 수정, 삭제(api) (0) 2024.06.15 쇼핑몰프로젝트 --카테고리 당 아이템 조회(api기반) (0) 2024.06.15 쇼핑몰프로젝트 --아이템, 카테고리 수정, 삭제 (api기반) (0) 2024.06.15 쇼핑몰프로젝트 --엔티티 만들기 (api기반) (0) 2024.06.15