|
| 1 | +package com.bookstore.web.rest; |
| 2 | + |
| 3 | +import com.codahale.metrics.annotation.Timed; |
| 4 | +import com.bookstore.domain.Author; |
| 5 | +import com.bookstore.repository.AuthorRepository; |
| 6 | +import com.bookstore.web.rest.util.PaginationUtil; |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | +import org.springframework.data.domain.Page; |
| 10 | +import org.springframework.http.HttpHeaders; |
| 11 | +import org.springframework.http.HttpStatus; |
| 12 | +import org.springframework.http.MediaType; |
| 13 | +import org.springframework.http.ResponseEntity; |
| 14 | +import org.springframework.web.bind.annotation.*; |
| 15 | + |
| 16 | +import javax.inject.Inject; |
| 17 | +import javax.validation.Valid; |
| 18 | +import java.net.URI; |
| 19 | +import java.net.URISyntaxException; |
| 20 | +import javax.servlet.http.HttpServletResponse; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +/** |
| 24 | + * REST controller for managing Author. |
| 25 | + */ |
| 26 | +@RestController |
| 27 | +@RequestMapping("/api") |
| 28 | +public class AuthorResource { |
| 29 | + |
| 30 | + private final Logger log = LoggerFactory.getLogger(AuthorResource.class); |
| 31 | + |
| 32 | + @Inject |
| 33 | + private AuthorRepository authorRepository; |
| 34 | + |
| 35 | + /** |
| 36 | + * POST /authors -> Create a new author. |
| 37 | + */ |
| 38 | + @RequestMapping(value = "/authors", |
| 39 | + method = RequestMethod.POST, |
| 40 | + produces = MediaType.APPLICATION_JSON_VALUE) |
| 41 | + @Timed |
| 42 | + public ResponseEntity<Void> create(@Valid @RequestBody Author author) throws URISyntaxException { |
| 43 | + log.debug("REST request to save Author : {}", author); |
| 44 | + if (author.getId() != null) { |
| 45 | + return ResponseEntity.badRequest().header("Failure", "A new author cannot already have an ID").build(); |
| 46 | + } |
| 47 | + authorRepository.save(author); |
| 48 | + return ResponseEntity.created(new URI("/api/authors/" + author.getId())).build(); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * PUT /authors -> Updates an existing author. |
| 53 | + */ |
| 54 | + @RequestMapping(value = "/authors", |
| 55 | + method = RequestMethod.PUT, |
| 56 | + produces = MediaType.APPLICATION_JSON_VALUE) |
| 57 | + @Timed |
| 58 | + public ResponseEntity<Void> update(@Valid @RequestBody Author author) throws URISyntaxException { |
| 59 | + log.debug("REST request to update Author : {}", author); |
| 60 | + if (author.getId() == null) { |
| 61 | + return create(author); |
| 62 | + } |
| 63 | + authorRepository.save(author); |
| 64 | + return ResponseEntity.ok().build(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * GET /authors -> get all the authors. |
| 69 | + */ |
| 70 | + @RequestMapping(value = "/authors", |
| 71 | + method = RequestMethod.GET, |
| 72 | + produces = MediaType.APPLICATION_JSON_VALUE) |
| 73 | + @Timed |
| 74 | + public ResponseEntity<List<Author>> getAll(@RequestParam(value = "page" , required = false) Integer offset, |
| 75 | + @RequestParam(value = "per_page", required = false) Integer limit) |
| 76 | + throws URISyntaxException { |
| 77 | + Page<Author> page = authorRepository.findAll(PaginationUtil.generatePageRequest(offset, limit)); |
| 78 | + HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/authors", offset, limit); |
| 79 | + return new ResponseEntity<List<Author>>(page.getContent(), headers, HttpStatus.OK); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * GET /authors/:id -> get the "id" author. |
| 84 | + */ |
| 85 | + @RequestMapping(value = "/authors/{id}", |
| 86 | + method = RequestMethod.GET, |
| 87 | + produces = MediaType.APPLICATION_JSON_VALUE) |
| 88 | + @Timed |
| 89 | + public ResponseEntity<Author> get(@PathVariable Long id, HttpServletResponse response) { |
| 90 | + log.debug("REST request to get Author : {}", id); |
| 91 | + Author author = authorRepository.findOne(id); |
| 92 | + if (author == null) { |
| 93 | + return new ResponseEntity<>(HttpStatus.NOT_FOUND); |
| 94 | + } |
| 95 | + return new ResponseEntity<>(author, HttpStatus.OK); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * DELETE /authors/:id -> delete the "id" author. |
| 100 | + */ |
| 101 | + @RequestMapping(value = "/authors/{id}", |
| 102 | + method = RequestMethod.DELETE, |
| 103 | + produces = MediaType.APPLICATION_JSON_VALUE) |
| 104 | + @Timed |
| 105 | + public void delete(@PathVariable Long id) { |
| 106 | + log.debug("REST request to delete Author : {}", id); |
| 107 | + authorRepository.delete(id); |
| 108 | + } |
| 109 | +} |
0 commit comments