김소연/spring/product/src/main/java/com/example/product/entity/service/jpa/productBoard/ProductServiceimpl.java
package com.example.product.entity.service.jpa.productBoard;

import com.example.product.controller.jpa.productBoard.request.ProductRequest;
import com.example.product.entity.jpa.productBoard.Product;
import com.example.product.entity.repository.productBoard.ProductRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class ProductServiceimpl implements ProductService {

    @Autowired
    ProductRepository repository;

    @Override
    public void register(ProductRequest productRequest) {
        Product product = new Product();
        product.setProductName(productRequest.getProductName());
        product.setProductDescription(productRequest.getProductDescription());
        product.setPrice(productRequest.getPrice());
        product.setSeller(productRequest.getSeller());

        repository.save(product);
    }

    @Override
    public List<Product> list() {
        return repository.findAll(Sort.by(Sort.Direction.DESC,"productNo"));
    }

    @Override
    public Product read(Long productNo) {

        Optional<Product> maybeProduct = repository.findById(productNo);

        if (maybeProduct.equals(Optional.empty())) {
            log.info("Can't read product info!!!");
            return null;
        }

        return maybeProduct.get();
    }

    @Override
    public void modify(Product product) {
        repository.save(product);
    }

    @Override
    public void remove(Long productNo) {
        repository.deleteById(productNo);
    }
}
