Skip to content

Commit 5a2c728

Browse files
author
ecureuill
committed
πŸ”€ ✨ Get, Put and Delete testimonials
Merge branch 'feat/testimonials'
2 parents 52fd67d + 89006cf commit 5a2c728

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

β€Žsrc/main/java/ecureuill/milhasapi/controller/TestimonialController.java

+41
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package ecureuill.milhasapi.controller;
22

3+
import java.util.List;
4+
import java.util.stream.Collectors;
5+
36
import org.springframework.beans.factory.annotation.Autowired;
47
import org.springframework.http.HttpStatus;
58
import org.springframework.http.ResponseEntity;
9+
import org.springframework.web.bind.annotation.DeleteMapping;
10+
import org.springframework.web.bind.annotation.GetMapping;
611
import org.springframework.web.bind.annotation.PostMapping;
712
import org.springframework.web.bind.annotation.RequestBody;
813
import org.springframework.web.bind.annotation.RequestMapping;
@@ -14,8 +19,13 @@
1419
import ecureuill.milhasapi.domain.testimonial.TestimonialCreateRecord;
1520
import ecureuill.milhasapi.domain.testimonial.TestimonialDetailRecord;
1621
import ecureuill.milhasapi.domain.testimonial.TestimonialRepository;
22+
import ecureuill.milhasapi.domain.testimonial.TestimonialUpdateRecord;
23+
import jakarta.persistence.EntityNotFoundException;
1724
import jakarta.transaction.Transactional;
1825
import jakarta.validation.Valid;
26+
import org.springframework.web.bind.annotation.PutMapping;
27+
import org.springframework.web.bind.annotation.PathVariable;
28+
1929

2030
@RestController
2131
@RequestMapping("/depoimentos")
@@ -34,5 +44,36 @@ public ResponseEntity<TestimonialDetailRecord> save(@RequestBody @Valid Testimon
3444

3545
return ResponseEntity.created(uri).body(dto);
3646
}
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+
}
3778

3879
}

β€Žsrc/main/java/ecureuill/milhasapi/domain/testimonial/Testimonial.java

+6
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,10 @@ public Testimonial(TestimonialCreateRecord record) {
2929
this.testimonial = record.testimonial();
3030
this.photo = record.photo();
3131
}
32+
33+
public void update(TestimonialUpdateRecord record) {
34+
this.name = record.name();
35+
this.testimonial = record.testimonial();
36+
this.photo = record.photo();
37+
}
3238
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ecureuill.milhasapi.domain.testimonial;
2+
3+
import jakarta.validation.constraints.NotBlank;
4+
5+
public record TestimonialUpdateRecord(
6+
@NotBlank
7+
String name,
8+
@NotBlank
9+
String testimonial,
10+
String photo
11+
) {
12+
13+
}

0 commit comments

Comments
Β (0)