Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_dzienna_15_09
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
java_dzienna_15_09
Commits
53a1f2b2
Commit
53a1f2b2
authored
Oct 04, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Dalsze wersje wyszukiwarki i więcej metod z magicznymi nazwami
parent
5298d2fd
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
6 deletions
+47
-6
Wyszukiwarka.java
...epSpring/src/main/java/sklep/controller/Wyszukiwarka.java
+27
-4
ProductRepository.java
...ing/src/main/java/sklep/repository/ProductRepository.java
+14
-0
wyszukiwarka.jsp
...Spring/src/main/webapp/WEB-INF/templates/wyszukiwarka.jsp
+6
-2
No files found.
PC30-SklepSpring/src/main/java/sklep/controller/Wyszukiwarka.java
View file @
53a1f2b2
package
sklep
.
controller
;
import
java.math.BigDecimal
;
import
java.util.List
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -16,11 +17,33 @@ public class Wyszukiwarka {
private
ProductRepository
productRepository
;
@GetMapping
(
"/wyszukiwarka"
)
public
String
readAllProducts
(
Model
model
,
String
name
)
{
List
<
Product
>
products
=
productRepository
.
findByProductName
(
name
);
public
String
wyszukiwarka
(
Model
model
,
String
name
,
BigDecimal
min
,
BigDecimal
max
)
{
List
<
Product
>
products
=
List
.
of
();
if
(
name
!=
null
&&
!
name
.
isEmpty
()
&&
min
==
null
&&
max
==
null
)
{
products
=
productRepository
.
findByProductNameContainingIgnoringCase
(
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
"wyszukiwarka"
;
}
}
PC30-SklepSpring/src/main/java/sklep/repository/ProductRepository.java
View file @
53a1f2b2
package
sklep
.
repository
;
import
java.math.BigDecimal
;
import
java.util.List
;
import
org.springframework.data.jpa.repository.JpaRepository
;
...
...
@@ -11,6 +12,11 @@ import sklep.model.Product;
* 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.
*/
@Repository
...
...
@@ -18,4 +24,12 @@ public interface ProductRepository extends JpaRepository<Product, Integer> {
List
<
Product
>
findByProductName
(
String
name
);
List
<
Product
>
findByProductNameContains
(
String
name
);
List
<
Product
>
findByProductNameContainingIgnoringCase
(
String
name
);
List
<
Product
>
findByPriceBetween
(
BigDecimal
min
,
BigDecimal
max
);
List
<
Product
>
findByProductNameContainingIgnoringCaseAndPriceBetween
(
String
name
,
BigDecimal
min
,
BigDecimal
max
);
}
PC30-SklepSpring/src/main/webapp/WEB-INF/templates/wyszukiwarka.jsp
View file @
53a1f2b2
...
...
@@ -4,7 +4,7 @@
<html>
<head>
<meta
charset=
"UTF-8"
>
<title>
Wyszukiwarka
towarów
</title>
<title>
Katalog
towarów
</title>
<link
rel=
"stylesheet"
type=
"text/css"
href=
"/styl.css"
/>
</head>
<body>
...
...
@@ -13,8 +13,12 @@
<form
method=
"get"
class=
"wyszukiwarka"
>
<table>
<tr><td><label
for=
"name"
>
Podaj
nazwę
:
</label></td>
<tr><td><label
for=
"name"
>
Podaj
fragment nazwy
:
</label></td>
<td><input
type=
"text"
name=
"name"
value=
"${param.name}"
></td></tr>
<tr><td><label
for=
"min"
>
Cena minimalna:
</label></td>
<td><input
type=
"number"
name=
"min"
value=
"${param.min}"
></td></tr>
<tr><td><label
for=
"max"
>
Cena maksymalna:
</label></td>
<td><input
type=
"number"
name=
"max"
value=
"${param.max}"
></td></tr>
<tr><td><button>
Szukaj
</button></td></tr>
</table>
</form>
...
...
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