[Spring][쇼핑몰 프로젝트][36] 장바구니 기능(장바구니 추가(서버 구현)) - 4
https://kimvampa.tistory.com/263
This commit is contained in:
47
VamPa/src/main/java/com/vam/controller/CartController.java
Normal file
47
VamPa/src/main/java/com/vam/controller/CartController.java
Normal 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 + "";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ import com.vam.model.CartDTO;
|
|||||||
public interface CartMapper {
|
public interface CartMapper {
|
||||||
|
|
||||||
/* 카트 추가 */
|
/* 카트 추가 */
|
||||||
public int addCart(CartDTO cart);
|
public int addCart(CartDTO cart) throws Exception;
|
||||||
|
|
||||||
/* 카트 삭제 */
|
/* 카트 삭제 */
|
||||||
public int deleteCart(int cartId);
|
public int deleteCart(int cartId);
|
||||||
|
|||||||
10
VamPa/src/main/java/com/vam/service/CartService.java
Normal file
10
VamPa/src/main/java/com/vam/service/CartService.java
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package com.vam.service;
|
||||||
|
|
||||||
|
import com.vam.model.CartDTO;
|
||||||
|
|
||||||
|
public interface CartService {
|
||||||
|
|
||||||
|
/* 장바구니 추가 */
|
||||||
|
public int addCart(CartDTO cart);
|
||||||
|
|
||||||
|
}
|
||||||
33
VamPa/src/main/java/com/vam/service/CartServiceImpl.java
Normal file
33
VamPa/src/main/java/com/vam/service/CartServiceImpl.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -35,6 +35,7 @@
|
|||||||
</interceptor>
|
</interceptor>
|
||||||
<interceptor>
|
<interceptor>
|
||||||
<mapping path="/cart/**"/>
|
<mapping path="/cart/**"/>
|
||||||
|
<exclude-mapping path="/cart/add"/>
|
||||||
<beans:bean id="CartIntreceptor" class="com.vam.interceptor.CartInterceptor"></beans:bean>
|
<beans:bean id="CartIntreceptor" class="com.vam.interceptor.CartInterceptor"></beans:bean>
|
||||||
</interceptor>
|
</interceptor>
|
||||||
</interceptors>
|
</interceptors>
|
||||||
|
|||||||
40
VamPa/src/test/java/com/vam/service/CartServiceTests.java
Normal file
40
VamPa/src/test/java/com/vam/service/CartServiceTests.java
Normal 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);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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 + "";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ import com.vam.model.CartDTO;
|
|||||||
public interface CartMapper {
|
public interface CartMapper {
|
||||||
|
|
||||||
/* 카트 추가 */
|
/* 카트 추가 */
|
||||||
public int addCart(CartDTO cart);
|
public int addCart(CartDTO cart) throws Exception;
|
||||||
|
|
||||||
/* 카트 삭제 */
|
/* 카트 삭제 */
|
||||||
public int deleteCart(int cartId);
|
public int deleteCart(int cartId);
|
||||||
|
|||||||
10
VamPa_MySQL/src/main/java/com/vam/service/CartService.java
Normal file
10
VamPa_MySQL/src/main/java/com/vam/service/CartService.java
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package com.vam.service;
|
||||||
|
|
||||||
|
import com.vam.model.CartDTO;
|
||||||
|
|
||||||
|
public interface CartService {
|
||||||
|
|
||||||
|
/* 장바구니 추가 */
|
||||||
|
public int addCart(CartDTO cart);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -35,6 +35,7 @@
|
|||||||
</interceptor>
|
</interceptor>
|
||||||
<interceptor>
|
<interceptor>
|
||||||
<mapping path="/cart/**"/>
|
<mapping path="/cart/**"/>
|
||||||
|
<exclude-mapping path="/cart/add"/>
|
||||||
<beans:bean id="CartIntreceptor" class="com.vam.interceptor.CartInterceptor"></beans:bean>
|
<beans:bean id="CartIntreceptor" class="com.vam.interceptor.CartInterceptor"></beans:bean>
|
||||||
</interceptor>
|
</interceptor>
|
||||||
</interceptors>
|
</interceptors>
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user