From 7350d40e1e0b377936ede95ad81c5ac7d662293f Mon Sep 17 00:00:00 2001 From: SeoJin Kim Date: Thu, 18 Nov 2021 04:06:41 +0900 Subject: [PATCH] =?UTF-8?q?[Spring][=EC=87=BC=ED=95=91=EB=AA=B0=20?= =?UTF-8?q?=ED=94=84=EB=A1=9C=EC=A0=9D=ED=8A=B8][36]=20=EC=9E=A5=EB=B0=94?= =?UTF-8?q?=EA=B5=AC=EB=8B=88=20=EA=B8=B0=EB=8A=A5(=EC=9E=A5=EB=B0=94?= =?UTF-8?q?=EA=B5=AC=EB=8B=88=20=EC=B6=94=EA=B0=80(=EC=84=9C=EB=B2=84=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84))=20-=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://kimvampa.tistory.com/263 --- .../com/vam/controller/CartController.java | 47 +++++++++++++++++++ .../main/java/com/vam/mapper/CartMapper.java | 2 +- .../java/com/vam/service/CartService.java | 10 ++++ .../java/com/vam/service/CartServiceImpl.java | 33 +++++++++++++ .../spring/appServlet/servlet-context.xml | 1 + .../com/vam/service/CartServiceTests.java | 40 ++++++++++++++++ .../com/vam/controller/CartController.java | 47 +++++++++++++++++++ .../main/java/com/vam/mapper/CartMapper.java | 2 +- .../java/com/vam/service/CartService.java | 10 ++++ .../java/com/vam/service/CartServiceImpl.java | 32 +++++++++++++ .../spring/appServlet/servlet-context.xml | 1 + .../com/vam/service/CartServiceTests.java | 40 ++++++++++++++++ 12 files changed, 263 insertions(+), 2 deletions(-) create mode 100644 VamPa/src/main/java/com/vam/controller/CartController.java create mode 100644 VamPa/src/main/java/com/vam/service/CartService.java create mode 100644 VamPa/src/main/java/com/vam/service/CartServiceImpl.java create mode 100644 VamPa/src/test/java/com/vam/service/CartServiceTests.java create mode 100644 VamPa_MySQL/src/main/java/com/vam/controller/CartController.java create mode 100644 VamPa_MySQL/src/main/java/com/vam/service/CartService.java create mode 100644 VamPa_MySQL/src/main/java/com/vam/service/CartServiceImpl.java create mode 100644 VamPa_MySQL/src/test/java/com/vam/service/CartServiceTests.java diff --git a/VamPa/src/main/java/com/vam/controller/CartController.java b/VamPa/src/main/java/com/vam/controller/CartController.java new file mode 100644 index 0000000..592cc18 --- /dev/null +++ b/VamPa/src/main/java/com/vam/controller/CartController.java @@ -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 + ""; + } + +} diff --git a/VamPa/src/main/java/com/vam/mapper/CartMapper.java b/VamPa/src/main/java/com/vam/mapper/CartMapper.java index 0f45c77..531f3b5 100644 --- a/VamPa/src/main/java/com/vam/mapper/CartMapper.java +++ b/VamPa/src/main/java/com/vam/mapper/CartMapper.java @@ -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); diff --git a/VamPa/src/main/java/com/vam/service/CartService.java b/VamPa/src/main/java/com/vam/service/CartService.java new file mode 100644 index 0000000..7a685a1 --- /dev/null +++ b/VamPa/src/main/java/com/vam/service/CartService.java @@ -0,0 +1,10 @@ +package com.vam.service; + +import com.vam.model.CartDTO; + +public interface CartService { + + /* 장바구니 추가 */ + public int addCart(CartDTO cart); + +} diff --git a/VamPa/src/main/java/com/vam/service/CartServiceImpl.java b/VamPa/src/main/java/com/vam/service/CartServiceImpl.java new file mode 100644 index 0000000..8fe0bb7 --- /dev/null +++ b/VamPa/src/main/java/com/vam/service/CartServiceImpl.java @@ -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; + } + + } + + +} diff --git a/VamPa/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml b/VamPa/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml index 65baef4..d004d80 100644 --- a/VamPa/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml +++ b/VamPa/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml @@ -35,6 +35,7 @@ + diff --git a/VamPa/src/test/java/com/vam/service/CartServiceTests.java b/VamPa/src/test/java/com/vam/service/CartServiceTests.java new file mode 100644 index 0000000..35258bf --- /dev/null +++ b/VamPa/src/test/java/com/vam/service/CartServiceTests.java @@ -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); + + + } + +} diff --git a/VamPa_MySQL/src/main/java/com/vam/controller/CartController.java b/VamPa_MySQL/src/main/java/com/vam/controller/CartController.java new file mode 100644 index 0000000..a549dc8 --- /dev/null +++ b/VamPa_MySQL/src/main/java/com/vam/controller/CartController.java @@ -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 + ""; + } + +} diff --git a/VamPa_MySQL/src/main/java/com/vam/mapper/CartMapper.java b/VamPa_MySQL/src/main/java/com/vam/mapper/CartMapper.java index 714891c..7d393b8 100644 --- a/VamPa_MySQL/src/main/java/com/vam/mapper/CartMapper.java +++ b/VamPa_MySQL/src/main/java/com/vam/mapper/CartMapper.java @@ -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); diff --git a/VamPa_MySQL/src/main/java/com/vam/service/CartService.java b/VamPa_MySQL/src/main/java/com/vam/service/CartService.java new file mode 100644 index 0000000..688d9ce --- /dev/null +++ b/VamPa_MySQL/src/main/java/com/vam/service/CartService.java @@ -0,0 +1,10 @@ +package com.vam.service; + +import com.vam.model.CartDTO; + +public interface CartService { + + /* 장바구니 추가 */ + public int addCart(CartDTO cart); + +} diff --git a/VamPa_MySQL/src/main/java/com/vam/service/CartServiceImpl.java b/VamPa_MySQL/src/main/java/com/vam/service/CartServiceImpl.java new file mode 100644 index 0000000..d4803d6 --- /dev/null +++ b/VamPa_MySQL/src/main/java/com/vam/service/CartServiceImpl.java @@ -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; + } + + } + +} diff --git a/VamPa_MySQL/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml b/VamPa_MySQL/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml index bc5f885..e4f7de7 100644 --- a/VamPa_MySQL/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml +++ b/VamPa_MySQL/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml @@ -35,6 +35,7 @@ + diff --git a/VamPa_MySQL/src/test/java/com/vam/service/CartServiceTests.java b/VamPa_MySQL/src/test/java/com/vam/service/CartServiceTests.java new file mode 100644 index 0000000..4ef3761 --- /dev/null +++ b/VamPa_MySQL/src/test/java/com/vam/service/CartServiceTests.java @@ -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); + + + } + +}