Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_weekendowa_20221008
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_weekendowa_20221008
Commits
1fe4907e
Commit
1fe4907e
authored
Nov 26, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Utworzenie *klasy* ProductRepository
parent
f8ab0469
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
127 additions
and
16 deletions
+127
-16
ProductController_v4.java
...alternatywne_wersje_bazy_danych/ProductController_v4.java
+1
-1
ProductController_v5.java
...alternatywne_wersje_bazy_danych/ProductController_v5.java
+47
-0
ProductRepository_v5.java
...alternatywne_wersje_bazy_danych/ProductRepository_v5.java
+34
-0
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+9
-14
ProductRepository.java
...ing/src/main/java/sklep/repository/ProductRepository.java
+34
-0
index.jsp
PC30-SklepSpring/src/main/webapp/WEB-INF/templates/index.jsp
+1
-0
products.jsp
...klepSpring/src/main/webapp/WEB-INF/templates/products.jsp
+1
-1
No files found.
PC30-SklepSpring/src/main/java/sklep/alternatywne_wersje_bazy_danych/ProductController_v4.java
View file @
1fe4907e
...
@@ -16,7 +16,7 @@ import org.springframework.web.bind.annotation.RequestParam;
...
@@ -16,7 +16,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import
sklep.model.Product
;
import
sklep.model.Product
;
@Controller
@Controller
@RequestMapping
(
path
=
"/alt4"
,
produces
=
"text/plain
"
)
@RequestMapping
(
"/alt4
"
)
public
class
ProductController_v4
{
public
class
ProductController_v4
{
@Autowired
@Autowired
private
EntityManager
em
;
private
EntityManager
em
;
...
...
PC30-SklepSpring/src/main/java/sklep/alternatywne_wersje_bazy_danych/ProductController_v5.java
0 → 100644
View file @
1fe4907e
package
sklep
.
alternatywne_wersje_bazy_danych
;
import
java.util.List
;
import
java.util.Optional
;
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
org.springframework.web.bind.annotation.RequestParam
;
import
sklep.model.Product
;
@Controller
@RequestMapping
(
"/alt5"
)
public
class
ProductController_v5
{
@Autowired
private
ProductRepository_v5
productRepository
;
@GetMapping
public
String
wszystkieProdukty
(
Model
model
)
{
List
<
Product
>
products
=
productRepository
.
findAll
();
model
.
addAttribute
(
"products"
,
products
);
return
"products"
;
}
@GetMapping
(
"/{id}"
)
public
String
jedenProdukt
(
@PathVariable
int
id
,
Model
model
)
{
Optional
<
Product
>
product
=
productRepository
.
findById
(
id
);
if
(
product
.
isPresent
())
{
model
.
addAttribute
(
"product"
,
product
.
get
());
return
"product"
;
}
else
{
model
.
addAttribute
(
"product_id"
,
id
);
return
"missing_product"
;
}
}
@GetMapping
(
"/szukaj"
)
public
String
wyszukajProdukty
(
@RequestParam
String
name
,
Model
model
)
{
List
<
Product
>
products
=
productRepository
.
findByProductName
(
name
);
model
.
addAttribute
(
"products"
,
products
);
return
"products"
;
}
}
PC30-SklepSpring/src/main/java/sklep/alternatywne_wersje_bazy_danych/ProductRepository_v5.java
0 → 100644
View file @
1fe4907e
package
sklep
.
alternatywne_wersje_bazy_danych
;
import
java.util.List
;
import
java.util.Optional
;
import
javax.persistence.EntityManager
;
import
javax.persistence.TypedQuery
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Repository
;
import
sklep.model.Product
;
@Repository
public
class
ProductRepository_v5
{
@Autowired
private
EntityManager
em
;
public
List
<
Product
>
findAll
()
{
TypedQuery
<
Product
>
query
=
em
.
createNamedQuery
(
"Product.findAll"
,
Product
.
class
);
return
query
.
getResultList
();
}
public
Optional
<
Product
>
findById
(
int
productId
)
{
return
Optional
.
ofNullable
(
em
.
find
(
Product
.
class
,
productId
));
}
public
List
<
Product
>
findByProductName
(
String
name
)
{
final
String
sql
=
"SELECT p FROM Product p WHERE p.productName = :name"
;
TypedQuery
<
Product
>
query
=
em
.
createQuery
(
sql
,
Product
.
class
);
query
.
setParameter
(
"name"
,
name
);
return
query
.
getResultList
();
}
}
PC30-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
1fe4907e
package
sklep
.
controller
;
package
sklep
.
controller
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Optional
;
import
javax.persistence.EntityManager
;
import
javax.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
;
...
@@ -14,26 +12,26 @@ import org.springframework.web.bind.annotation.RequestMapping;
...
@@ -14,26 +12,26 @@ import org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
sklep.model.Product
;
import
sklep.model.Product
;
import
sklep.repository.ProductRepository
;
@Controller
@Controller
@RequestMapping
(
path
=
"/products"
,
produces
=
"text/plain
"
)
@RequestMapping
(
"/products
"
)
public
class
ProductController
{
public
class
ProductController
{
@Autowired
@Autowired
private
EntityManager
em
;
private
ProductRepository
productRepository
;
@GetMapping
@GetMapping
public
String
wszystkieProdukty
(
Model
model
)
{
public
String
wszystkieProdukty
(
Model
model
)
{
TypedQuery
<
Product
>
query
=
em
.
createNamedQuery
(
"Product.findAll"
,
Product
.
class
);
List
<
Product
>
products
=
productRepository
.
findAll
();
List
<
Product
>
products
=
query
.
getResultList
();
model
.
addAttribute
(
"products"
,
products
);
model
.
addAttribute
(
"products"
,
products
);
return
"products"
;
return
"products"
;
}
}
@GetMapping
(
"/{id}"
)
@GetMapping
(
"/{id}"
)
public
String
jedenProdukt
(
@PathVariable
int
id
,
Model
model
)
{
public
String
jedenProdukt
(
@PathVariable
int
id
,
Model
model
)
{
Product
product
=
em
.
find
(
Product
.
class
,
id
);
Optional
<
Product
>
product
=
productRepository
.
findById
(
id
);
if
(
product
!=
null
)
{
if
(
product
.
isPresent
()
)
{
model
.
addAttribute
(
"product"
,
product
);
model
.
addAttribute
(
"product"
,
product
.
get
()
);
return
"product"
;
return
"product"
;
}
else
{
}
else
{
model
.
addAttribute
(
"product_id"
,
id
);
model
.
addAttribute
(
"product_id"
,
id
);
...
@@ -43,10 +41,7 @@ public class ProductController {
...
@@ -43,10 +41,7 @@ public class ProductController {
@GetMapping
(
"/szukaj"
)
@GetMapping
(
"/szukaj"
)
public
String
wyszukajProdukty
(
@RequestParam
String
name
,
Model
model
)
{
public
String
wyszukajProdukty
(
@RequestParam
String
name
,
Model
model
)
{
final
String
sql
=
"SELECT p FROM Product p WHERE p.productName = :name"
;
List
<
Product
>
products
=
productRepository
.
findByProductName
(
name
);
TypedQuery
<
Product
>
query
=
em
.
createQuery
(
sql
,
Product
.
class
);
query
.
setParameter
(
"name"
,
name
);
List
<
Product
>
products
=
query
.
getResultList
();
model
.
addAttribute
(
"products"
,
products
);
model
.
addAttribute
(
"products"
,
products
);
return
"products"
;
return
"products"
;
}
}
...
...
PC30-SklepSpring/src/main/java/sklep/repository/ProductRepository.java
0 → 100644
View file @
1fe4907e
package
sklep
.
repository
;
import
java.util.List
;
import
java.util.Optional
;
import
javax.persistence.EntityManager
;
import
javax.persistence.TypedQuery
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Repository
;
import
sklep.model.Product
;
@Repository
public
class
ProductRepository
{
@Autowired
private
EntityManager
em
;
public
List
<
Product
>
findAll
()
{
TypedQuery
<
Product
>
query
=
em
.
createNamedQuery
(
"Product.findAll"
,
Product
.
class
);
return
query
.
getResultList
();
}
public
Optional
<
Product
>
findById
(
int
productId
)
{
return
Optional
.
ofNullable
(
em
.
find
(
Product
.
class
,
productId
));
}
public
List
<
Product
>
findByProductName
(
String
name
)
{
final
String
sql
=
"SELECT p FROM Product p WHERE p.productName = :name"
;
TypedQuery
<
Product
>
query
=
em
.
createQuery
(
sql
,
Product
.
class
);
query
.
setParameter
(
"name"
,
name
);
return
query
.
getResultList
();
}
}
PC30-SklepSpring/src/main/webapp/WEB-INF/templates/index.jsp
View file @
1fe4907e
...
@@ -29,6 +29,7 @@
...
@@ -29,6 +29,7 @@
<li><a
href=
"/products/1"
>
products/1
</a>
- jeden produkt
</li>
<li><a
href=
"/products/1"
>
products/1
</a>
- jeden produkt
</li>
<li><a
href=
"/products/9"
>
products/9
</a>
- nieistniejący produkt
</li>
<li><a
href=
"/products/9"
>
products/9
</a>
- nieistniejący produkt
</li>
<li><a
href=
"/products/szukaj"
>
wyszukiwarka
</a></li>
<li><a
href=
"/products/szukaj"
>
wyszukiwarka
</a></li>
<li><a
href=
"/products/szukaj?name=pralka"
>
wyszukiwarka/pralka
</a></li>
<li><a
href=
"/products/new"
>
nowy produkt
</a></li>
<li><a
href=
"/products/new"
>
nowy produkt
</a></li>
<li><a
href=
"/products/1/edit"
>
edycja produktu
</a></li>
<li><a
href=
"/products/1/edit"
>
edycja produktu
</a></li>
</ul>
</ul>
...
...
PC30-SklepSpring/src/main/webapp/WEB-INF/templates/products.jsp
View file @
1fe4907e
...
@@ -26,7 +26,7 @@
...
@@ -26,7 +26,7 @@
<c:forEach
var=
"product"
items=
"${products}"
>
<c:forEach
var=
"product"
items=
"${products}"
>
<div
class=
"product"
>
<div
class=
"product"
>
<img
class=
"photo"
src=
"/products/${product.productId}/photo"
alt=
""
/>
<img
class=
"photo"
src=
"/products/${product.productId}/photo"
alt=
""
/>
<p>
Towar
<a
href=
"products/${product.productId}"
class=
"product-name"
>
${product.productName}
</a></p>
<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>
Cena:
<span
class=
"product-price"
>
${product.price}
</span></p>
<p
class=
"product-description"
>
${product.description}
</p>
<p
class=
"product-description"
>
${product.description}
</p>
<div
class=
"action"
><a
href=
"/products/${product.productId}/edit"
>
Edytuj
</a></div>
<div
class=
"action"
><a
href=
"/products/${product.productId}/edit"
>
Edytuj
</a></div>
...
...
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