jae_coding

[Spring Project] 상품 도메인 개발 본문

Spring, java/Spring_Project

[Spring Project] 상품 도메인 개발

재코딩 2022. 8. 22. 17:53
반응형

목차

  • 상품 엔티티 비지니스로직 추가하기
  • 상품 Repository
  • 상품 Service

1. 상품 엔티티 비지니스 로직 추가하기

package jpabook.jpashop.domain.Item;

import jpabook.jpashop.domain.Category;
import jpabook.jpashop.exception.NotEnoughStockException;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Getter
@Setter
//상속관계 전략
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="dtype")

public abstract class Item {
    @Id @GeneratedValue
    @Column(name = "item_id")
    private Long id;

    private String name;
    private int price;
    private int stockQuantity;

    @ManyToMany(mappedBy = "items")
    private List<Category> categories = new ArrayList<>();

    //==비지니스 로직 ==//
    //재고 수량 증가
    private void addStock(int Quantity){
        this.stockQuantity += Quantity;
    }

    //재고 수량 감소
    private void removeStock(int Quantity){
        int restStock = this.stockQuantity - Quantity;
        if(restStock < 0){
            throw new NotEnoughStockException("need more stock");
        }
        this.stockQuantity = restStock;
    }
}

Exception 추가하기

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);
    }

}

2. 상품 Repository

package jpabook.jpashop.repository;

import jpabook.jpashop.domain.Item.Item;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import java.util.List;

@Repository
@RequiredArgsConstructor
public class ItemRepository {
    private final EntityManager em;

    // 아이템 저장
    public void save(Item item){
        if(item.getId() == null){
            em.persist(item);
        } else{
            em.merge(item); //update와 비슷하다.
        }
    }

    // 아이템 1개 조회
    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();
    }
}

3. 상품 Service

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);
    }
}

 

 

반응형
Comments