Commit 38b3d5e9 by Patryk Czarnik

wyszukiwarka2

parent bccbdb8c
package sklep.controller; package sklep.controller;
import java.math.BigDecimal;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
...@@ -38,10 +39,31 @@ public class ProductController { ...@@ -38,10 +39,31 @@ public class ProductController {
} }
@GetMapping("/products/szukaj") @GetMapping("/products/szukaj")
public String szukaj(Model model, String name) { public String szukaj(Model model,
List<Product> products = productRepository.findByProductName(name); String name,
model.addAttribute("products", products); BigDecimal min,
return "wyszukiwarka"; BigDecimal max) {
List<Product> products = List.of();
if(name != null && !name.isEmpty() && min == null && max == null) {
products = productRepository.findByProductNameContainsIgnoringCase(name);
} else if ((name == null || name.isEmpty()) && (min != null || max != null)) {
if(min == null) {
min = BigDecimal.ZERO;
}
if(max == null) {
max = BigDecimal.valueOf(1000_000_000);
}
products = productRepository.findByPriceBetween(min, max);
} else if (name != null && !name.isEmpty() && (min != null || max != null)) {
if(min == null) {
min = BigDecimal.ZERO;
}
if(max == null) {
max = BigDecimal.valueOf(1000_000_000);
}
products = productRepository.findByProductNameContainingIgnoringCaseAndPriceBetween(name, min, max);
}
model.addAttribute("products", products);
return "wyszukiwarka2";
} }
} }
package sklep.repository; package sklep.repository;
import java.math.BigDecimal;
import java.util.List; import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import sklep.model.Product; import sklep.model.Product;
/* Gdy w projekcie umieścimy interfejs rozszerzający interfejs JpaRepository (albo podobny)
* i oznaczymy go adnotacją @Repository (albo skonfigurujemy w inny sposób...),
* to Spring AUTOMATYCZNIE UTWORZY IMPLEMENTACJĘ tego interfejsu.
* Dzięki temu "za darmo" mamy metody dający podstawowy dostęp do tabel.
* Dodatkowo w interfejsie można dopisać własne metody, w których nazwie kryje się zasada działania.
* Np. findByPriceBetween szuka produktów o cenie między min i max.
*
* findByEmail - szuka rekordów z polem email równym parametrowi.
*
* przejście do innych tabel / encji:
* List<Employee> findByDepartment_Location_City(String city)
* jakby w Javie sprawdzić: employee.getDepartment().getLocation.getCity().equals(city)
*
* https://www.baeldung.com/spring-data-derived-queries
*/
public interface ProductRepository extends JpaRepository<Product, Integer> { public interface ProductRepository extends JpaRepository<Product, Integer> {
List<Product> findByProductName(String name); List<Product> findByProductName(String name);
List<Product> findByProductNameContainsIgnoringCase(String name);
List<Product> findByPriceBetween(BigDecimal min, BigDecimal max);
List<Product> findByProductNameContainingIgnoringCaseAndPriceBetween(String name, BigDecimal min, BigDecimal max);
} }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Katalog towarów</title>
<link rel="stylesheet" type="text/css" href="/styl.css"/>
</head>
<body>
<div><a href="/">Powrót do spisu treści</a></div>
<h1>Wyszukiwarka produktów</h1>
<form method="get" class="wyszukiwarka">
<table>
<tr><td><label for="name">Podaj nazwę:</label></td>
<td><input type="text" id="name" name="name" value="${param.name}"></td></tr>
<tr><td><label for="min">Cena minimalna:</label></td>
<td><input type="number" id="min" name="min" value="${param.min}"></td></tr>
<tr><td><label for="max">Cena maksymalna:</label></td>
<td><input type="number" id="max" name="max" value="${param.max}"></td></tr>
<tr><td><button>Szukaj</button></td></tr>
</table>
</form>
<c:forEach var="product" items="${products}">
<div class="product">
<img class="photo" src="/products/${product.productId}/photo" alt=""/>
<p>Towar <a href="/products/${product.productId}" class="product-name">${product.productName}</a></p>
<p>Cena: <span class="product-price">${product.price}</span></p>
<p class="product-description">${product.description}</p>
</div>
</c:forEach>
</body>
</html>
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