package com.dangdang.mypp.web.rest; import com.codahale.metrics.annotation.Timed; import com.dangdang.mypp.domain.Author; import com.dangdang.mypp.repository.AuthorRepository; import com.dangdang.mypp.web.rest.util.PaginationUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.domain.Page; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import javax.inject.Inject; import javax.validation.Valid; import java.net.URI; import java.net.URISyntaxException; import javax.servlet.http.HttpServletResponse; import java.util.List; /** * REST controller for managing Author. */ @RestController @RequestMapping("/api") public class AuthorResource { private final Logger log = LoggerFactory.getLogger(AuthorResource.class); @Inject private AuthorRepository authorRepository; /** * POST /authors -> Create a new author. */ @RequestMapping(value = "/authors", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @Timed public ResponseEntity<Void> create(@Valid @RequestBody Author author) throws URISyntaxException { log.debug("REST request to save Author : {}", author); if (author.getId() != null) { return ResponseEntity.badRequest().header("Failure", "A new author cannot already have an ID").build(); } authorRepository.save(author); return ResponseEntity.created(new URI("/api/authors/" + author.getId())).build(); } /** * PUT /authors -> Updates an existing author. */ @RequestMapping(value = "/authors", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE) @Timed public ResponseEntity<Void> update(@Valid @RequestBody Author author) throws URISyntaxException { log.debug("REST request to update Author : {}", author); if (author.getId() == null) { return create(author); } authorRepository.save(author); return ResponseEntity.ok().build(); } /** * GET /authors -> get all the authors. */ @RequestMapping(value = "/authors", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @Timed public ResponseEntity<List<Author>> getAll(@RequestParam(value = "page" , required = false) Integer offset, @RequestParam(value = "per_page", required = false) Integer limit) throws URISyntaxException { Page<Author> page = authorRepository.findAll(PaginationUtil.generatePageRequest(offset, limit)); HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/authors", offset, limit); return new ResponseEntity<List<Author>>(page.getContent(), headers, HttpStatus.OK); } /** * GET /authors/:id -> get the "id" author. */ @RequestMapping(value = "/authors/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @Timed public ResponseEntity<Author> get(@PathVariable Long id, HttpServletResponse response) { log.debug("REST request to get Author : {}", id); Author author = authorRepository.findOne(id); if (author == null) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } return new ResponseEntity<>(author, HttpStatus.OK); } /** * DELETE /authors/:id -> delete the "id" author. */ @RequestMapping(value = "/authors/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE) @Timed public void delete(@PathVariable Long id) { log.debug("REST request to delete Author : {}", id); authorRepository.delete(id); } }