|
| 1 | +package ecureuill.milhasapi.controller; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.stream.Collectors; |
| 5 | + |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.http.HttpStatus; |
| 8 | +import org.springframework.http.ResponseEntity; |
| 9 | +import org.springframework.web.bind.annotation.DeleteMapping; |
| 10 | +import org.springframework.web.bind.annotation.GetMapping; |
| 11 | +import org.springframework.web.bind.annotation.PathVariable; |
| 12 | +import org.springframework.web.bind.annotation.PostMapping; |
| 13 | +import org.springframework.web.bind.annotation.PutMapping; |
| 14 | +import org.springframework.web.bind.annotation.RequestBody; |
| 15 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 16 | +import org.springframework.web.bind.annotation.RequestParam; |
| 17 | +import org.springframework.web.bind.annotation.ResponseStatus; |
| 18 | +import org.springframework.web.bind.annotation.RestController; |
| 19 | +import org.springframework.web.server.ResponseStatusException; |
| 20 | +import org.springframework.web.util.UriComponentsBuilder; |
| 21 | + |
| 22 | +import ecureuill.milhasapi.domain.destination.DestinationRepository; |
| 23 | +import ecureuill.milhasapi.domain.destination.DestinationUpdateRecord; |
| 24 | +import jakarta.transaction.Transactional; |
| 25 | +import jakarta.validation.Valid; |
| 26 | +import ecureuill.milhasapi.domain.destination.Destination; |
| 27 | +import ecureuill.milhasapi.domain.destination.DestinationCreateRecord; |
| 28 | +import ecureuill.milhasapi.domain.destination.DestinationDetailRecord; |
| 29 | + |
| 30 | +@RestController |
| 31 | +@RequestMapping("/destinos") |
| 32 | +public class DestinationController { |
| 33 | + |
| 34 | + @Autowired |
| 35 | + private DestinationRepository repository; |
| 36 | + |
| 37 | + @PostMapping |
| 38 | + @Transactional |
| 39 | + @ResponseStatus(HttpStatus.CREATED) |
| 40 | + public ResponseEntity<DestinationDetailRecord> create(@Valid @RequestBody DestinationCreateRecord record, UriComponentsBuilder uriBuilder) { |
| 41 | + |
| 42 | + var destination = repository.save(new Destination(record)); |
| 43 | + |
| 44 | + var uri = uriBuilder.path("destinos/{id}").buildAndExpand(destination.getId()).toUri(); |
| 45 | + |
| 46 | + return ResponseEntity.created(uri).body(new DestinationDetailRecord(destination)); |
| 47 | + } |
| 48 | + |
| 49 | + @GetMapping |
| 50 | + @ResponseStatus(HttpStatus.OK) |
| 51 | + public ResponseEntity<List<DestinationDetailRecord>> getAll(@RequestParam(name="name", required = false) String name) { |
| 52 | + |
| 53 | + if(name != null){ |
| 54 | + var result = repository.findAllByNameStartingWith(name); |
| 55 | + if (result.isEmpty()) { |
| 56 | + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Nenhum destino foi encontrado", null); |
| 57 | + } |
| 58 | + |
| 59 | + return ResponseEntity.ok().body(result.stream().map(DestinationDetailRecord::new).collect(Collectors.toList())); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + return ResponseEntity.ok().body(repository.findAll().stream().map(DestinationDetailRecord::new).collect(Collectors.toList())); |
| 64 | + } |
| 65 | + |
| 66 | + @GetMapping(value = "{id}") |
| 67 | + @ResponseStatus(HttpStatus.OK) |
| 68 | + public ResponseEntity<DestinationDetailRecord> getOne(@PathVariable Long id) { |
| 69 | + var data = repository.getReferenceById(id); |
| 70 | + |
| 71 | + return ResponseEntity.ok().body(new DestinationDetailRecord(data)); |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + @PutMapping(value = "/{id}") |
| 76 | + @Transactional |
| 77 | + @ResponseStatus(HttpStatus.OK) |
| 78 | + public ResponseEntity<DestinationDetailRecord> update(@PathVariable Long id, @Valid @RequestBody DestinationUpdateRecord record) { |
| 79 | + var destination = repository.getReferenceById(id); |
| 80 | + |
| 81 | + destination.update(record); |
| 82 | + |
| 83 | + return ResponseEntity.ok().body(new DestinationDetailRecord(destination)); |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + @DeleteMapping(value = "{id}") |
| 88 | + @Transactional |
| 89 | + @ResponseStatus(HttpStatus.NO_CONTENT) |
| 90 | + public ResponseEntity<Void> delete(@PathVariable Long id) { |
| 91 | + repository.deleteById(id); |
| 92 | + |
| 93 | + return ResponseEntity.noContent().build(); |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | +} |
0 commit comments