Commit 5bffc4d1 by Patryk Czarnik

alternatywne produkty - by_price

parent 33db460e
package sklep.controller;
import java.math.BigDecimal;
import java.util.List;
import javax.persistence.EntityManager;
......@@ -11,6 +12,7 @@ import org.springframework.ui.Model;
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.RequestParam;
import sklep.model.Product;
......@@ -35,4 +37,17 @@ public class AlternatywneProdukty2 {
return "product";
}
@GetMapping("/by_price")
public String byPrice(Model model,
@RequestParam(defaultValue = "0") BigDecimal min,
@RequestParam(defaultValue = "999999999") BigDecimal max) {
String ql = "SELECT p FROM Product p WHERE p.price BETWEEN :min AND :max ORDER BY p.productId";
TypedQuery<Product> query = em.createQuery(ql, Product.class);
query.setParameter("min", min);
query.setParameter("max", max);
List<Product> products = query.getResultList();
model.addAttribute("products", products);
return "products";
}
}
......@@ -45,7 +45,7 @@
<li><a href="/alt1/products">Dostęp JDBC</a> (wstrzykiwanie DataSource)</li>
<li><a href="/alt2/products">Dostęp Hibernate/JPA</a> (wstrzykiwanie EntityManager)</li>
<li><a href="/alt2/products/1">jeden produkt</a></li>
<li><a href="/alt2/products/by_price?min=1000&max=2900">by price</a></li>
<li><a href="/alt2/by_price?min=1000&max=2900">by price</a></li>
</ul>
</body>
......
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