Commit e2c4a390 by Patryk Czarnik

Dostęp RESTowy - początek

parent b68da23c
package sklep.rest;
import org.springframework.http.HttpStatusCode;
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.photo.PhotoUtil;
import sklep.repository.ProductRepository;
import java.util.List;
@Controller
@RequestMapping("/rest/products")
public class ProductEndpoint {
private ProductRepository productRepository;
private PhotoUtil photoUtil;
// Tutaj stosujemy wstrzykiwanie przez konstruktor.
public ProductEndpoint(ProductRepository productRepository, PhotoUtil photoUtil) {
this.productRepository = productRepository;
this.photoUtil = photoUtil;
}
@GetMapping
@ResponseBody
public List<Product> getProducts() {
return productRepository.findAll();
}
@GetMapping("/{id}")
@ResponseBody
public Product getProduct(@PathVariable int id) {
return productRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatusCode.valueOf(404),
"Brak produktu o numerze " + id));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment