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
5227d823
Commit
5227d823
authored
Oct 05, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Wspólny adres /products na poziimie klasy i inne poprawki
parent
6321149f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
16 deletions
+18
-16
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+9
-7
Product.java
PC30-SklepSpring/src/main/java/sklep/model/Product.java
+9
-9
No files found.
PC30-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
5227d823
...
@@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.GetMapping;
...
@@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestHeader
;
import
org.springframework.web.bind.annotation.RequestHeader
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.bind.annotation.SessionAttribute
;
import
org.springframework.web.bind.annotation.SessionAttribute
;
...
@@ -24,6 +25,7 @@ import sklep.repository.ProductRepository;
...
@@ -24,6 +25,7 @@ import sklep.repository.ProductRepository;
import
sklep.utils.ExceptionUtils
;
import
sklep.utils.ExceptionUtils
;
@Controller
@Controller
@RequestMapping
(
"/products"
)
public
class
ProductController
{
public
class
ProductController
{
@Autowired
@Autowired
private
ProductRepository
productRepository
;
private
ProductRepository
productRepository
;
...
@@ -31,7 +33,7 @@ public class ProductController {
...
@@ -31,7 +33,7 @@ public class ProductController {
@Autowired
@Autowired
private
PhotoUtil
photoUtil
;
private
PhotoUtil
photoUtil
;
@GetMapping
(
"/products"
)
@GetMapping
public
String
readAllProducts
(
Model
model
)
{
public
String
readAllProducts
(
Model
model
)
{
List
<
Product
>
products
=
productRepository
.
findAll
(
Sort
.
by
(
"productId"
));
List
<
Product
>
products
=
productRepository
.
findAll
(
Sort
.
by
(
"productId"
));
model
.
addAttribute
(
"products"
,
products
);
model
.
addAttribute
(
"products"
,
products
);
...
@@ -39,7 +41,7 @@ public class ProductController {
...
@@ -39,7 +41,7 @@ public class ProductController {
return
"products"
;
return
"products"
;
}
}
@GetMapping
(
"/
products/
{id}"
)
@GetMapping
(
"/{id}"
)
public
String
readOneProduct
(
Model
model
,
@PathVariable
(
"id"
)
int
productId
)
{
public
String
readOneProduct
(
Model
model
,
@PathVariable
(
"id"
)
int
productId
)
{
Optional
<
Product
>
product
=
productRepository
.
findById
(
productId
);
Optional
<
Product
>
product
=
productRepository
.
findById
(
productId
);
if
(
product
.
isPresent
())
{
if
(
product
.
isPresent
())
{
...
@@ -51,12 +53,12 @@ public class ProductController {
...
@@ -51,12 +53,12 @@ public class ProductController {
}
}
}
}
@GetMapping
(
"/
products/
new"
)
@GetMapping
(
"/new"
)
public
String
newProduct
(
Product
product
)
{
public
String
newProduct
(
Product
product
)
{
return
"product_form"
;
return
"product_form"
;
}
}
@GetMapping
(
"/
products/
{id}/edit"
)
@GetMapping
(
"/{id}/edit"
)
public
String
editProduct
(
Model
model
,
@PathVariable
(
"id"
)
int
productId
)
{
public
String
editProduct
(
Model
model
,
@PathVariable
(
"id"
)
int
productId
)
{
Optional
<
Product
>
product
=
productRepository
.
findById
(
productId
);
Optional
<
Product
>
product
=
productRepository
.
findById
(
productId
);
if
(
product
.
isPresent
())
{
if
(
product
.
isPresent
())
{
...
@@ -68,7 +70,7 @@ public class ProductController {
...
@@ -68,7 +70,7 @@ public class ProductController {
}
}
}
}
@PostMapping
({
"/
products/new"
,
"/products
/{id}/edit"
})
@PostMapping
({
"/
new"
,
"
/{id}/edit"
})
public
String
saveProduct
(
public
String
saveProduct
(
@Valid
Product
product
,
@Valid
Product
product
,
BindingResult
bindingResult
,
BindingResult
bindingResult
,
...
@@ -104,7 +106,7 @@ public class ProductController {
...
@@ -104,7 +106,7 @@ public class ProductController {
}
}
}
}
@GetMapping
(
"/
products/
{id}/add-to-basket"
)
@GetMapping
(
"/{id}/add-to-basket"
)
public
String
addToBasket
(
public
String
addToBasket
(
@SessionAttribute
Basket
basket
,
@SessionAttribute
Basket
basket
,
@PathVariable
(
"id"
)
int
productId
,
@PathVariable
(
"id"
)
int
productId
,
...
@@ -116,7 +118,7 @@ public class ProductController {
...
@@ -116,7 +118,7 @@ public class ProductController {
return
"redirect:"
+
adresPowrotu
;
return
"redirect:"
+
adresPowrotu
;
}
}
@GetMapping
(
path
=
"/
products/
{id}/photo"
,
produces
=
"image/jpeg"
)
@GetMapping
(
path
=
"/{id}/photo"
,
produces
=
"image/jpeg"
)
@ResponseBody
@ResponseBody
public
byte
[]
readPhoto
(
@PathVariable
(
"id"
)
int
productId
)
{
public
byte
[]
readPhoto
(
@PathVariable
(
"id"
)
int
productId
)
{
return
photoUtil
.
readBytes
(
productId
);
return
photoUtil
.
readBytes
(
productId
);
...
...
PC30-SklepSpring/src/main/java/sklep/model/Product.java
View file @
5227d823
...
@@ -36,14 +36,14 @@ import javax.validation.constraints.Size;
...
@@ -36,14 +36,14 @@ import javax.validation.constraints.Size;
public
class
Product
implements
Serializable
{
public
class
Product
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
private
static
final
long
serialVersionUID
=
1L
;
// W celu obserwacji powstawania i usuwania obiektów:
// W celu obserwacji powstawania i usuwania obiektów:
{
System
.
out
.
println
(
"!new Product!"
);
}
//
{ System.out.println("!new Product!"); }
@Override
//
@Override
protected
void
finalize
()
{
//
protected void finalize() {
System
.
out
.
println
(
"Ginie Produkt nr "
+
productId
);
//
System.out.println("Ginie Produkt nr " + productId);
}
//
}
// u
żywanie finalize tak, że miałoby wpływać na działanie, jest niezalecane!
// U
żywanie finalize tak, że miałoby wpływać na działanie, jest niezalecane!
@Id
@Id
@GeneratedValue
(
strategy
=
GenerationType
.
IDENTITY
)
@GeneratedValue
(
strategy
=
GenerationType
.
IDENTITY
)
...
@@ -55,12 +55,12 @@ public class Product implements Serializable {
...
@@ -55,12 +55,12 @@ public class Product implements Serializable {
@NotNull
@NotNull
@DecimalMin
(
"0.01"
)
@DecimalMin
(
"0.01"
)
@DecimalMax
(
"9999
.99"
)
// tylko dla sprawdzenia działania → docelowo wartość ma być większa
@DecimalMax
(
"9999
9999.99"
)
private
BigDecimal
price
;
private
BigDecimal
price
;
@Column
(
name
=
"product_name"
)
@Column
(
name
=
"product_name"
)
@NotNull
@NotNull
@Size
(
min
=
2
,
max
=
10
)
// FIXME
@Size
(
min
=
2
,
max
=
10
0
)
private
String
productName
;
private
String
productName
;
@DecimalMin
(
"0.00"
)
@DecimalMin
(
"0.00"
)
...
...
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