Commit ba270f8c by Patryk Czarnik

Adnotacje Bean Validation i sprawdzanie podczas save

parent cdac874d
......@@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.PostMapping;
import sklep.model.Product;
import sklep.repository.ProductRepository;
import sklep.utils.ExceptionUtils;
@Controller
public class ProductController {
......@@ -66,6 +67,12 @@ public class ProductController {
// Taki parametr od razu staje się częścią modelu (to jest tzw. ModelAttribute)
// 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 {
System.out.println("Produkt przed zapisem: " + product);
productRepository.save(product);
......@@ -73,7 +80,7 @@ public class ProductController {
model.addAttribute("saved", true);
} catch(Exception e) {
System.out.println("Produkt bez zapisu : " + product);
model.addAttribute("errors", List.of(e.toString()));
model.addAttribute("errors", ExceptionUtils.allMessages(e));
}
return "product_form";
}
......
......@@ -2,6 +2,8 @@ package sklep.model;
import java.io.Serializable;
import javax.persistence.*;
import javax.validation.constraints.Pattern;
import java.util.List;
......@@ -30,6 +32,7 @@ public class Customer implements Serializable {
private String phoneNumber;
@Column(name="postal_code")
@Pattern(regexp="\\d{2}-\\d{3}")
private String postalCode;
//bi-directional many-to-one association to Order
......
......@@ -2,6 +2,8 @@ package sklep.model;
import java.io.Serializable;
import javax.persistence.*;
import javax.validation.constraints.Min;
import java.math.BigDecimal;
......@@ -21,6 +23,7 @@ public class OrderProduct implements Serializable {
@Column(name="actual_price")
private BigDecimal actualPrice;
@Min(1)
private Integer quantity;
//bi-directional many-to-one association to Order
......
package sklep.model;
import java.io.Serializable;
import javax.persistence.*;
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.
*
......@@ -30,13 +41,21 @@ public class Product implements Serializable {
@Column(name = "product_id", insertable = false, updatable = false)
private Integer productId;
@Size(max=4000)
private String description;
@NotNull
@DecimalMin("0.01")
@DecimalMax("9999.99") // tylko dla sprawdzenia działania → docelowo wartość ma być większa
private BigDecimal price;
@Column(name = "product_name")
@NotNull
@Size(min=2, max=10) // FIXME
private String productName;
@DecimalMin("0.00")
@DecimalMax("0.99")
private BigDecimal vat;
public Product() {
......
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();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment