4
4
import java .util .List ;
5
5
import java .util .stream .Collectors ;
6
6
7
+ import javax .validation .Valid ;
8
+
7
9
import org .springframework .beans .factory .annotation .Autowired ;
8
10
import org .springframework .data .domain .Page ;
9
11
import org .springframework .http .ResponseEntity ;
@@ -36,17 +38,17 @@ public ResponseEntity<Category> find(@PathVariable Integer id) {
36
38
}
37
39
38
40
@ PostMapping
39
- public ResponseEntity <Void > insert (@ RequestBody Category category ) {
40
- category = categoryService .insert (category );
41
-
42
- URI uri = ServletUriComponentsBuilder .fromCurrentRequest ().path ("/{id}" ).buildAndExpand (category .getId ())
43
- .toUri ();
41
+ public ResponseEntity <Void > insert (@ Valid @ RequestBody CategoryDTO categoryDto ) {
42
+ Category category = categoryService .insert (categoryDto .toCategory ());
43
+ URI uri = ServletUriComponentsBuilder .fromCurrentRequest ().path ("/{id}" ).
44
+ buildAndExpand (category .getId ()).toUri ();
44
45
45
46
return ResponseEntity .created (uri ).build ();
46
47
}
47
48
48
49
@ PutMapping ("/{id}" )
49
- public ResponseEntity <Void > update (@ RequestBody Category category , @ PathVariable Integer id ) {
50
+ public ResponseEntity <Void > update (@ Valid @ RequestBody CategoryDTO categoryDto , @ PathVariable Integer id ) {
51
+ Category category = categoryDto .toCategory ();
50
52
category .setId (id );
51
53
category = categoryService .update (category );
52
54
return ResponseEntity .noContent ().build ();
@@ -66,11 +68,10 @@ public ResponseEntity<List<CategoryDTO>> findAll() {
66
68
}
67
69
68
70
@ GetMapping ("/page" )
69
- public ResponseEntity <Page <CategoryDTO >> findPage (
70
- @ RequestParam (name = "page" , defaultValue = "0" ) Integer page ,
71
- @ RequestParam (name = "linesPerPage" , defaultValue = "24" )Integer linesPerPages ,
72
- @ RequestParam (name = "orderBy" , defaultValue = "name" )String orderBy ,
73
- @ RequestParam (name = "direction" , defaultValue = "ASC" )String direction ) {
71
+ public ResponseEntity <Page <CategoryDTO >> findPage (@ RequestParam (name = "page" , defaultValue = "0" ) Integer page ,
72
+ @ RequestParam (name = "linesPerPage" , defaultValue = "24" ) Integer linesPerPages ,
73
+ @ RequestParam (name = "orderBy" , defaultValue = "name" ) String orderBy ,
74
+ @ RequestParam (name = "direction" , defaultValue = "ASC" ) String direction ) {
74
75
Page <Category > listCategory = categoryService .findPage (page , linesPerPages , orderBy , direction );
75
76
Page <CategoryDTO > listDTO = listCategory .map (e -> new CategoryDTO (e ));
76
77
return ResponseEntity .ok ().body (listDTO );
0 commit comments