Commit 2bef7e90 by Patryk Czarnik

ProductController jako zwykły @Controller

parent bdeb79cb
package sklep.rest;
import java.util.List;
import java.util.Optional;
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.ResponseBody;
import org.springframework.web.server.ResponseStatusException;
import sklep.model.Product;
import sklep.repository.ProductRepository;
@Controller
public class ProductController {
@Autowired
private ProductRepository productRepository;
@GetMapping("/products")
@ResponseBody
public List<Product> readAll() {
return productRepository.findAll();
}
@GetMapping("/products/{id}")
@ResponseBody
public Product readOne(@PathVariable("id") int productId) {
Optional<Product> product = productRepository.findById(productId);
if(product.isPresent()) {
return product.get();
} else {
throw new ResponseStatusException(HttpStatus.NOT_FOUND); // 404
}
}
}
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