<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{layouts/layout1}">
<head>
<meta name="_csrf" th:content="${_csrf.token}"/>
<meta name="_csrf_header" th:content="${_csrf.headerName}">
</head>
<th:block layout:fragment="css">
<script th:inline="javascript">
function cancelOrder(orderId) {
var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");
var url = "/order/" + orderId + "/cancel";
var paramData = {
orderId : orderId
}
var param = JSON.stringify(paramData);
$.ajax({
url : url,
type : "POST",
contentType : "application/json",
data : param,
beforeSend : function(xhr) {
// 데이터 전송하기 전에 헤더에 csrf 값을 설정
xhr.setRequestHeader(header, token);
},
dataType : "json",
cache : false,
success : function(result, status) {
alert("주문이 취소 되었습니다.");
location.href='/orders/' + [[${page}]];
},
error : function(jqXHR, status, error) {
if(jqXHR.status == '401') {
alert('로그인 후 이용해주세요.');
location.href='/members/login';
} else {
alert(jqXHR.responseText);
}
}
});
}
</script>
<style>
.content-mg {
margin-left: 30%;
margin-right: 30%
margin-top: 2%;
margin-bottom: 100px;
}
.repImgDiv {
margin-right: 15px;
margin-left: 15px;
height: auto;
}
.repImg {
height: 100px;
width: 100px;
}
.card {
width: 750px;
height: 100%;
padding: 30px;
margin-bottom: 20px;
}
.fs18 {
font-size: 18px;
}
.fs24 {
font-size: 24px;
}
</style>
</th:block>
<div layout:fragment="content" class="content-mg">
<h2 class="mb-4">구매 이력</h2>
<div th:each="order : ${orders.getContent()}">
<div class="d-flex mb-3 align-self-center">
<h4 th:text="${order.orderDate} + '주문'"></h4>
<div class="ml-3">
<th:block th:if="${order.orderStatus == T(com.shop.constant.OrderStatus).ORDER}">
<button type="button" class="btn btn-outline-secondary" th:value="${order.orderId}"
onclick="cancelOrder(this.value)">주문취소</button>
</th:block>
<th:block th:unless="${order.orderStatus == T(com.shop.constant.OrderStatus).ORDER}">
<h4>(취소완료)</h4></th:block>
</div>
</div>
<div class="card d-flex">
<div th:each="orderItem : ${order.orderItemDtoList}" class="d-flex mb-3">
<div class="repImgDiv">
<img th:src="${orderItem.imgUrl}" class="rounded repImg" th:alt="${orderItem.itemNm}">
</div>
<div class="align-self-center w-75">
<span th:text="${orderItem.itemNm}" class="fs24 font-weight-bold"></span>
<div class="fs18 font-weight-light">
<span th:text="${orderItem.orderPrice} + '원'"></span>
<span th:text="${orderItem.count} + '개'"></span>
</div>
</div>
</div>
</div>
</div>
<div th:with="start=${(orders.number/maxPage) * maxPage + 1},
end=${(orders.totalPages == 0) ? 1 : (start + (maxPage -1) < orders.totalPages ?
start + (maxPage -1) : orders.totalPages)}">
<ul class="pagination justify-content-center">
<li class="page-item" th:classappend="${orders.number eq 0} ? 'disabled' : ''">
<a th:href="@{'/orders/'+ ${orders.number-1}}" aria-label="Previous" class="page-link">
<span aria-hidden="true">Previous</span>
</a>
</li>
<li class="page-item" th:each="page : ${#numbers.sequence(start, end)}"
th:classappend="${orders.number eq page - 1} ?'active':''">
<a th:href="@{'/orders/'+ ${page - 1}}" th:inline="text" class="page-link">[[${page}]]</a>
</li>
<li class="page-item" th:classappend="${orders.number+1 ge orders.totalPages}? 'disabled' : ''">
<a th:href="@{'/orders/'+ ${orders.number + 1}}" aria-label="Next" class="page-link">
<span aria-hidden="true">Next</span>
</a>
</li>
</ul>
</div>
</div>
</html>
package com.shop.entity;
import com.shop.constant.ItemSellStatus;
import com.shop.dto.ItemFormDto;
import com.shop.exception.OutOfStockException;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.time.LocalDateTime;
import java.util.List;
@Entity
@Table(name = "item")
@Getter
@Setter
@ToString
public class Item extends BaseEntity{
@Id
@Column(name = "item_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id; // 상품 코드
@Column(nullable = false, length = 50)
private String itemNm; // 상품명
@Column(name = "price", nullable = false)
private int price; // 가격
@Column(nullable = false)
private int stockNumber; // 수량
@Lob
@Column(nullable = false)
private String itemDetail; // 상품 상세 설명
@Enumerated
private ItemSellStatus itemSellStatus; // 상품 판매 상태
// private LocalDateTime regTime; // 등록 시간
//
// private LocalDateTime updateTime; // 수정 시간
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
name = "member_item",
joinColumns = @JoinColumn(name = "member_id"),
inverseJoinColumns = @JoinColumn(name = "item_id")
)
private List<Member> member;
public void updateItem(ItemFormDto itemFormDto) {
this.itemNm = itemFormDto.getItemNm();
this.price = itemFormDto.getPrice();
this.stockNumber = itemFormDto.getStockNumber();
this.itemDetail = itemFormDto.getItemDetail();
this.itemSellStatus = itemFormDto.getItemSellStatus();
}
public void removeStock(int stockNumber) {
int restStock = this.stockNumber - stockNumber; // 10, 5 / 10, 20
if(restStock < 0) {
throw new OutOfStockException("상품의 재고가 부족합니다.(현재 재고 수량: " + this.stockNumber + ")");
}
this.stockNumber = restStock;
}
public void addStock(int stockNumber) {
this.stockNumber += stockNumber;
}
}
package com.shop.entity;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import java.time.LocalDateTime;
@Entity
@Getter
@Setter
public class OrderItem extends BaseEntity{
@Id
@GeneratedValue
@Column(name = "order_item_id")
private Long id;
@ManyToOne
@JoinColumn(name = "item_id")
private Item item;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "order_id")
private Order order;
private int orderPrice;
private int count;
// private LocalDateTime regTime;
// private LocalDateTime updateTime;
// item(상품) -> OrderItem(주문 상품)
public static OrderItem createOrderItem(Item item, int count) {
OrderItem orderItem = new OrderItem();
orderItem.setItem(item);
orderItem.setCount(count);
orderItem.setOrderPrice(item.getPrice());
item.removeStock(count);
return orderItem;
}
public int getTotalPrice() {
return orderPrice * count;
}
public void cancel() {
this.getItem().addStock(count);
}
}
package com.shop.entity;
import com.shop.constant.OrderStatus;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@Entity
@Table(name = "orders")
@Getter
@Setter
public class Order extends BaseEntity {
@Id
@GeneratedValue
@Column(name = "order_id")
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;
private LocalDateTime orderDate;
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true,
fetch = FetchType.LAZY) // 양방향이면 주인과 부하 -> order가 orderItem 관리한다
private List<OrderItem> orderItems = new ArrayList<>();
@Enumerated(EnumType.STRING)
private OrderStatus orderStatus;
// private LocalDateTime regTime;
//
// private LocalDateTime updateTime;
// 주문서 주문 아이템 리스트에 주문 아이템 추가
// 주문 아이템에 주문서 추가
public void addOrderItem(OrderItem orderItem) {
orderItems.add(orderItem);
orderItem.setOrder(this);
}
// 주문서 생성
// 현재 로그인된 멤버 주문서에 추가
// 주문 아이템 리스트를 반복문을 통해서 주문서에 추가
// 상태는 주문으로 세팅
// 주문 시간은 현재 시간으로 세팅
// 주문서 리턴
public static Order createOrder(Member member, List<OrderItem> orderItemList) {
Order order = new Order();
order.setMember(member);
for (OrderItem orderItem : orderItemList) {
order.addOrderItem(orderItem);
}
order.setOrderStatus(OrderStatus.ORDER);
order.setOrderDate(LocalDateTime.now());
return order;
}
// 주문서에 있는 주문마다 아이템 리스트를 반복
// 주문 아이템마다 총 가격을 totalPrice에 추가
public int getTotalPrice() {
int totalPrice = 0;
for (OrderItem orderItem : orderItems) {
totalPrice += orderItem.getTotalPrice();
}
return totalPrice;
}
public void cancelOrder() {
this.orderStatus = OrderStatus.CANCEL;
for (OrderItem orderItem : orderItems) {
orderItem.cancel();
}
}
}
package com.shop.service;
import com.shop.dto.OrderDto;
import com.shop.dto.OrderHistDto;
import com.shop.dto.OrderItemDto;
import com.shop.entity.*;
import com.shop.repository.ItemImgRepository;
import com.shop.repository.ItemRepository;
import com.shop.repository.MemberRepository;
import com.shop.repository.OrderRepository;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
@Service
@Transactional
@RequiredArgsConstructor
public class OrderService {
private final ItemRepository itemRepository;
private final MemberRepository memberRepository;
private final OrderRepository orderRepository;
private final ItemImgRepository itemImgRepository;
public Long order(OrderDto orderDto, String email) {
Item item = itemRepository.findById(orderDto.getItemId())
.orElseThrow(EntityNotFoundException::new);
Member member = memberRepository.findByEmail(email);
List<OrderItem> orderItemList = new ArrayList<>();
OrderItem orderItem = OrderItem.createOrderItem(item, orderDto.getCount());
orderItemList.add(orderItem);
Order order = Order.createOrder(member, orderItemList);
orderRepository.save(order);
return order.getId();
}
// Entity Order, OrderItem
// change OrderHistDto, OrderItemDto-> result Page<OrderHistDto>
@Transactional(readOnly = true)
public Page<OrderHistDto> getOrderList(String email, Pageable pageable) {
List<Order> orders = orderRepository.findOrders(email, pageable);
Long totalCount = orderRepository.countOrder(email);
List<OrderHistDto> orderHistDtos = new ArrayList<>();
// Order -> OrderHistDto
// OrderItem -> OrderItemDto+
for (Order order : orders) {
OrderHistDto orderHistDto = new OrderHistDto(order);
List<OrderItem> orderItems = order.getOrderItems();
for (OrderItem orderItem : orderItems) {
ItemImg itemImg = itemImgRepository.findByItemIdAndRepImgYn(orderItem.getItem().getId(),
"Y");
OrderItemDto orderItemDto = new OrderItemDto(orderItem, itemImg.getImgUrl());
orderHistDto.addOrderItemDto(orderItemDto);
}
orderHistDtos.add(orderHistDto);
}
return new PageImpl<OrderHistDto>(orderHistDtos, pageable, totalCount);
}
@Transactional(readOnly = true)
public boolean validateOrder(Long orderId, String email) {
Member curMember = memberRepository.findByEmail(email);
Order order = orderRepository.findById(orderId)
.orElseThrow(EntityNotFoundException::new);
Member savedMember = order.getMember();
if (!StringUtils.equals(curMember.getEmail(), savedMember.getEmail())) {
return false;
}
return true;
}
public void cancelOrder(Long orderId) {
Order order = orderRepository.findById(orderId)
.orElseThrow(EntityNotFoundException::new);
order.cancelOrder();
}
}
package com.shop.controller;
import com.shop.dto.OrderDto;
import com.shop.dto.OrderHistDto;
import com.shop.service.OrderService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.domain.Pageable;
import java.security.Principal;
import java.util.List;
import java.util.Optional;
@Controller
@RequiredArgsConstructor
public class OrderController {
private final OrderService orderService;
// ajax
@PostMapping(value = "/order")
public @ResponseBody
ResponseEntity order(@RequestBody @Valid OrderDto orderDto, BindingResult bindingResult,
Principal principal) {
// String a = "abc" + "def"
// StringBuilder a;
// a.append("abc");
// a.append("def");
if (bindingResult.hasErrors()) {
StringBuilder sb = new StringBuilder();
List<FieldError> fieldErrors = bindingResult.getFieldErrors();
for (FieldError fieldError : fieldErrors) {
sb.append(fieldError.getDefaultMessage());
}
return new ResponseEntity<String>(sb.toString(), HttpStatus.BAD_REQUEST);
}
// 로그인 정보 -> Spring security
// principal.getName() (현재 로그인 된 정보)
String email = principal.getName();
Long orderId;
try {
orderId = orderService.order(orderDto, email);
} catch (Exception e) {
return new ResponseEntity<String>(e.getMessage(), HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<Long>(orderId, HttpStatus.OK);
}
@GetMapping(value = {"/orders", "/orders/{page}"})
public String orderHist(@PathVariable("page") Optional<Integer> page,
Principal principal, Model model) {
Pageable pageable = PageRequest.of(page.isPresent() ? page.get() : 0, 5);
Page<OrderHistDto> orderHistDtoList = orderService.getOrderList(principal.getName(), pageable);
model.addAttribute("orders", orderHistDtoList);
model.addAttribute("page", pageable.getPageNumber());
model.addAttribute("maxPage", 5);
return "order/orderHist";
}
@PostMapping("/order/{orderId}/cancel")
public @ResponseBody ResponseEntity cancelOrder(@PathVariable("orderId") Long orderId,
Principal principal) {
if (!orderService.validateOrder(orderId, principal.getName())) {
return new ResponseEntity<String>("주문 취소 권한이 없습니다.", HttpStatus.FORBIDDEN);
}
orderService.cancelOrder(orderId);
return new ResponseEntity<Long>(orderId, HttpStatus.OK);
}
}
package com.shop.dto;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class CartItemDto {
@NotNull(message = "상품 아이디는 필수 입력 값입니다.")
private Long itemId;
@Min(value = 1, message = "최소 1개 이상 담아주세요.")
private int count;
}
package com.shop.entity;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Entity
@Table(name = "cart")
@Getter
@Setter
@ToString
public class Cart {
@Id
@Column(name = "cart_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;
public static Cart createCart(Member member) {
Cart cart = new Cart();
cart.setMember(member);
return cart;
}
}
package com.shop.entity;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
@Entity
@Getter
@Setter
@Table(name = "cart_item")
public class CartItem extends BaseEntity{
@Id
@GeneratedValue
@Column(name = "cart_item_id")
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "cart_id")
private Cart cart;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "item_id")
private Item item;
private int count;
public static CartItem createCartItem(Cart cart, Item item, int count) {
CartItem cartItem = new CartItem();
cartItem.setCart(cart);
cartItem.setItem(item);
cartItem.setCount(count);
return cartItem;
}
public void addCount(int count) {
this.count += count;
}
public void updateCount(int count) {
this.count = count;
}
}
package com.shop.controller;
import com.shop.dto.CartDetailDto;
import com.shop.dto.CartItemDto;
import com.shop.service.CartService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import java.security.Principal;
import java.util.List;
@Controller
@RequiredArgsConstructor
public class CartController {
private final CartService cartService;
@PostMapping(value = "/cart")
ResponseEntity order(@RequestBody @Valid CartItemDto cartItemDto,
BindingResult bindingResult, Principal principal) {
if (bindingResult.hasErrors()) {
StringBuilder sb = new StringBuilder();
List<FieldError> fieldErrors = bindingResult.getFieldErrors();
for (FieldError fieldError : fieldErrors) {
sb.append(fieldError.getDefaultMessage());
}
return new ResponseEntity<String>(sb.toString(), HttpStatus.BAD_REQUEST);
}
String email = principal.getName();
Long cartItemId;
try {
cartItemId = cartService.addCart(cartItemDto, email);
} catch (Exception e) {
return new ResponseEntity<String>(e.getMessage(), HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<Long>(cartItemId, HttpStatus.OK);
}
@GetMapping(value = "/cart")
public String orderHist(Principal principal, Model model) {
List<CartDetailDto> cartDetailDtoList = cartService.getCartList(principal.getName());
model.addAttribute("cartItems", cartDetailDtoList);
return "cart/cartList";
}
@PatchMapping(value = "/cartItem/{cartItemId}")
public @ResponseBody ResponseEntity updateCartItem(@PathVariable("cartItemId") Long cartItemId,
int count, Principal principal) {
System.out.println(cartItemId);
if (count <= 0) {
return new ResponseEntity<String>("최소 1개 이상 담아주세요.", HttpStatus.BAD_REQUEST);
} else if (!cartService.validateCartItem(cartItemId, principal.getName())) {
return new ResponseEntity<String>("수정 권한이 없습니다.", HttpStatus.FORBIDDEN);
}
cartService.updateCartItemCount(cartItemId, count);
return new ResponseEntity<Long>(cartItemId, HttpStatus.OK);
}
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{layouts/layout1}">
<head>
<meta name="_csrf" th:content="${_csrf.token}"/>
<meta name="_csrf_header" th:content="${_csrf.headerName}"/>
</head>
<th:block layout:fragment="script">
<script th:inline="javascript">
$(document).ready(function() {
calculateTotalPrice();
$("#count").change(function() {
calculateTotalPrice();
});
});
function calculateTotalPrice() {
var count = $("#count").val();
var price = $("#price").val();
var totalPrice = price * count;
$("#totalPrice").html(totalPrice + '원');
}
function order() {
var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");
var url = "/order";
var paramData = {
itemId : $("#itemId").val(),
count : $("#count").val()
}
var param = JSON.stringify(paramData);
$.ajax({
url : url,
type : "POST",
contentType : "application/json",
data : param,
beforeSend : function(xhr) {
// 데이터 전송하기 전에 헤더에 csrf 값을 설정
xhr.setRequestHeader(header, token);
},
dataType : "json",
cache : false,
success : function(result, status) {
alert("주문이 완료 되었습니다.");
location.href='/';
},
error : function(jqXHR, status, error) {
if(jqXHR.status == '401') {
alert('로그인 후 이용해주세요.');
location.href='/members/login';
} else {
alert(jqXHR.responseText);
}
}
});
}
function addCart() {
var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");
var url = "/cart";
var paramData = {
itemId : $("#itemId").val(),
count : $("#count").val()
};
var param = JSON.stringify(paramData);
$.ajax({
url : url,
type : "POST",
contentType : "application/json",
data : param,
beforeSend : function(xhr) {
// 데이터 전송하기 전에 헤더에 csrf 값을 설정
xhr.setRequestHeader(header, token);
},
dataType : "json",
cache : false,
success : function(result, status) {
alert("상품을 장바구니에 담았습니다.");
location.href='/';
},
error : function(jqXHR, status, error) {
if(jqXHR.status == '401') {
alert('로그인 후 이용해주세요.');
location.href='/members/login';
} else {
alert(jqXHR.responseText);
}
}
});
}
</script>
</th:block>
<!--사용자 css 추가-->
<th:block layout:fragment="css">
<style>
.mgb-15 {
margin-bottom: 15px;
}
.mgt-30 {
margin-top: 30px;
}
.mgt-50 {
margin-top: 50px;
}
.repImgDiv {
margin-right: 15px;
height: auto;
width: 50%;
}
.repImg {
width: 100%;
height: 400px;
}
.wd50 {
height: auto;
width: 50%;
}
</style>
</th:block>
<div layout:fragment="content" style="margin-left:25%, margin-right: 25%">
<input type="hidden" id="itemId" th:value="${item.id}">
<div class="d-flex">
<div class="repImgDiv">
<img th:src="${item.itemImgDtoList[0].imgUrl}" class="rounded repImg" th:alt="${item.itemNm}">
</div>
<div class="wd50">
<span th:if="${item.itemSellStatus == T(com.shop.constant.ItemSellStatus).SELL}"
class="badge bg-primary mgb-15">판매중</span>
<span th:unless="${item.itemSellStatus == T(com.shop.constant.ItemSellStatus).SELL}"
class="badge bg-primary mgb-15">품절</span>
<div class="h4" th:text="${item.itemNm}"></div>
<hr class="my-4">
<div class="text-right">
<div class="h4 text-danger text-left">
<input type="hidden" th:value="${item.price}" id="price" name="price">
<span th:text="${item.price}"></span>원
</div>
<div class="input-group w-50">
<div class="input-group-prepend">
<span class="input-group-text">수량</span>
</div>
<input type="number" name="count" id="count" class="form-control" value="1" min="1">
</div>
</div>
<hr class="my-4">
<div class="text-right mgt-50">
<h5>결제 금액</h5>
<h3 name="totalPrice" id="totalPrice" class="font-weight-bold"></h3>
</div>
<div th:if="${item.itemSellStatus == T(com.shop.constant.ItemSellStatus).SELL}"
class="text-right">
<button type="button" class="btn btn-light border border-primary btn-lg"
onclick="addCart()">
장바구니 담기</button>
<button type="button" class="btn btn-primary btn-lg" onclick="order()">주문하기</button>
</div>
<div th:unless="${item.itemSellStatus == T(com.shop.constant.ItemSellStatus).SELL}"
class="text-right">
<button type="button" class="btn btn-danger btn-lg">품절</button>
</div>
</div>
</div>
<div class="mgt-30">
<div class="container">
<h4 class="border border-success-subtle rounded-pill display-5">상품 상세 설명</h4>
<hr class="my-4">
<p class="lead" th:text="${item.itemDetail}"></p>
</div>
</div>
<div th:each="itemImg : ${item.itemImgDtoList}" class="text-center">
<img th:if="${not #strings.isEmpty(itemImg.imgUrl)}" th:src="${itemImg.imgUrl}"
class="rounded mgb-15"
width="800">
</div>
</div>
</html>
package com.shop.repository;
import com.shop.entity.Cart;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CartRepository extends JpaRepository<Cart, Long> {
Cart findByMemberId(Long memberId);
}
package com.shop.repository;
import com.shop.dto.CartDetailDto;
import com.shop.entity.CartItem;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
public interface CartItemRepository extends JpaRepository<CartItem, Long> {
CartItem findByCartIdAndItemId(Long cartId, Long itemId);
@Query("select new com.shop.dto.CartDetailDto(ci.id, i.itemNm, i.price, ci.count, im.imgUrl) " +
"from CartItem ci, ItemImg im " +
"join ci.item i " +
"where ci.cart.id = :cartId " +
"and im.item.id = ci.item.id " +
"and im.repImgYn = 'Y' " +
"order by ci.regTime desc")
List<CartDetailDto> findCartDetailDtoList(Long cartId);
}
package com.shop.service;
import com.shop.dto.CartDetailDto;
import com.shop.dto.CartItemDto;
import com.shop.entity.Cart;
import com.shop.entity.CartItem;
import com.shop.entity.Item;
import com.shop.entity.Member;
import com.shop.repository.CartItemRepository;
import com.shop.repository.CartRepository;
import com.shop.repository.ItemRepository;
import com.shop.repository.MemberRepository;
import jakarta.persistence.EntityExistsException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
@Service
@RequiredArgsConstructor
@Transactional
public class CartService {
private final ItemRepository itemRepository;
private final MemberRepository memberRepository;
private final CartRepository cartRepository;
private final CartItemRepository cartItemRepository;
private final OrderService orderService;
public Long addCart(CartItemDto cartItemDto, String email) {
Item item = itemRepository.findById(cartItemDto.getItemId())
.orElseThrow(EntityExistsException::new);
Member member = memberRepository.findByEmail(email);
Cart cart = cartRepository.findByMemberId(member.getId());
if (cart == null) {
cart = Cart.createCart(member);
cartRepository.save(cart);
}
CartItem savedCartItem = cartItemRepository.findByCartIdAndItemId(cart.getId(), item.getId());
if (savedCartItem != null) {
savedCartItem.addCount(cartItemDto.getCount());
return savedCartItem.getId();
} else {
CartItem cartItem = CartItem.createCartItem(cart, item, cartItemDto.getCount());
cartItemRepository.save(cartItem);
return cartItem.getId();
}
}
@Transactional(readOnly = true)
public List<CartDetailDto> getCartList(String email) {
List<CartDetailDto> cartDetailDtoList = new ArrayList<>();
Member member = memberRepository.findByEmail(email);
Cart cart = cartRepository.findByMemberId(member.getId());
if (cart == null) {
return cartDetailDtoList;
}
cartDetailDtoList = cartItemRepository.findCartDetailDtoList(cart.getId());
return cartDetailDtoList;
}
@Transactional(readOnly = true)
public boolean validateCartItem(Long cartItemId, String email) {
Member curMember = memberRepository.findByEmail(email);
CartItem cartItem = cartItemRepository.findById(cartItemId)
.orElseThrow(EntityExistsException::new);
Member savedMember = cartItem.getCart().getMember();
if (!StringUtils.equals(curMember.getEmail(), savedMember.getEmail())) {
return false;
}
return true;
}
public void updateCartItemCount(Long cartItemId, int count) {
CartItem cartItem = cartItemRepository.findById(cartItemId)
.orElseThrow(EntityExistsException::new);
cartItem.updateCount(count);
}
}
package com.shop.dto;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class CartDetailDto {
private Long cartItemId;
private String itemNm;
private int price;
private int count;
private String imgUrl;
public CartDetailDto(Long cartItemId, String itemNm, int price, int count, String imgUrl) {
this.cartItemId = cartItemId;
this.itemNm = itemNm;
this.price = price;
this.imgUrl = imgUrl;
this.count = count;
}
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{layouts/layout1}">
<head>
<meta name="_csrf" th:content="${_csrf.token}"/>
<meta name="_csrf_header" th:content="${_csrf.headerName}"/>
</head>
<th:block layout:fragment="script">
<script th:inline="javascript">
$(document).ready(function() {
$("input[name=cartChkBox]").change(function() {
getOrderTotalPrice();
});
});
function getOrderTotalPrice() {
var orderTotalPrice = 0;
$("input[name=cartChkBox]:checked").each(function() {
var cartItemId = $(this).val();
var price = $("#price_" + cartItemId).attr("data-price");
var count = $("#count_" + cartItemId).val();
orderTotalPrice += price * count;
});
$("#orderTotalPrice").html(orderTotalPrice+'원');
}
function changeCount(obj) {
var count = obj.value;
var cartItemId = obj.id.split('_')[1];
var price = $("#price_" + cartItemId).data("price");
var totalPrice = count * price;
$("#totalPrice_" + cartItemId).html(totalPrice+"원");
getOrderTotalPrice();
updateCartItemCount(cartItemId, count);
}
function checkAll() {
if($("#checkall").prop("checked")) {
$("input[name=cartChkBox]").prop("checked", true);
} else {
$("input[name=cartChkBox]").prop("checked", false);
}
getOrderTotalPrice();
}
</script>
</th:block>
<!--사용자 css 추가-->
<th:block layout:fragment="css">
<style>
.content-mg {
margin-left: 30%;
margin-right: 30%;
margin-top: 2%;
margin-bottom: 100px;
}
.repImgDiv {
margin-right: 15px;
margin-left: 15px;
height: auto;
}
.repImg {
height: 100px;
width: 100px;
}
.card {
width: 750px;
height: 100%;
padding: 30px;
margin-bottom: 20px;
}
.fs18 {
font-size: 18px;
}
.fs24 {
font-size: 24px;
}
</style>
</th:block>
<div layout:fragment="content" class="content-mg">
<h2 class="mb-4">장바구니 목록</h2>
<div>
<table class="table">
<colgroup>
<col width="15%">
<col width="70%">
<col width="15%">
</colgroup>
<thead>
<tr class="text-center">
<td>
<input type="checkbox" id="checkall" onclick="checkAll()"> 전체선택
</td>
<td>상품정보</td>
<td>상품금액</td>
</tr>
</thead>
<tbody>
<tr th:each="cartItem : ${cartItems}">
<td class="text-center align-middle">
<input type="checkbox" name="cartChkBox" th:value="${cartItem.cartItemId}">
</td>
<td class="d-flex">
<div class="repImgDiv align-self-center">
<img th:src="${cartItem.imgUrl}" class="rounded repImg" th:alt="${cartItem.itemNm}">
</div>
<div class="align-self-center">
<span th:text="${cartItem.itemNm}" class="fs24 font-weight-bold"></span>
<div class="fs18 font-weight-light">
<span class="input-group mt-2">
<span th:id="'price_' + ${cartItem.cartItemId}" th:data-price="${cartItem.price}"
th:text="${cartItem.price} + '원'" class="align-self-center mr-2">
</span>
<input type="number" name="count" th:id="'count_' + ${cartItem.cartItemId}"
th:value="${cartItem.count}" min="1" onchange="changeCount(this)">
<button type="button" class="close" aria-label="Close">
<span aria-hidden="true" th:data-id="${cartItem.cartItemId}"
onclick="deleteCartItem(this)">×</span>
</button>
</span>
</div>
</div>
</td>
<td class="text-center align-middle">
<span th:id="'totalPrice_' + ${cartItem.cartItemId}" name="totalPrice"
th:text="${cartItem.price * cartItem.count} + '원'"></span>
</td>
</tr>
</tbody>
</table>
<h2 class="text-center">
총 주문 금액 : <span id="orderTotalPrice" class="text-danger">0원</span>
</h2>
<div class="text-center mt-3">
<button type="button" class="btn btn-primary btn-lg" onclick="orders()">주문하기</button>
</div>
</div>
</div>
</html>
오늘도 장바구니를 구현했다. 넘 힘들당