PhotoResource.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package rocks.zipcode.io.web.rest;
  2. import com.codahale.metrics.annotation.Timed;
  3. import rocks.zipcode.io.domain.Photo;
  4. import rocks.zipcode.io.repository.PhotoRepository;
  5. import rocks.zipcode.io.web.rest.errors.BadRequestAlertException;
  6. import rocks.zipcode.io.web.rest.util.HeaderUtil;
  7. import io.github.jhipster.web.util.ResponseUtil;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.springframework.http.ResponseEntity;
  11. import org.springframework.web.bind.annotation.*;
  12. import java.net.URI;
  13. import java.net.URISyntaxException;
  14. import java.util.List;
  15. import java.util.Optional;
  16. /**
  17. * REST controller for managing Photo.
  18. */
  19. @RestController
  20. @RequestMapping("/api")
  21. public class PhotoResource {
  22. private final Logger log = LoggerFactory.getLogger(PhotoResource.class);
  23. private static final String ENTITY_NAME = "photo";
  24. private final PhotoRepository photoRepository;
  25. public PhotoResource(PhotoRepository photoRepository) {
  26. this.photoRepository = photoRepository;
  27. }
  28. /**
  29. * POST /photos : Create a new photo.
  30. *
  31. * @param photo the photo to create
  32. * @return the ResponseEntity with status 201 (Created) and with body the new photo, or with status 400 (Bad Request) if the photo has already an ID
  33. * @throws URISyntaxException if the Location URI syntax is incorrect
  34. */
  35. @PostMapping("/photos")
  36. @Timed
  37. public ResponseEntity<Photo> createPhoto(@RequestBody Photo photo) throws URISyntaxException {
  38. log.debug("REST request to save Photo : {}", photo);
  39. if (photo.getId() != null) {
  40. throw new BadRequestAlertException("A new photo cannot already have an ID", ENTITY_NAME, "idexists");
  41. }
  42. Photo result = photoRepository.save(photo);
  43. return ResponseEntity.created(new URI("/api/photos/" + result.getId()))
  44. .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
  45. .body(result);
  46. }
  47. /**
  48. * PUT /photos : Updates an existing photo.
  49. *
  50. * @param photo the photo to update
  51. * @return the ResponseEntity with status 200 (OK) and with body the updated photo,
  52. * or with status 400 (Bad Request) if the photo is not valid,
  53. * or with status 500 (Internal Server Error) if the photo couldn't be updated
  54. * @throws URISyntaxException if the Location URI syntax is incorrect
  55. */
  56. @PutMapping("/photos")
  57. @Timed
  58. public ResponseEntity<Photo> updatePhoto(@RequestBody Photo photo) throws URISyntaxException {
  59. log.debug("REST request to update Photo : {}", photo);
  60. if (photo.getId() == null) {
  61. throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
  62. }
  63. Photo result = photoRepository.save(photo);
  64. return ResponseEntity.ok()
  65. .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, photo.getId().toString()))
  66. .body(result);
  67. }
  68. /**
  69. * GET /photos : get all the photos.
  70. *
  71. * @param eagerload flag to eager load entities from relationships (This is applicable for many-to-many)
  72. * @return the ResponseEntity with status 200 (OK) and the list of photos in body
  73. */
  74. @GetMapping("/photos")
  75. @Timed
  76. public List<Photo> getAllPhotos(@RequestParam(required = false, defaultValue = "false") boolean eagerload) {
  77. log.debug("REST request to get all Photos");
  78. return photoRepository.findAllWithEagerRelationships();
  79. }
  80. /**
  81. * GET /photos/:id : get the "id" photo.
  82. *
  83. * @param id the id of the photo to retrieve
  84. * @return the ResponseEntity with status 200 (OK) and with body the photo, or with status 404 (Not Found)
  85. */
  86. @GetMapping("/photos/{id}")
  87. @Timed
  88. public ResponseEntity<Photo> getPhoto(@PathVariable Long id) {
  89. log.debug("REST request to get Photo : {}", id);
  90. Optional<Photo> photo = photoRepository.findOneWithEagerRelationships(id);
  91. return ResponseUtil.wrapOrNotFound(photo);
  92. }
  93. /**
  94. * DELETE /photos/:id : delete the "id" photo.
  95. *
  96. * @param id the id of the photo to delete
  97. * @return the ResponseEntity with status 200 (OK)
  98. */
  99. @DeleteMapping("/photos/{id}")
  100. @Timed
  101. public ResponseEntity<Void> deletePhoto(@PathVariable Long id) {
  102. log.debug("REST request to delete Photo : {}", id);
  103. photoRepository.deleteById(id);
  104. return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
  105. }
  106. }