|
| 1 | +package com.kvark900.api.controller; |
| 2 | + |
| 3 | +import com.kvark900.api.model.Author; |
| 4 | +import com.kvark900.api.service.AuthorService; |
| 5 | +import org.springframework.http.HttpHeaders; |
| 6 | +import org.springframework.http.HttpStatus; |
| 7 | +import org.springframework.http.MediaType; |
| 8 | +import org.springframework.http.ResponseEntity; |
| 9 | +import org.springframework.validation.BindingResult; |
| 10 | +import org.springframework.web.bind.annotation.*; |
| 11 | +import org.springframework.web.util.UriComponentsBuilder; |
| 12 | + |
| 13 | +import javax.validation.Valid; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +/** |
| 17 | + * Created by Keno&Kemo on 17.12.2017.. |
| 18 | + */ |
| 19 | +@RestController |
| 20 | +@RequestMapping(value = "/authors", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, |
| 21 | + consumes = MediaType.APPLICATION_JSON_UTF8_VALUE) |
| 22 | +public class AuthorController { |
| 23 | + private AuthorService authorService; |
| 24 | + |
| 25 | + public AuthorController(AuthorService authorService) { |
| 26 | + this.authorService = authorService; |
| 27 | + } |
| 28 | + |
| 29 | + @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, |
| 30 | + consumes = MediaType.APPLICATION_JSON_UTF8_VALUE) |
| 31 | + public ResponseEntity<List<Author>> getAllAuthors(){ |
| 32 | + List<Author> allAuthors = authorService.findAll(); |
| 33 | + if(allAuthors == null){ |
| 34 | + return new ResponseEntity<>(HttpStatus.NOT_FOUND); |
| 35 | + } |
| 36 | + else if(allAuthors.isEmpty()){ |
| 37 | + return new ResponseEntity<>(allAuthors, HttpStatus.NO_CONTENT); |
| 38 | + } |
| 39 | + else return new ResponseEntity<>(allAuthors, HttpStatus.OK); |
| 40 | + } |
| 41 | + |
| 42 | + @GetMapping ("/{id}") |
| 43 | + public ResponseEntity<Author> getAuthor(@PathVariable Long id){ |
| 44 | + Author author = authorService.findById(id); |
| 45 | + if(author == null){ |
| 46 | + return new ResponseEntity<>(HttpStatus.NOT_FOUND); |
| 47 | + } |
| 48 | + else return new ResponseEntity<>(author, HttpStatus.OK); |
| 49 | + } |
| 50 | + |
| 51 | + @PostMapping(value = "",produces = MediaType.APPLICATION_JSON_UTF8_VALUE, |
| 52 | + consumes = MediaType.APPLICATION_JSON_UTF8_VALUE) |
| 53 | + public ResponseEntity<Author> saveAuthor(@RequestBody @Valid Author author, BindingResult bindingResult, |
| 54 | + UriComponentsBuilder uriComponentsBuilder){ |
| 55 | + BindingErrorsResponse errors = new BindingErrorsResponse(); |
| 56 | + HttpHeaders headers = new HttpHeaders(); |
| 57 | + if(bindingResult.hasErrors() || (author == null)){ |
| 58 | + errors.addAllErrors(bindingResult); |
| 59 | + headers.add("errors", errors.toJSON()); |
| 60 | + return new ResponseEntity<>(headers, HttpStatus.BAD_REQUEST); |
| 61 | + } |
| 62 | + else{ |
| 63 | + authorService.save(author); |
| 64 | + headers.setLocation(uriComponentsBuilder.path("/authors/{id}"). |
| 65 | + buildAndExpand(author.getId()).toUri()); |
| 66 | + return new ResponseEntity<>(author, headers, HttpStatus.CREATED); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @PutMapping ("/{id}") |
| 71 | + public ResponseEntity<Author> updateAuthor(@PathVariable ("id") Long id, @RequestBody @Valid Author author, |
| 72 | + BindingResult bindingResult){ |
| 73 | + Author currentAuthor = authorService.findById(id); |
| 74 | + BindingErrorsResponse errors = new BindingErrorsResponse(); |
| 75 | + HttpHeaders headers = new HttpHeaders(); |
| 76 | + if(bindingResult.hasErrors() || (author == null)){ |
| 77 | + errors.addAllErrors(bindingResult); |
| 78 | + headers.add("errors", errors.toJSON()); |
| 79 | + return new ResponseEntity<>(headers, HttpStatus.BAD_REQUEST); |
| 80 | + } |
| 81 | + if(currentAuthor == null){ |
| 82 | + return new ResponseEntity<>(HttpStatus.NOT_FOUND); |
| 83 | + } |
| 84 | + authorService.update(author); |
| 85 | + return new ResponseEntity<>(author, HttpStatus.NO_CONTENT); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + @DeleteMapping ("/{id}") |
| 91 | + public ResponseEntity<Author> deleteAuthor(@PathVariable ("id") Long id){ |
| 92 | + Author authorToDelete = authorService.findById(id); |
| 93 | + if(authorToDelete == null){ |
| 94 | + return new ResponseEntity<>(HttpStatus.NOT_FOUND); |
| 95 | + } |
| 96 | + else { |
| 97 | + authorService.delete(id); |
| 98 | + return new ResponseEntity<>(authorToDelete, HttpStatus.NO_CONTENT); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments