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
ba270f8c
Commit
ba270f8c
authored
Oct 05, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adnotacje Bean Validation i sprawdzanie podczas save
parent
cdac874d
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
56 additions
and
2 deletions
+56
-2
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+8
-1
Customer.java
PC30-SklepSpring/src/main/java/sklep/model/Customer.java
+3
-0
OrderProduct.java
PC30-SklepSpring/src/main/java/sklep/model/OrderProduct.java
+3
-0
Product.java
PC30-SklepSpring/src/main/java/sklep/model/Product.java
+20
-1
ExceptionUtils.java
...SklepSpring/src/main/java/sklep/utils/ExceptionUtils.java
+22
-0
No files found.
PC30-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
ba270f8c
...
@@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.PostMapping;
...
@@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.PostMapping;
import
sklep.model.Product
;
import
sklep.model.Product
;
import
sklep.repository.ProductRepository
;
import
sklep.repository.ProductRepository
;
import
sklep.utils.ExceptionUtils
;
@Controller
@Controller
public
class
ProductController
{
public
class
ProductController
{
...
@@ -66,6 +67,12 @@ public class ProductController {
...
@@ -66,6 +67,12 @@ 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)
// i nie trzeba dodawać go w osobnym poleceniu.
// i nie trzeba dodawać go w osobnym poleceniu.
// Kwestia Bean Validation:
// W tej wersji (bez adnotacji @Valid) Spring wchodzi do tej metody
// i dopiero podczas próby zapisania obiektu operacją save Hibernate
// dokonuje walidacji obiektu względem adnotacji.
// W przypadku niezgodności wyrzucany jest wyjątek, a obiekt nie jest zapisywany.
// Dzieje się to jeszcze zanim dane trafią do bazy danych.
try
{
try
{
System
.
out
.
println
(
"Produkt przed zapisem: "
+
product
);
System
.
out
.
println
(
"Produkt przed zapisem: "
+
product
);
productRepository
.
save
(
product
);
productRepository
.
save
(
product
);
...
@@ -73,7 +80,7 @@ public class ProductController {
...
@@ -73,7 +80,7 @@ public class ProductController {
model
.
addAttribute
(
"saved"
,
true
);
model
.
addAttribute
(
"saved"
,
true
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"Produkt bez zapisu : "
+
product
);
System
.
out
.
println
(
"Produkt bez zapisu : "
+
product
);
model
.
addAttribute
(
"errors"
,
List
.
of
(
e
.
toString
()
));
model
.
addAttribute
(
"errors"
,
ExceptionUtils
.
allMessages
(
e
));
}
}
return
"product_form"
;
return
"product_form"
;
}
}
...
...
PC30-SklepSpring/src/main/java/sklep/model/Customer.java
View file @
ba270f8c
...
@@ -2,6 +2,8 @@ package sklep.model;
...
@@ -2,6 +2,8 @@ package sklep.model;
import
java.io.Serializable
;
import
java.io.Serializable
;
import
javax.persistence.*
;
import
javax.persistence.*
;
import
javax.validation.constraints.Pattern
;
import
java.util.List
;
import
java.util.List
;
...
@@ -30,6 +32,7 @@ public class Customer implements Serializable {
...
@@ -30,6 +32,7 @@ public class Customer implements Serializable {
private
String
phoneNumber
;
private
String
phoneNumber
;
@Column
(
name
=
"postal_code"
)
@Column
(
name
=
"postal_code"
)
@Pattern
(
regexp
=
"\\d{2}-\\d{3}"
)
private
String
postalCode
;
private
String
postalCode
;
//bi-directional many-to-one association to Order
//bi-directional many-to-one association to Order
...
...
PC30-SklepSpring/src/main/java/sklep/model/OrderProduct.java
View file @
ba270f8c
...
@@ -2,6 +2,8 @@ package sklep.model;
...
@@ -2,6 +2,8 @@ package sklep.model;
import
java.io.Serializable
;
import
java.io.Serializable
;
import
javax.persistence.*
;
import
javax.persistence.*
;
import
javax.validation.constraints.Min
;
import
java.math.BigDecimal
;
import
java.math.BigDecimal
;
...
@@ -21,6 +23,7 @@ public class OrderProduct implements Serializable {
...
@@ -21,6 +23,7 @@ public class OrderProduct implements Serializable {
@Column
(
name
=
"actual_price"
)
@Column
(
name
=
"actual_price"
)
private
BigDecimal
actualPrice
;
private
BigDecimal
actualPrice
;
@Min
(
1
)
private
Integer
quantity
;
private
Integer
quantity
;
//bi-directional many-to-one association to Order
//bi-directional many-to-one association to Order
...
...
PC30-SklepSpring/src/main/java/sklep/model/Product.java
View file @
ba270f8c
package
sklep
.
model
;
package
sklep
.
model
;
import
java.io.Serializable
;
import
java.io.Serializable
;
import
javax.persistence.*
;
import
java.math.BigDecimal
;
import
java.math.BigDecimal
;
import
javax.persistence.Column
;
import
javax.persistence.Entity
;
import
javax.persistence.GeneratedValue
;
import
javax.persistence.GenerationType
;
import
javax.persistence.Id
;
import
javax.persistence.NamedQuery
;
import
javax.persistence.Table
;
import
javax.validation.constraints.DecimalMax
;
import
javax.validation.constraints.DecimalMin
;
import
javax.validation.constraints.NotNull
;
import
javax.validation.constraints.Size
;
/**
/**
* The persistent class for the products database table.
* The persistent class for the products database table.
*
*
...
@@ -30,13 +41,21 @@ public class Product implements Serializable {
...
@@ -30,13 +41,21 @@ public class Product implements Serializable {
@Column
(
name
=
"product_id"
,
insertable
=
false
,
updatable
=
false
)
@Column
(
name
=
"product_id"
,
insertable
=
false
,
updatable
=
false
)
private
Integer
productId
;
private
Integer
productId
;
@Size
(
max
=
4000
)
private
String
description
;
private
String
description
;
@NotNull
@DecimalMin
(
"0.01"
)
@DecimalMax
(
"9999.99"
)
// tylko dla sprawdzenia działania → docelowo wartość ma być większa
private
BigDecimal
price
;
private
BigDecimal
price
;
@Column
(
name
=
"product_name"
)
@Column
(
name
=
"product_name"
)
@NotNull
@Size
(
min
=
2
,
max
=
10
)
// FIXME
private
String
productName
;
private
String
productName
;
@DecimalMin
(
"0.00"
)
@DecimalMax
(
"0.99"
)
private
BigDecimal
vat
;
private
BigDecimal
vat
;
public
Product
()
{
public
Product
()
{
...
...
PC30-SklepSpring/src/main/java/sklep/utils/ExceptionUtils.java
0 → 100644
View file @
ba270f8c
package
sklep
.
utils
;
import
java.util.ArrayList
;
import
java.util.List
;
public
class
ExceptionUtils
{
public
static
List
<
String
>
allMessages
(
Throwable
e
)
{
List
<
String
>
messages
=
new
ArrayList
<>();
messages
.
add
(
oneMessage
(
e
));
Throwable
cause
=
e
.
getCause
();
while
(
cause
!=
null
)
{
messages
.
add
(
oneMessage
(
cause
));
cause
=
cause
.
getCause
();
}
return
messages
;
}
private
static
String
oneMessage
(
Throwable
e
)
{
return
e
.
getClass
().
getSimpleName
()
+
": "
+
e
.
getMessage
();
}
}
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