Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230928
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_20230928
Commits
e9c2002b
Commit
e9c2002b
authored
Nov 07, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
addToBasket - wersja z parametrem HttpSession i leniwą inicjalizacją
parent
4b33454c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
2 deletions
+21
-2
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+21
-2
No files found.
PC30-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
e9c2002b
...
@@ -15,7 +15,9 @@ import org.springframework.web.bind.annotation.PostMapping;
...
@@ -15,7 +15,9 @@ import org.springframework.web.bind.annotation.PostMapping;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
jakarta.servlet.http.HttpSession
;
import
jakarta.validation.Valid
;
import
jakarta.validation.Valid
;
import
sklep.basket.Basket
;
import
sklep.model.Product
;
import
sklep.model.Product
;
import
sklep.repository.ProductRepository
;
import
sklep.repository.ProductRepository
;
import
sklep.util.PhotoUtil
;
import
sklep.util.PhotoUtil
;
...
@@ -108,7 +110,7 @@ public class ProductController {
...
@@ -108,7 +110,7 @@ public class ProductController {
// Taki parametr od razu staje się częścią modelu (to jest tzw. ModelAttribute)
// Taki parametr od razu staje się częścią modelu (to jest tzw. ModelAttribute)
if
(
bindingResult
.
hasErrors
())
{
if
(
bindingResult
.
hasErrors
())
{
System
.
out
.
println
(
"Są błedy: "
+
bindingResult
.
getAllErrors
());
System
.
err
.
println
(
"Są błedy: "
+
bindingResult
.
getAllErrors
());
// normalnie wyświetlilibyśmy coś na stronie...
// normalnie wyświetlilibyśmy coś na stronie...
// ale robi to za nas tag f:form i f:errors
// ale robi to za nas tag f:form i f:errors
return
"product_form"
;
return
"product_form"
;
...
@@ -119,8 +121,25 @@ public class ProductController {
...
@@ -119,8 +121,25 @@ public class ProductController {
System
.
out
.
println
(
"id po zapisie: "
+
product
.
getProductId
());
System
.
out
.
println
(
"id po zapisie: "
+
product
.
getProductId
());
return
"redirect:/products"
;
return
"redirect:/products"
;
}
}
}
}
@GetMapping
(
"/{id}/add-to-basket"
)
public
String
addToBasket
(
@PathVariable
(
"id"
)
Integer
productId
,
HttpSession
sesja
)
{
Optional
<
Product
>
product
=
productRepository
.
findById
(
productId
);
if
(
product
.
isPresent
())
{
// w tej wersji stosujemy leniwą inicjalizację
Basket
basket
=
(
Basket
)
sesja
.
getAttribute
(
"basket"
);
if
(
basket
==
null
)
{
basket
=
new
Basket
();
sesja
.
setAttribute
(
"basket"
,
basket
);
}
basket
.
addProduct
(
product
.
get
());
}
else
{
System
.
err
.
println
(
"Nieznany produkt dodawany do koszyka: "
+
productId
);
}
return
"redirect:/products"
;
}
@GetMapping
(
path
=
"/{id}/photo"
,
produces
=
"image/jpeg"
)
@GetMapping
(
path
=
"/{id}/photo"
,
produces
=
"image/jpeg"
)
@ResponseBody
@ResponseBody
...
...
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