Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230617
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
javab_20230617
Commits
f426f4ad
Commit
f426f4ad
authored
Jul 29, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Wyszukiwarka w oparciu o EntityManager
parent
9504c4be
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
80 additions
and
0 deletions
+80
-0
ProductController_v3.java
...ep/alternatywne_dostepy_do_bazy/ProductController_v3.java
+10
-0
ProductController_v7.java
...ep/alternatywne_dostepy_do_bazy/ProductController_v7.java
+70
-0
No files found.
PC27-SklepSpring/src/main/java/sklep/alternatywne_dostepy_do_bazy/ProductController_v3.java
View file @
f426f4ad
package
sklep
.
alternatywne_dostepy_do_bazy
;
package
sklep
.
alternatywne_dostepy_do_bazy
;
import
jakarta.persistence.EntityManager
;
import
jakarta.persistence.EntityManager
;
import
jakarta.persistence.TypedQuery
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.ui.Model
;
import
org.springframework.ui.Model
;
...
@@ -35,4 +36,13 @@ public class ProductController_v3 {
...
@@ -35,4 +36,13 @@ public class ProductController_v3 {
return
"missing_product"
;
return
"missing_product"
;
}
}
}
}
@GetMapping
(
"/szukaj"
)
public
String
szukaj
(
String
name
,
Model
model
)
{
TypedQuery
<
Product
>
query
=
em
.
createQuery
(
"SELECT p FROM Product p WHERE productName = :nazwa"
,
Product
.
class
);
query
.
setParameter
(
"nazwa"
,
name
);
List
<
Product
>
products
=
query
.
getResultList
();
model
.
addAttribute
(
"products"
,
products
);
return
"wyszukiwarka"
;
}
}
}
PC27-SklepSpring/src/main/java/sklep/alternatywne_dostepy_do_bazy/ProductController_v7.java
0 → 100644
View file @
f426f4ad
package
sklep
.
alternatywne_dostepy_do_bazy
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Controller
;
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
sklep.model.Product
;
import
sklep.repository.ProductRepository
;
import
java.math.BigDecimal
;
import
java.util.List
;
import
java.util.Optional
;
@Controller
@RequestMapping
(
"/alt7/products"
)
public
class
ProductController_v7
{
@Autowired
private
ProductRepository
productRepository
;
@GetMapping
public
String
readAll
(
Model
model
)
{
List
<
Product
>
products
=
productRepository
.
findAll
();
model
.
addAttribute
(
"products"
,
products
);
return
"products"
;
}
@GetMapping
(
"/{id}"
)
public
String
readOne
(
@PathVariable
(
"id"
)
Integer
productId
,
Model
model
)
{
Optional
<
Product
>
product
=
productRepository
.
findById
(
productId
);
if
(
product
.
isPresent
())
{
model
.
addAttribute
(
"product"
,
product
.
get
());
return
"product"
;
}
else
{
return
"missing_product"
;
}
}
@GetMapping
(
"/szukaj"
)
public
String
szukaj
(
Model
model
,
String
name
,
BigDecimal
min
,
BigDecimal
max
)
{
List
<
Product
>
products
=
List
.
of
();
if
(
name
!=
null
&&
!
name
.
isEmpty
()
&&
min
==
null
&&
max
==
null
)
{
products
=
productRepository
.
findByProductNameContainingIgnoreCase
(
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"
;
}
}
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