1
1
package ecureuill .milhasapi .controller ;
2
2
3
+ import java .util .List ;
4
+ import java .util .stream .Collectors ;
5
+
3
6
import org .springframework .beans .factory .annotation .Autowired ;
4
7
import org .springframework .http .HttpStatus ;
5
8
import org .springframework .http .ResponseEntity ;
9
+ import org .springframework .web .bind .annotation .DeleteMapping ;
10
+ import org .springframework .web .bind .annotation .GetMapping ;
6
11
import org .springframework .web .bind .annotation .PostMapping ;
7
12
import org .springframework .web .bind .annotation .RequestBody ;
8
13
import org .springframework .web .bind .annotation .RequestMapping ;
14
19
import ecureuill .milhasapi .domain .testimonial .TestimonialCreateRecord ;
15
20
import ecureuill .milhasapi .domain .testimonial .TestimonialDetailRecord ;
16
21
import ecureuill .milhasapi .domain .testimonial .TestimonialRepository ;
22
+ import ecureuill .milhasapi .domain .testimonial .TestimonialUpdateRecord ;
23
+ import jakarta .persistence .EntityNotFoundException ;
17
24
import jakarta .transaction .Transactional ;
18
25
import jakarta .validation .Valid ;
26
+ import org .springframework .web .bind .annotation .PutMapping ;
27
+ import org .springframework .web .bind .annotation .PathVariable ;
28
+
19
29
20
30
@ RestController
21
31
@ RequestMapping ("/depoimentos" )
@@ -34,5 +44,36 @@ public ResponseEntity<TestimonialDetailRecord> save(@RequestBody @Valid Testimon
34
44
35
45
return ResponseEntity .created (uri ).body (dto );
36
46
}
47
+
48
+ @ GetMapping
49
+ public ResponseEntity <List <TestimonialDetailRecord >> getAll (){
50
+ return ResponseEntity .ok ().body (repository .findAll ().stream ().map (TestimonialDetailRecord ::new ).collect (Collectors .toList ()));
51
+ }
52
+
53
+ @ PutMapping (value ="/{id}" )
54
+ @ Transactional
55
+ public ResponseEntity <TestimonialDetailRecord > update (@ PathVariable Long id , @ RequestBody TestimonialUpdateRecord record ) {
56
+ var data = repository .findById (id );
57
+ if (data .isEmpty ()) {
58
+ throw new EntityNotFoundException ();
59
+ }
60
+ var testimonial = data .get ();
61
+ testimonial .update (record );
62
+
63
+ return ResponseEntity .ok ().body (new TestimonialDetailRecord (testimonial ));
64
+ }
65
+
66
+ @ DeleteMapping (value ="/{id}" )
67
+ @ Transactional
68
+ @ ResponseStatus (HttpStatus .NO_CONTENT )
69
+ public ResponseEntity <Void > delete (@ PathVariable Long id ) {
70
+ var data = repository .findById (id );
71
+ if (data .isEmpty ()) {
72
+ throw new EntityNotFoundException ();
73
+ }
74
+ repository .deleteById (id );
75
+
76
+ return ResponseEntity .noContent ().build ();
77
+ }
37
78
38
79
}
0 commit comments