Order.java
// ====생성 메서드====
// 복잡한 로직을 한번에 처리할 수 있음
public static Order createOrder(Member member, Delivery delivery,
OrderItem... orderItems) {
Order order = new Order();
order.setMember(member);
order.setDelivery(delivery);
for (OrderItem orderItem : orderItems) {
order.addOrderItem(orderItem);
}
// 처음 주문상태를 ORDER 로 지정
order.setStatus(OrderStatus.ORDER);
order.setOrderDate(LocalDateTime.now());
return order;
}
// ==비즈니스 로직==
// 주문 취소
public void cancel() {
if (delivery.getStatus() == DeliveryStatus.COMP) {
throw new IllegalStateException("이미 배송완료된 상품은 취소가 불가능합니 다.");
}
this.setStatus(OrderStatus.CANCEL);
// 재고를 원복
for (OrderItem orderItem : orderItems) {
orderItem.cancel();
}
}
//==조회 로직==//
// 전체 주문 가격 조회
public int getTotalPrice() {
int totalPrice = 0;
for (OrderItem orderItem : orderItems) {
totalPrice += orderItem.getTotalPrice();
}
return totalPrice;
}
OrderItem.java
// ==== 생성 메서드 ====
public static OrderItem createOrderItem(Item item, int orderPrice, int count) {
OrderItem orderItem = new OrderItem();
orderItem.setItem(item);
orderItem.setOrderPrice(orderPrice);
orderItem.setCount(count);
item.removeStock(count);
return orderItem;
}
// ==== 비즈니스 로직 ====
// 주문취소 -> 재고를 원복
public void cancel() {
getItem().addStock(count);
}
// ==== 조회 로직 ====
// 주문상품 전체 가격 조회
public int getTotalPrice() {
return getOrderPrice() * getCount();
}
OrderRepository.java
package sorzzzzy.dearmydog.repository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import sorzzzzy.dearmydog.domain.Order;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import java.util.List;
@Repository
@RequiredArgsConstructor
public class OrderRepository {
private final EntityManager em;
public void save(Order order) {
em.persist(order);
}
public Order findOne(Long id) {
return em.find(Order.class, id);
}
public List<Order> findAllByString(OrderSearch orderSearch) {
String jpql = "select o from Order o join o.member m";
boolean isFirstCondition = true;
// 주문 상태 검색
if (orderSearch.getOrderStatus() != null) {
if (isFirstCondition) {
jpql += " where";
isFirstCondition = false;
} else {
jpql += " and";
}
jpql += " o.status = :status";
}
// 회원 이름 검색
if (StringUtils.hasText(orderSearch.getMemberName())) {
if (isFirstCondition) {
jpql += " where";
isFirstCondition = false;
} else {
jpql += " and";
}
jpql += " m.name like :name";
}
TypedQuery<Order> query = em.createQuery(jpql, Order.class)
.setMaxResults(1000);
if (orderSearch.getOrderStatus() != null) {
query = query.setParameter("status", orderSearch.getOrderStatus());
}
if (StringUtils.hasText(orderSearch.getMemberName())) {
query = query.setParameter("name", orderSearch.getMemberName());
}
return query.getResultList();
}
}
아 JPQL 적응 안 돼 어려워 후ㅡ엉어어어어어어
OrderService.java
package sorzzzzy.dearmydog.service;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sorzzzzy.dearmydog.domain.*;
import sorzzzzy.dearmydog.domain.Item.Item;
import sorzzzzy.dearmydog.repository.ItemRepository;
import sorzzzzy.dearmydog.repository.MemberRepository;
import sorzzzzy.dearmydog.repository.OrderRepository;
import sorzzzzy.dearmydog.repository.OrderSearch;
import java.util.List;
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class OrderService {
private final MemberRepository memberRepository;
private final OrderRepository orderRepository;
private final ItemRepository itemRepository;
@Transactional
public Long order(Long memberId, Long itemId, int count) {
// 엔티티 조회
Member member = memberRepository.findOne(memberId);
Item item = itemRepository.findOne(itemId);
// 배송정보 생성
Delivery delivery = new Delivery();
delivery.setAddress(member.getAddress());
delivery.setStatus(DeliveryStatus.READY);
// 주문상품 생성
OrderItem orderItem = OrderItem.createOrderItem(item, item.getPrice(), count);
// 주문 생성
Order order = Order.createOrder(member, delivery, orderItem);
// 주문 저장
orderRepository.save(order);
return order.getId();
}
// 주문 취소
@Transactional
public void cancelOrder(Long orderId) {
// 주문 엔티티 조회
Order order = orderRepository.findOne(orderId);
// 주문 취소
order.cancel();
}
// 검색
public List<Order> findOrders(OrderSearch orderSearch) {
return orderRepository.findAllByString(orderSearch);
}
}
JPQL 저 자식 때문에^^ 엄청나게 낑낑거렸지만^^ 이것 또한 교훈이겠죠^^
이제 웹개발 해야하는데 걱정된다 헤헤