Commit 5227d823 by Patryk Czarnik

Wspólny adres /products na poziimie klasy i inne poprawki

parent 6321149f
...@@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.GetMapping; ...@@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute; import org.springframework.web.bind.annotation.SessionAttribute;
...@@ -24,6 +25,7 @@ import sklep.repository.ProductRepository; ...@@ -24,6 +25,7 @@ import sklep.repository.ProductRepository;
import sklep.utils.ExceptionUtils; import sklep.utils.ExceptionUtils;
@Controller @Controller
@RequestMapping("/products")
public class ProductController { public class ProductController {
@Autowired @Autowired
private ProductRepository productRepository; private ProductRepository productRepository;
...@@ -31,7 +33,7 @@ public class ProductController { ...@@ -31,7 +33,7 @@ public class ProductController {
@Autowired @Autowired
private PhotoUtil photoUtil; private PhotoUtil photoUtil;
@GetMapping("/products") @GetMapping
public String readAllProducts(Model model) { public String readAllProducts(Model model) {
List<Product> products = productRepository.findAll(Sort.by("productId")); List<Product> products = productRepository.findAll(Sort.by("productId"));
model.addAttribute("products", products); model.addAttribute("products", products);
...@@ -39,7 +41,7 @@ public class ProductController { ...@@ -39,7 +41,7 @@ public class ProductController {
return "products"; return "products";
} }
@GetMapping("/products/{id}") @GetMapping("/{id}")
public String readOneProduct(Model model, @PathVariable("id") int productId) { public String readOneProduct(Model model, @PathVariable("id") int productId) {
Optional<Product> product = productRepository.findById(productId); Optional<Product> product = productRepository.findById(productId);
if(product.isPresent()) { if(product.isPresent()) {
...@@ -51,12 +53,12 @@ public class ProductController { ...@@ -51,12 +53,12 @@ public class ProductController {
} }
} }
@GetMapping("/products/new") @GetMapping("/new")
public String newProduct(Product product) { public String newProduct(Product product) {
return "product_form"; return "product_form";
} }
@GetMapping("/products/{id}/edit") @GetMapping("/{id}/edit")
public String editProduct(Model model, @PathVariable("id") int productId) { public String editProduct(Model model, @PathVariable("id") int productId) {
Optional<Product> product = productRepository.findById(productId); Optional<Product> product = productRepository.findById(productId);
if(product.isPresent()) { if(product.isPresent()) {
...@@ -68,7 +70,7 @@ public class ProductController { ...@@ -68,7 +70,7 @@ public class ProductController {
} }
} }
@PostMapping({"/products/new", "/products/{id}/edit"}) @PostMapping({"/new", "/{id}/edit"})
public String saveProduct( public String saveProduct(
@Valid Product product, @Valid Product product,
BindingResult bindingResult, BindingResult bindingResult,
...@@ -104,7 +106,7 @@ public class ProductController { ...@@ -104,7 +106,7 @@ public class ProductController {
} }
} }
@GetMapping("/products/{id}/add-to-basket") @GetMapping("/{id}/add-to-basket")
public String addToBasket( public String addToBasket(
@SessionAttribute Basket basket, @SessionAttribute Basket basket,
@PathVariable("id") int productId, @PathVariable("id") int productId,
...@@ -116,7 +118,7 @@ public class ProductController { ...@@ -116,7 +118,7 @@ public class ProductController {
return "redirect:" + adresPowrotu; return "redirect:" + adresPowrotu;
} }
@GetMapping(path="/products/{id}/photo", produces="image/jpeg") @GetMapping(path="/{id}/photo", produces="image/jpeg")
@ResponseBody @ResponseBody
public byte[] readPhoto(@PathVariable("id") int productId) { public byte[] readPhoto(@PathVariable("id") int productId) {
return photoUtil.readBytes(productId); return photoUtil.readBytes(productId);
......
...@@ -36,14 +36,14 @@ import javax.validation.constraints.Size; ...@@ -36,14 +36,14 @@ import javax.validation.constraints.Size;
public class Product implements Serializable { public class Product implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
// W celu obserwacji powstawania i usuwania obiektów: // W celu obserwacji powstawania i usuwania obiektów:
{ System.out.println("!new Product!"); } // { System.out.println("!new Product!"); }
@Override // @Override
protected void finalize() { // protected void finalize() {
System.out.println("Ginie Produkt nr " + productId); // System.out.println("Ginie Produkt nr " + productId);
} // }
// używanie finalize tak, że miałoby wpływać na działanie, jest niezalecane! // Używanie finalize tak, że miałoby wpływać na działanie, jest niezalecane!
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
...@@ -55,12 +55,12 @@ public class Product implements Serializable { ...@@ -55,12 +55,12 @@ public class Product implements Serializable {
@NotNull @NotNull
@DecimalMin("0.01") @DecimalMin("0.01")
@DecimalMax("9999.99") // tylko dla sprawdzenia działania → docelowo wartość ma być większa @DecimalMax("99999999.99")
private BigDecimal price; private BigDecimal price;
@Column(name = "product_name") @Column(name = "product_name")
@NotNull @NotNull
@Size(min=2, max=10) // FIXME @Size(min=2, max=100)
private String productName; private String productName;
@DecimalMin("0.00") @DecimalMin("0.00")
......
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