content: package com.codesoom.demo.application;
  
  import com.codesoom.demo.errors.ProductNotFoundException;
  import com.codesoom.demo.domain.Product;
  import com.codesoom.demo.domain.ProductRepository;
  import com.codesoom.demo.dto.ProductData;
  import com.github.dozermapper.core.DozerBeanMapperBuilder;
  import com.github.dozermapper.core.Mapper;
  import org.springframework.stereotype.Service;
  
  import javax.transaction.Transactional;
  import java.util.List;
  
  /**
   * Service for products.
   *
   * @author Ashal aka JOKER (ahastudio@gmail.com)
   */
  @Service
  @Transactional
  public class ProductService {
      private final Mapper mapper;
      private final ProductRepository productRepository;
  
      public ProductService(
              Mapper dozerMapper,
              ProductRepository productRepository
      ) {
          this.mapper = dozerMapper;
          this.productRepository = productRepository;
      }
  
      /**
       * Returns all products in this application.
       *
       * @return all products.
       */
      public List<Product> getProducts() {
          return productRepository.findAll();
      }
  
      /**
       * Returns the product with given ID.
       *
       * @param id is the identifier of the product.
       * @return the product with given ID.
       * @throws ProductNotFoundException in case any product doesn't exist
       * with the given ID.
       */
      public Product getProduct(Long id) {
          return findProduct(id);
      }
  
      public Product createProduct(ProductData productData) {
          Product product = mapper.map(productData, Product.class);
          return productRepository.save(product);
      }
  
      public Product updateProduct(Long id, ProductData productData) {
          Product product = findProduct(id);
  
          product.changeWith(mapper.map(productData, Product.class));
  
          return product;
      }
  
      public Product deleteProduct(Long id) {
          Product product = findProduct(id);
  
          productRepository.delete(product);
  
          return product;
      }
  
      private Product findProduct(Long id) {
          return productRepository.findById(id)
                  .orElseThrow(() -> new ProductNotFoundException(id));
      }
  }
  