[Spring][쇼핑몰 프로젝트][36] 장바구니 기능(장바구니 추가(서버 구현)) - 4

https://kimvampa.tistory.com/263
This commit is contained in:
SeoJin Kim
2021-11-18 04:06:41 +09:00
parent 5181ff9dec
commit 7350d40e1e
12 changed files with 263 additions and 2 deletions

View File

@@ -0,0 +1,47 @@
package com.vam.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.vam.model.CartDTO;
import com.vam.model.MemberVO;
import com.vam.service.CartService;
@Controller
public class CartController {
@Autowired
private CartService cartService;
/* 장바구니 추가 */
/**
* 0: 등록 실패
* 1: 등록 성공
* 2: 등록된 데이터 존재
* 5: 로그인 필요
*
*/
@PostMapping("/cart/add")
@ResponseBody
public String addCartPOST(CartDTO cart, HttpServletRequest request) {
// 로그인 체크
HttpSession session = request.getSession();
MemberVO mvo = (MemberVO)session.getAttribute("member");
if(mvo == null) {
return "5";
}
// 카트 등록
int result = cartService.addCart(cart);
return result + "";
}
}

View File

@@ -7,7 +7,7 @@ import com.vam.model.CartDTO;
public interface CartMapper {
/* 카트 추가 */
public int addCart(CartDTO cart);
public int addCart(CartDTO cart) throws Exception;
/* 카트 삭제 */
public int deleteCart(int cartId);

View File

@@ -0,0 +1,10 @@
package com.vam.service;
import com.vam.model.CartDTO;
public interface CartService {
/* 장바구니 추가 */
public int addCart(CartDTO cart);
}

View File

@@ -0,0 +1,33 @@
package com.vam.service;
import org.springframework.beans.factory.annotation.Autowired;
import com.vam.mapper.CartMapper;
import com.vam.model.CartDTO;
public class CartServiceImpl implements CartService {
@Autowired
private CartMapper cartMapper;
@Override
public int addCart(CartDTO cart) {
// 장바구니 데이터 체크
CartDTO checkCart = cartMapper.checkCart(cart);
if(checkCart != null) {
return 2;
}
// 장바구니 등록 & 에러 시 0반환
try {
return cartMapper.addCart(cart);
} catch (Exception e) {
return 0;
}
}
}

View File

@@ -35,6 +35,7 @@
</interceptor>
<interceptor>
<mapping path="/cart/**"/>
<exclude-mapping path="/cart/add"/>
<beans:bean id="CartIntreceptor" class="com.vam.interceptor.CartInterceptor"></beans:bean>
</interceptor>
</interceptors>

View File

@@ -0,0 +1,40 @@
package com.vam.service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.vam.model.CartDTO;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
public class CartServiceTests {
@Autowired
private CartService service;
//등록 테스트
@Test
public void addCartTest() {
//given
String memberId = "admin";
int bookId = 22;
int count = 5;
CartDTO dto = new CartDTO();
dto.setMemberId(memberId);
dto.setBookId(bookId);
dto.setBookCount(count);
//when
int result = service.addCart(dto);
//then
System.out.println("** result : " + result);
}
}

View File

@@ -0,0 +1,47 @@
package com.vam.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.vam.model.CartDTO;
import com.vam.model.MemberVO;
import com.vam.service.CartService;
@Controller
public class CartController {
@Autowired
private CartService cartService;
/* 장바구니 추가 */
/**
* 0: 등록 실패
* 1: 등록 성공
* 2: 등록된 데이터 존재
* 5: 로그인 필요
*
*/
@PostMapping("/cart/add")
@ResponseBody
public String addCartPOST(CartDTO cart, HttpServletRequest request) {
// 로그인 체크
HttpSession session = request.getSession();
MemberVO mvo = (MemberVO)session.getAttribute("member");
if(mvo == null) {
return "5";
}
// 카트 등록
int result = cartService.addCart(cart);
return result + "";
}
}

View File

@@ -7,7 +7,7 @@ import com.vam.model.CartDTO;
public interface CartMapper {
/* 카트 추가 */
public int addCart(CartDTO cart);
public int addCart(CartDTO cart) throws Exception;
/* 카트 삭제 */
public int deleteCart(int cartId);

View File

@@ -0,0 +1,10 @@
package com.vam.service;
import com.vam.model.CartDTO;
public interface CartService {
/* 장바구니 추가 */
public int addCart(CartDTO cart);
}

View File

@@ -0,0 +1,32 @@
package com.vam.service;
import org.springframework.beans.factory.annotation.Autowired;
import com.vam.mapper.CartMapper;
import com.vam.model.CartDTO;
public class CartServiceImpl implements CartService {
@Autowired
private CartMapper cartMapper;
@Override
public int addCart(CartDTO cart) {
// 장바구니 데이터 체크
CartDTO checkCart = cartMapper.checkCart(cart);
if(checkCart != null) {
return 2;
}
// 장바구니 등록 & 에러 시 0반환
try {
return cartMapper.addCart(cart);
} catch (Exception e) {
return 0;
}
}
}

View File

@@ -35,6 +35,7 @@
</interceptor>
<interceptor>
<mapping path="/cart/**"/>
<exclude-mapping path="/cart/add"/>
<beans:bean id="CartIntreceptor" class="com.vam.interceptor.CartInterceptor"></beans:bean>
</interceptor>
</interceptors>

View File

@@ -0,0 +1,40 @@
package com.vam.service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.vam.model.CartDTO;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
public class CartServiceTests {
@Autowired
private CartService service;
//등록 테스트
@Test
public void addCartTest() {
//given
String memberId = "admin";
int bookId = 22;
int count = 5;
CartDTO dto = new CartDTO();
dto.setMemberId(memberId);
dto.setBookId(bookId);
dto.setBookCount(count);
//when
int result = service.addCart(dto);
//then
System.out.println("** result : " + result);
}
}