File tree Expand file tree Collapse file tree 4 files changed +26
-1
lines changed
src/main/java/com/amigoscode/product Expand file tree Collapse file tree 4 files changed +26
-1
lines changed Original file line number Diff line number Diff line change 5353 <groupId >org.springframework.boot</groupId >
5454 <artifactId >spring-boot-starter-data-jpa</artifactId >
5555 </dependency >
56+ <dependency >
57+ <groupId >org.springframework.boot</groupId >
58+ <artifactId >spring-boot-starter-validation</artifactId >
59+ </dependency >
5660 </dependencies >
5761
5862 <build >
Original file line number Diff line number Diff line change 11package com .amigoscode .product ;
22
3+ import jakarta .validation .constraints .*;
4+
35import java .math .BigDecimal ;
46
57public record NewProductRequest (
8+ @ NotBlank
9+ @ Size (
10+ min = 2 ,
11+ max = 50 ,
12+ message = "Name must be between 2 and 50 characters"
13+ )
614 String name ,
15+ @ Size (
16+ min = 5 ,
17+ max = 500 ,
18+ message = "Description must be between 5 and 500 characters"
19+ )
720 String description ,
21+ @ NotNull (message = "Price is required" )
22+ @ DecimalMin (value = "0.1" , message = "Price must be greater than 0.1" )
823 BigDecimal price ,
24+
25+ @ NotNull (message = "Price is required" )
26+ @ Min (value = 1 , message = "Min Stock Level is 1" )
927 Integer stockLevel ,
1028 String imageUrl
1129) {
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ public class Product {
1919 @ Column (nullable = false , length = 50 )
2020 private String name ;
2121
22+ @ Column (length = 500 )
2223 private String description ;
2324
2425 @ Column (nullable = false , precision = 10 , scale = 2 )
Original file line number Diff line number Diff line change 11package com .amigoscode .product ;
22
3+ import jakarta .validation .Valid ;
4+ import org .springframework .validation .annotation .Validated ;
35import org .springframework .web .bind .annotation .*;
46
57import java .util .List ;
@@ -31,7 +33,7 @@ public void deleteProductById(@PathVariable("id") UUID id) {
3133 }
3234
3335 @ PostMapping
34- public UUID saveProduct (@ RequestBody NewProductRequest product ) {
36+ public UUID saveProduct (@ RequestBody @ Valid NewProductRequest product ) {
3537 return productService .saveNewProduct (product );
3638 }
3739
You can’t perform that action at this time.
0 commit comments