ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 쇼핑몰프로젝트 --아이템, 카테고리 수정, 삭제 (api기반)
    쇼핑몰 프로젝트 2024. 6. 15. 13:30

    아이템 수정,삭제

    item클래스 (변경감지 메서드 추가)

    //상품변경메서드
    public Long changeItem(Category category, String itemName, String itemContent, int itemPrice, int itemStock) {
        this.category = category;
        this.itemName = itemName;
        this.itemContent = itemContent;
        this.itemPrice = itemPrice;
        this.itemStock = itemStock;
    
        return this.itemId;
    }
    

     

     

    itemRepository

    //삭제
    @Transactional
    public void deleteItem(Long itemId) {
        itemRepository.deleteItem(itemId);
    }

     

     

    itemService(수정에 변경감지 메서드 추가)

    //수정
    @Transactional
    public void updateItem(Long itemId, ItemUpdateDto itemUpdateDto) {
        Item item = itemRepository.findOne(itemId);
    
        Category category = categoryRepository.findOneCategory(itemUpdateDto.getCategoryId());
    
        item.changeItem(category,
                itemUpdateDto.getItemName(),
                itemUpdateDto.getItemContent(),
                itemUpdateDto.getItemPrice(),
                item.getItemStock());
    }
    
    
    //삭제
        @Transactional
        public void deleteItem(Long itemId) {
            itemRepository.deleteItem(itemId);
        }

     

     

    itemUpdateDto

    package ypjs.project.dto.itemdto;
    
    import lombok.Getter;
    
    @Getter
    public class ItemUpdateDto {
        private Long categoryId;
        private String itemName;
        private String itemContent;
        private int itemPrice;
        private int itemStock;
    
        public ItemUpdateDto(){}
    
        public ItemUpdateDto(Long categoryId, Long itemId, String itemName, String itemContent, int itemPrice, int itemStock) {
            this.categoryId = categoryId;
            this.itemName = itemName;
            this.itemContent = itemContent;
            this.itemPrice = itemPrice;
            this.itemStock = itemStock;
        }
    }
    

     

     

    itemController

    //수정
    @PutMapping("/ypjs/item/update/{itemId}")
    public void updateItem(@PathVariable("itemId") Long itemId,
                                      @RequestBody @Valid ItemUpdateDto itemUpdateDto) {
    
        itemService.updateItem(itemId,itemUpdateDto);
        Item findItem = itemService.findOneItem(itemId);
    
        //화면구현할 때 void string같은 타입으로 바꾸고 return 화면구현할 주소로 지금은 return 없어서 포스트맨 안됨 걀가값 보고 싶으면 public 옆에 ItemUpdateDto로 타입 바꾸고
        //return new ItemUpdateDto(밑에 값 써주기)
        //ItemUpdateDto response = new ItemUpdateDto(findItem.getItemId(), findItem.getCategory().getCategoryId(), findItem.getItemName(), findItem.getItemContent(), findItem.getItemPrice(),findItem.getItemStock());
    
    
    
    }
    
    
      //삭제
        @DeleteMapping("/ypjs/item/delete/{itemId}")
        public void deleteItem(@PathVariable("itemId") Long itemId){
            itemService.deleteItem(itemId);
        }

     

     

     

    카테고리 수정, 삭제

     

    category(변경감지 메서드 추가)

    //카테고리 변경 메서드
    public Long changeCategory (Category categoryParent, String categoryName) {
        this.categoryParent = categoryParent;
        this.categoryName = categoryName;
    
        return this.categoryId;
    }

     

     

    categoryReposirtory

    //category삭제
    public void deleteCategory(Long categoryId) {
        Category findCategory = em.find(Category.class, categoryId);
        em.remove(findCategory);
    }

     

    categoryService(수정에 변경감지 메서드 추가)

    //category수정
    @Transactional
    public void updateCategory(Long categoryId, CategoryUpdateDto categoryUpdateDto) {
        Category category = categoryRepository.findOneCategory(categoryId);
    
        Category parentCategory = categoryRepository.findOneCategory(categoryUpdateDto.getCategoryParent());
    
        category.changeCategory(
                parentCategory,
                categoryUpdateDto.getCategoryName()
        );
    }
    
    
    //categtory삭제
    @Transactional
    public void deleteCategory(Long categoryId) {
        categoryRepository.deleteCategory(categoryId);
    }
    
    

     

     

    categoryUpdateDto

    package ypjs.project.dto.categorydto;
    
    import lombok.Getter;
    import ypjs.project.domain.Category;
    
    @Getter
    public class CategoryUpdateDto {
    
        //private Long categoryId;
        private Long categoryParent;
        private String categoryName;
    
        public CategoryUpdateDto() {}
    
        public CategoryUpdateDto(Long categoryId, Long categoryParent, String categoryName) {
            //this.categoryId = categoryId;
            this.categoryParent = categoryParent;
            this.categoryName = categoryName;
        }
    }
    

     

     

    categoryController

     //category수정
    @PutMapping("ypjs/category/update/{categoryId}")
     public CategoryUpdateDto updateCategory(@PathVariable("categoryId") Long categoryId,
                                             @RequestBody @Valid CategoryUpdateDto categoryUpdateDto) {
    
         categoryService.updateCategory(categoryId, categoryUpdateDto);
         Category findCategory = categoryService.findOneCategory(categoryId);
    
    
        return new CategoryUpdateDto(findCategory.getCategoryId(), findCategory.getCategoryParent().getCategoryId(), findCategory.getCategoryName());
    }
    
    
    //category삭제
     @DeleteMapping("ypjs/category/delete/{categoryId}")
     public void deleteCategory(@PathVariable("categoryId") Long categoryId) {
         categoryService.deleteCategory(categoryId);
     }
    

     

Designed by Tistory.