Commit b34bf391 by Patryk Czarnik

@SessionAttribute

parent a8285ec0
...@@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.PathVariable; ...@@ -14,6 +14,7 @@ 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.RequestMapping; 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 jakarta.servlet.http.HttpSession; import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid; import jakarta.validation.Valid;
...@@ -125,11 +126,9 @@ public class ProductController { ...@@ -125,11 +126,9 @@ public class ProductController {
@GetMapping("/{id}/add-to-basket") @GetMapping("/{id}/add-to-basket")
public String addToBasket(@PathVariable("id") Integer productId, public String addToBasket(@PathVariable("id") Integer productId,
HttpSession sesja) { @SessionAttribute Basket basket) {
Optional<Product> product = productRepository.findById(productId); Optional<Product> product = productRepository.findById(productId);
if(product.isPresent()) { if(product.isPresent()) {
// w tej wersji zakładamy, że listener przygotował pusty koszyk
Basket basket = (Basket) sesja.getAttribute("basket");
basket.addProduct(product.get()); basket.addProduct(product.get());
} else { } else {
System.err.println("Nieznany produkt dodawany do koszyka: " + productId); System.err.println("Nieznany produkt dodawany do koszyka: " + productId);
......
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