Skip to content

Commit

Permalink
#4 Add memberCartProductCountUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
pursue503 committed Jul 23, 2020
1 parent a689c77 commit c5ffbb2
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.memoryboost.controller.cart;

import com.memoryboost.domain.vo.cart.request.CartProductCountUpdateRequestVO;
import com.memoryboost.service.cart.CartService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;

@Slf4j
@RequiredArgsConstructor
Expand All @@ -22,4 +25,16 @@ public String memberCartList(Authentication authentication , Model model){
return "cart 페이지";
}

@PutMapping("/cart")
public String memberCartProductCountUpdate(CartProductCountUpdateRequestVO updateRequestVO) {
try{
cartService.cartCountUpdate(updateRequestVO);
return "redirect:/cart";
} catch (NullPointerException e) {
return "error";
}
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@NoArgsConstructor
@ToString
public class CartProductCountUpdateRequestVO {

private Long cartNo;
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/memoryboost/service/cart/CartService.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ public List<MemberCartResponseVO> memberCart(Authentication authentication) {
return cartRepository.findByMemberCart(member);
}


@Transactional
public void cartCountUpdate(CartProductCountUpdateRequestVO updateRequestVO) {
Cart cart = cartRepository.findById(updateRequestVO.getCartNo()).orElseThrow(NullPointerException::new);
cart.cartProductCountUpdate(updateRequestVO);
}


}
84 changes: 84 additions & 0 deletions src/test/java/com/memoryboost/service/CartServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.memoryboost.service;

import com.memoryboost.domain.entity.cart.Cart;
import com.memoryboost.domain.entity.cart.CartRepository;
import com.memoryboost.domain.entity.member.Member;
import com.memoryboost.domain.entity.member.MemberRepository;
import com.memoryboost.domain.entity.member.Role;
import com.memoryboost.domain.entity.product.Product;
import com.memoryboost.domain.entity.product.ProductRepository;
import com.memoryboost.domain.vo.cart.request.CartProductCountUpdateRequestVO;
import com.memoryboost.service.cart.CartService;
import lombok.extern.slf4j.Slf4j;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class CartServiceTest {

@Autowired
private CartService cartService;

@Autowired
private CartRepository cartRepository;

@Autowired
private ProductRepository productRepository;

@Autowired
private MemberRepository memberRepository;

@Before
public void before(){
Product product = productRepository.save(Product.builder().productName("갤럭시 GALAX 지포스 RTX 2070 SUPER EX OC D6 8GB PINK Edition").
productCategory(1)
.productThumbnail("썸네일경로")
.productDescription("설명")
.productPrice(600000)
.build());

Member member = memberRepository.save(Member.builder().memberLoginId("abc1234").memberPw("aaa1111").
memberName("홍길동").
memberEmail("abc1234@naver.com").
memberSns("memoryboost").
memberZipCode("08080").
memberAddress("서울특별시").
memberDetailAddress("종로구댕댕댕").
memberTel("010-0000-0000").
memberAuth(Role.USER).build());
cartRepository.save(Cart.builder().product(product).member(member).productCnt(5).build());
}

@After
public void after(){
cartRepository.deleteAll();
memberRepository.deleteAll();
productRepository.deleteAll();
}

@Test
public void memberCartProductCountUpdateTests(){

int count = 10;

CartProductCountUpdateRequestVO cartProductCountUpdateRequestVO = new CartProductCountUpdateRequestVO();
cartProductCountUpdateRequestVO.setCartNo(1L);
cartProductCountUpdateRequestVO.setProductCnt(count);

cartService.cartCountUpdate(cartProductCountUpdateRequestVO);

Cart cart = cartRepository.findById(1L).orElseThrow(NullPointerException::new);

assertThat(count).isEqualTo(cart.getProductCnt());
}

}

0 comments on commit c5ffbb2

Please sign in to comment.