package sklep.rest;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.server.ResponseStatusException;

import sklep.model.Product;
import sklep.repository.ProductRepository;

@Controller
@RequestMapping("/rest/products")
public class ProductEndpoint {
    @Autowired
    private ProductRepository productRepository;
    
    @GetMapping
    @ResponseBody
    public List<Product> readAll() {
        return productRepository.findAll();
    }
    
    @GetMapping("/{id}")
    @ResponseBody
    public Product readOne(@PathVariable Integer id) {
        return productRepository.findById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
    }

}
