[Spring][쇼핑몰 프로젝트][37] 장바구니 기능(장바구니 페이지) -1

https://kimvampa.tistory.com/265
This commit is contained in:
SeoJin Kim
2021-11-24 02:15:48 +09:00
parent 467ae88379
commit 96949a54d5
18 changed files with 148 additions and 18 deletions

View File

@@ -5,6 +5,9 @@ import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
@@ -44,4 +47,13 @@ public class CartController {
return result + ""; return result + "";
} }
/* 장바구니 페이지 이동 */
@GetMapping("/cart/{memberId}")
public String cartPageGET(@PathVariable("memberId") String memberId, Model model) {
model.addAttribute("cartInfo", cartService.getCartList(memberId));
return "/cart";
}
} }

View File

@@ -22,6 +22,10 @@ public class CartDTO {
private int salePrice; private int salePrice;
private int totalPrice; private int totalPrice;
private int point;
private int totalPoint;
public int getCartId() { public int getCartId() {
return cartId; return cartId;
@@ -85,19 +89,32 @@ public class CartDTO {
public int getTotalPrice() { public int getTotalPrice() {
return totalPrice; return totalPrice;
} }
public int getPoint() {
return point;
}
public int getTotalPoint() {
return totalPoint;
}
public void initSaleTotal() { public void initSaleTotal() {
this.salePrice = (int) (this.bookPrice * (1-this.bookDiscount)); this.salePrice = (int) (this.bookPrice * (1-this.bookDiscount));
this.totalPrice = this.salePrice*this.bookCount; this.totalPrice = this.salePrice*this.bookCount;
} this.point = (int)(Math.floor(this.salePrice*0.05));
this.totalPoint =this.point * this.bookCount;
}
@Override @Override
public String toString() { public String toString() {
return "CartDTO [cartId=" + cartId + ", memberId=" + memberId + ", bookId=" + bookId + ", bookCount=" return "CartDTO [cartId=" + cartId + ", memberId=" + memberId + ", bookId=" + bookId + ", bookCount="
+ bookCount + ", bookName=" + bookName + ", bookPrice=" + bookPrice + ", bookDiscount=" + bookDiscount + bookCount + ", bookName=" + bookName + ", bookPrice=" + bookPrice + ", bookDiscount=" + bookDiscount
+ ", salePrice=" + salePrice + ", totalPrice=" + totalPrice + "]"; + ", salePrice=" + salePrice + ", totalPrice=" + totalPrice + ", point=" + point + ", totalPoint="
} + totalPoint + "]";
}

View File

@@ -1,10 +1,15 @@
package com.vam.service; package com.vam.service;
import java.util.List;
import com.vam.model.CartDTO; import com.vam.model.CartDTO;
public interface CartService { public interface CartService {
/* 장바구니 추가 */ /* 장바구니 추가 */
public int addCart(CartDTO cart); public int addCart(CartDTO cart);
/* 장바구니 정보 리스트 */
public List<CartDTO> getCartList(String memberId);
} }

View File

@@ -1,5 +1,7 @@
package com.vam.service; package com.vam.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -29,6 +31,19 @@ public class CartServiceImpl implements CartService {
} }
} }
@Override
public List<CartDTO> getCartList(String memberId) {
List<CartDTO> cart = cartMapper.getCart(memberId);
for(CartDTO dto : cart) {
dto.initSaleTotal();
}
return cart;
}
} }

View File

@@ -0,0 +1,12 @@
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
${cartInfo}
</body>
</html>

View File

@@ -38,7 +38,7 @@
마이룸 마이룸
</li> </li>
<li> <li>
장바구니 <a href="/cart/${member.memberId}">장바구니</a>
</li> </li>
</c:if> </c:if>
<li> <li>
@@ -123,7 +123,11 @@
<div class="discount_price"> <div class="discount_price">
판매가 : <span class="discount_price_number"><fmt:formatNumber value="${goodsInfo.bookPrice - (goodsInfo.bookPrice*goodsInfo.bookDiscount)}" pattern="#,### 원" /></span> 판매가 : <span class="discount_price_number"><fmt:formatNumber value="${goodsInfo.bookPrice - (goodsInfo.bookPrice*goodsInfo.bookDiscount)}" pattern="#,### 원" /></span>
[<fmt:formatNumber value="${goodsInfo.bookDiscount*100}" pattern="###" />% [<fmt:formatNumber value="${goodsInfo.bookDiscount*100}" pattern="###" />%
<fmt:formatNumber value="${goodsInfo.bookPrice*goodsInfo.bookDiscount}" pattern="#,### 원" /> 할인]</div> <fmt:formatNumber value="${goodsInfo.bookPrice*goodsInfo.bookDiscount}" pattern="#,### 원" /> 할인]
</div>
<div>
적립 포인트 : <span class="point_span"></span>원
</div>
</div> </div>
<div class="line"> <div class="line">
</div> </div>
@@ -231,7 +235,13 @@ $(document).ready(function(){
$(".publeyear").html(publeYear); $(".publeyear").html(publeYear);
}); /* 포인트 삽입 */
let salePrice = "${goodsInfo.bookPrice - (goodsInfo.bookPrice*goodsInfo.bookDiscount)}"
let point = salePrice*0.05;
point = Math.floor(point);
$(".point_span").text(point);
}); //$(document).ready(function(){
// 수량 버튼 조작 // 수량 버튼 조작

View File

@@ -38,7 +38,7 @@
마이룸 마이룸
</li> </li>
<li> <li>
장바구니 <a href="/cart/${member.memberId}">장바구니</a>
</li> </li>
</c:if> </c:if>
<li> <li>

View File

@@ -38,7 +38,7 @@
마이룸 마이룸
</li> </li>
<li> <li>
장바구니 <a href="/cart/${member.memberId}">장바구니</a>
</li> </li>
</c:if> </c:if>
<li> <li>

View File

@@ -1,5 +1,5 @@
#Generated by Maven Integration for Eclipse #Generated by Maven Integration for Eclipse
#Sat Nov 13 22:18:14 KST 2021 #Tue Nov 23 14:20:08 KST 2021
m2e.projectLocation=C\:\\Users\\sjinj\\git\\Blog_Project2\\VamPa m2e.projectLocation=C\:\\Users\\sjinj\\git\\Blog_Project2\\VamPa
m2e.projectName=VamPa m2e.projectName=VamPa
groupId=com.vam groupId=com.vam

View File

@@ -5,6 +5,9 @@ import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
@@ -42,6 +45,15 @@ public class CartController {
int result = cartService.addCart(cart); int result = cartService.addCart(cart);
return result + ""; return result + "";
}
/* 장바구니 페이지 이동 */
@GetMapping("/cart/{memberId}")
public String cartPageGET(@PathVariable("memberId") String memberId, Model model) {
model.addAttribute("cartInfo", cartService.getCartList(memberId));
return "/cart";
} }
} }

View File

@@ -22,6 +22,10 @@ public class CartDTO {
private int salePrice; private int salePrice;
private int totalPrice; private int totalPrice;
private int point;
private int totalPoint;
public int getCartId() { public int getCartId() {
return cartId; return cartId;
@@ -87,16 +91,27 @@ public class CartDTO {
return totalPrice; return totalPrice;
} }
public int getPoint() {
return point;
}
public int getTotalPoint() {
return totalPoint;
}
public void initSaleTotal() { public void initSaleTotal() {
this.salePrice = (int) (this.bookPrice * (1-this.bookDiscount)); this.salePrice = (int) (this.bookPrice * (1-this.bookDiscount));
this.totalPrice = this.salePrice*this.bookCount; this.totalPrice = this.salePrice*this.bookCount;
} this.point = (int)(Math.floor(this.salePrice*0.05));
this.totalPoint =this.point * this.bookCount;
}
@Override @Override
public String toString() { public String toString() {
return "CartDTO [cartId=" + cartId + ", memberId=" + memberId + ", bookId=" + bookId + ", bookCount=" return "CartDTO [cartId=" + cartId + ", memberId=" + memberId + ", bookId=" + bookId + ", bookCount="
+ bookCount + ", bookName=" + bookName + ", bookPrice=" + bookPrice + ", bookDiscount=" + bookDiscount + bookCount + ", bookName=" + bookName + ", bookPrice=" + bookPrice + ", bookDiscount=" + bookDiscount
+ ", salePrice=" + salePrice + ", totalPrice=" + totalPrice + "]"; + ", salePrice=" + salePrice + ", totalPrice=" + totalPrice + ", point=" + point + ", totalPoint="
} + totalPoint + "]";
}
} }

View File

@@ -1,10 +1,15 @@
package com.vam.service; package com.vam.service;
import java.util.List;
import com.vam.model.CartDTO; import com.vam.model.CartDTO;
public interface CartService { public interface CartService {
/* 장바구니 추가 */ /* 장바구니 추가 */
public int addCart(CartDTO cart); public int addCart(CartDTO cart);
/* 장바구니 정보 리스트 */
public List<CartDTO> getCartList(String memberId);
} }

View File

@@ -1,5 +1,7 @@
package com.vam.service; package com.vam.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -30,4 +32,17 @@ public class CartServiceImpl implements CartService {
} }
@Override
public List<CartDTO> getCartList(String memberId) {
List<CartDTO> cart = cartMapper.getCart(memberId);
for(CartDTO dto : cart) {
dto.initSaleTotal();
}
return cart;
}
} }

View File

@@ -0,0 +1,12 @@
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>

View File

@@ -38,7 +38,7 @@
마이룸 마이룸
</li> </li>
<li> <li>
장바구니 <a href="/cart/${member.memberId}">장바구니</a>
</li> </li>
</c:if> </c:if>
<li> <li>

View File

@@ -38,7 +38,7 @@
마이룸 마이룸
</li> </li>
<li> <li>
장바구니 <a href="/cart/${member.memberId}">장바구니</a>
</li> </li>
</c:if> </c:if>
<li> <li>

View File

@@ -38,7 +38,7 @@
마이룸 마이룸
</li> </li>
<li> <li>
장바구니 <a href="/cart/${member.memberId}">장바구니</a>
</li> </li>
</c:if> </c:if>
<li> <li>

View File

@@ -1,5 +1,5 @@
#Generated by Maven Integration for Eclipse #Generated by Maven Integration for Eclipse
#Sat Nov 13 22:18:15 KST 2021 #Tue Nov 23 14:20:08 KST 2021
m2e.projectLocation=C\:\\Users\\sjinj\\git\\Blog_Project2\\VamPa_MySQL m2e.projectLocation=C\:\\Users\\sjinj\\git\\Blog_Project2\\VamPa_MySQL
m2e.projectName=VamPa_MySQL m2e.projectName=VamPa_MySQL
groupId=com.vam groupId=com.vam