Skip to content

Commit

Permalink
🔀 ✨ Get, Put and Delete testimonials
Browse files Browse the repository at this point in the history
Merge branch 'feat/testimonials'
  • Loading branch information
ecureuill committed Jul 27, 2023
2 parents 52fd67d + 89006cf commit 5a2c728
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package ecureuill.milhasapi.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -14,8 +19,13 @@
import ecureuill.milhasapi.domain.testimonial.TestimonialCreateRecord;
import ecureuill.milhasapi.domain.testimonial.TestimonialDetailRecord;
import ecureuill.milhasapi.domain.testimonial.TestimonialRepository;
import ecureuill.milhasapi.domain.testimonial.TestimonialUpdateRecord;
import jakarta.persistence.EntityNotFoundException;
import jakarta.transaction.Transactional;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;


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

return ResponseEntity.created(uri).body(dto);
}

@GetMapping
public ResponseEntity<List<TestimonialDetailRecord>> getAll(){
return ResponseEntity.ok().body(repository.findAll().stream().map(TestimonialDetailRecord::new).collect(Collectors.toList()));
}

@PutMapping(value="/{id}")
@Transactional
public ResponseEntity<TestimonialDetailRecord> update(@PathVariable Long id, @RequestBody TestimonialUpdateRecord record) {
var data = repository.findById(id);
if (data.isEmpty()) {
throw new EntityNotFoundException();
}
var testimonial = data.get();
testimonial.update(record);

return ResponseEntity.ok().body(new TestimonialDetailRecord(testimonial));
}

@DeleteMapping(value="/{id}")
@Transactional
@ResponseStatus(HttpStatus.NO_CONTENT)
public ResponseEntity<Void> delete(@PathVariable Long id) {
var data = repository.findById(id);
if (data.isEmpty()) {
throw new EntityNotFoundException();
}
repository.deleteById(id);

return ResponseEntity.noContent().build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@ public Testimonial(TestimonialCreateRecord record) {
this.testimonial = record.testimonial();
this.photo = record.photo();
}

public void update(TestimonialUpdateRecord record) {
this.name = record.name();
this.testimonial = record.testimonial();
this.photo = record.photo();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ecureuill.milhasapi.domain.testimonial;

import jakarta.validation.constraints.NotBlank;

public record TestimonialUpdateRecord(
@NotBlank
String name,
@NotBlank
String testimonial,
String photo
) {

}

0 comments on commit 5a2c728

Please sign in to comment.