Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20240528-BJava
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
20240528-BJava
Commits
6b336779
Commit
6b336779
authored
Jun 27, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Wyszukiwarka - pełna wersja
parent
aecddd39
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
88 additions
and
5 deletions
+88
-5
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+26
-4
ProductRepository.java
...ing/src/main/java/sklep/repository/ProductRepository.java
+26
-1
wyszukiwarka2.jsp
...pring/src/main/webapp/WEB-INF/templates/wyszukiwarka2.jsp
+36
-0
No files found.
PC33-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
6b336779
...
@@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.RequestParam;
...
@@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import
sklep.model.Product
;
import
sklep.model.Product
;
import
sklep.repository.ProductRepository
;
import
sklep.repository.ProductRepository
;
import
java.math.BigDecimal
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Optional
;
import
java.util.Optional
;
...
@@ -40,10 +41,31 @@ public class ProductController {
...
@@ -40,10 +41,31 @@ public class ProductController {
}
}
@GetMapping
(
"/szukaj"
)
@GetMapping
(
"/szukaj"
)
public
String
wyszukiwarka
(
Model
model
,
public
String
szukaj
(
Model
model
,
@RequestParam
(
"name"
)
String
productName
)
{
@RequestParam
(
required
=
false
)
String
name
,
List
<
Product
>
products
=
productRepository
.
findByProductName
(
productName
);
@RequestParam
(
required
=
false
)
BigDecimal
min
,
@RequestParam
(
required
=
false
)
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
())
{
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
);
model
.
addAttribute
(
"products"
,
products
);
return
"wyszukiwarka
1
"
;
return
"wyszukiwarka
2
"
;
}
}
}
}
PC33-SklepSpring/src/main/java/sklep/repository/ProductRepository.java
View file @
6b336779
...
@@ -3,9 +3,34 @@ package sklep.repository;
...
@@ -3,9 +3,34 @@ package sklep.repository;
import
org.springframework.data.jpa.repository.JpaRepository
;
import
org.springframework.data.jpa.repository.JpaRepository
;
import
sklep.model.Product
;
import
sklep.model.Product
;
import
java.math.BigDecimal
;
import
java.util.List
;
import
java.util.List
;
// Rozszerzamy interfejs JpaRepository określając typ rekordu oraz typ klucza
/* 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
>
{
public
interface
ProductRepository
extends
JpaRepository
<
Product
,
Integer
>
{
List
<
Product
>
findByProductName
(
String
productName
);
List
<
Product
>
findByProductName
(
String
productName
);
List
<
Product
>
findByProductNameContainsIgnoringCase
(
String
name
);
List
<
Product
>
findByPriceBetween
(
BigDecimal
min
,
BigDecimal
max
);
List
<
Product
>
findByProductNameContainingIgnoringCaseAndPriceBetween
(
String
name
,
BigDecimal
min
,
BigDecimal
max
);
}
}
PC33-SklepSpring/src/main/webapp/WEB-INF/templates/wyszukiwarka2.jsp
0 → 100644
View file @
6b336779
<
%@
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.id}/photo"
alt=
""
/>
<p>
Towar
<a
href=
"/products/${product.id}"
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>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment