Commit ca963626 by Patryk Czarnik

wyszukiwarka - gotowa

parent 454ab788
......@@ -30,7 +30,27 @@ public class ProductController {
@GetMapping("/szukaj")
public String findProducts(Model model, String name, BigDecimal min, BigDecimal max) {
List<Product> products = productRepository.findByProductName(name);
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()) {
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 "wyszukiwarka";
}
......
......@@ -13,7 +13,6 @@ import java.util.Set;
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@ColumnDefault("nextval('orders_seq'::regclass)")
@Column(name = "order_id", nullable = false)
private Integer id;
......
......@@ -11,7 +11,6 @@ import java.math.BigDecimal;
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@ColumnDefault("nextval('products_seq'::regclass)")
@Column(name = "product_id", nullable = false)
private Integer id;
......
package sklep.repository;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import sklep.model.Product;
/* Gdy w projekcie umieścimy interfejs rozszerzający interfejs JpaRepository (albo podobny)
* 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
*
* Można też do dowolnej metody dopisać adnotację @Query i wtedy wykona się podane przez nas zapytanie
*/
public interface ProductRepository extends JpaRepository<Product, Integer> {
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);
List<Product> findByProductNameContainingIgnoreCase(String name);
}
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