반응형
상품 엔티티 개발(비즈니스 로직 추가)
구현 기능
- 상품 등록
- 상품 목록 조회
- 상품 수정
순서
- 상품 엔티티 개발(비즈니스 로직 추가)
- 상품 리포지토리 개발
- 상품 서비스 개발
- 상품 기능 테스트
domain/item/item에 다음 코드 추가
//==비즈니스 로직==//
/**
* stock(재고) 증가
*/
public void addStock(int quantity){
this.stockQuantity += quantity;
}
/**
* stock(재고) 감소
*/
public void removeStock(int quantity){
int restStock = this.stockQuantity - quantity;
if (restStock < 0) {
throw new NotEnoughStockException("need more stock");
}
this.stockQuantity = restStock;
}
- 보통 도메인 주도 설계라고 할 때 엔티티 자체가 해결할 수 있는 것들은 엔티티안에 비즈니스 로직을 넣는 것이 좋다(= 더 객체지향적이다).
- data를 가지고 있는 Entity에 비즈니스 로직이 있으면 응집도가 아무래도 더 좋다.
- item 클래스 안에 stockQuantity(data)가 있으니 같은 클래스에 비즈니스 로직을 작성해준다.
비즈니스 로직 분석
- addStock() 메서드는 파라미터로 넘어온 수만큼 재고를 늘린다. 이 메서드는 재고가 증가하거나 상품 주문을 취소해서 재고를 다시 늘려야 할 때 사용한다.
- removeStock() 메서드는 파라미터로 넘어온 수만큼 재고를 줄인다. 만약 재고가 부족하면 예외가 발생한다. 주로 상품을 주문할 때 사용한다.
예외추가 - jpabook.jpashop/exception/NotEnoughStockException
package jpabook.jpashop.exception;
public class NotEnoughStockException extends RuntimeException {
public NotEnoughStockException() {
super();
}
public NotEnoughStockException(String message) {
super(message);
}
public NotEnoughStockException(String message, Throwable cause) {
super(message, cause);
}
public NotEnoughStockException(Throwable cause) {
super(cause);
}
}
상품 리포지토리 개발
package jpabook.jpashop.repository;
import jakarta.persistence.EntityManager;
import jpabook.jpashop.domain.item.Item;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
@RequiredArgsConstructor
public class ItemRepository {
private final EntityManager em;
public void save(Item item) {
if (item.getId() == null) { //Id가 없으면
em.persist(item); //em.persist()를 통해 신규로 등록
} else { //Id가 있으면(이미 등록된 Id를 DB에서 가져온 것)
em.merge(item); //업데이트한다고 생각하면 된다. 자세한 건 변경 감지와 병합 피드에서 설명
}
}
public Item findOne(Long id) {
return em.find(Item.class, id);
}
public List<Item> findAll() {
return em.createQuery("select i from Item i", Item.class)
.getResultList();
}
}
기능 설명
- save()
- id 가 없으면 신규로 보고 persist() 실행
- id 가 있으면 이미 데이터베이스에 저장된 엔티티를 수정한다고 보고, merge() 를 실행, 자세한 내용은 뒤에 웹에서 설명(그냥 지금은 저장한다 정도로 생각하자)
상품 서비스 개발
package jpabook.jpashop.service;
import jpabook.jpashop.domain.item.Item;
import jpabook.jpashop.repository.ItemRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class ItemService {
private final ItemRepository itemRepository;
@Transactional
public void saveItem(Item item) {
itemRepository.save(item);
}
public List<Item> findItems() {
return itemRepository.findAll();
}
public Item findOne(Long itemId) {
return itemRepository.findOne(itemId);
}
}
- ItemService는 ItemRepository에 단순히 위임만 하는 클래스이다.
- 경우에 따라서 위임만 한다면 controller에서 repository로 바로 접근하는것도 딱히 상관없다.
출처 : 인프런 - 실전! 스프링 부트와 JPA 활용1 - 웹 애플리케이션 개발(김영한 강사님)
'JPA' 카테고리의 다른 글
API 개발 기본 (0) | 2023.05.29 |
---|---|
웹 계층 개발 (0) | 2023.05.21 |
주문 도메인 개발 (0) | 2023.05.21 |
도메인 분석 설계 / 애플리케이션 구현 준비 / 회원 도메인 개발 (0) | 2023.05.10 |
JPA와 DB설정, 동작확인 (0) | 2023.05.10 |