package org.jhipster.web.rest; import com.codahale.metrics.annotation.Timed; import org.jhipster.domain.Blog; import org.jhipster.repository.BlogRepository; import org.jhipster.web.rest.util.HeaderUtil; import io.github.jhipster.web.util.ResponseUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.net.URI; import java.net.URISyntaxException; import java.util.List; import java.util.Optional; /** * REST controller for managing Blog. */ @RestController @RequestMapping("/api") public class BlogResource { private final Logger log = LoggerFactory.getLogger(BlogResource.class); private static final String ENTITY_NAME = "blog"; private final BlogRepository blogRepository; public BlogResource(BlogRepository blogRepository) { this.blogRepository = blogRepository; } /** * POST /blogs : Create a new blog. * * @param blog the blog to create * @return the ResponseEntity with status 201 (Created) and with body the new blog, or with status 400 (Bad Request) if the blog has already an ID * @throws URISyntaxException if the Location URI syntax is incorrect */ @PostMapping("/blogs") @Timed public ResponseEntity<Blog> createBlog(@Valid @RequestBody Blog blog) throws URISyntaxException { log.debug("REST request to save Blog : {}", blog); if (blog.getId() != null) { return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new blog cannot already have an ID")).body(null); } Blog result = blogRepository.save(blog); return ResponseEntity.created(new URI("/api/blogs/" + result.getId())) .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString())) .body(result); } /** * PUT /blogs : Updates an existing blog. * * @param blog the blog to update * @return the ResponseEntity with status 200 (OK) and with body the updated blog, * or with status 400 (Bad Request) if the blog is not valid, * or with status 500 (Internal Server Error) if the blog couldnt be updated * @throws URISyntaxException if the Location URI syntax is incorrect */ @PutMapping("/blogs") @Timed public ResponseEntity<Blog> updateBlog(@Valid @RequestBody Blog blog) throws URISyntaxException { log.debug("REST request to update Blog : {}", blog); if (blog.getId() == null) { return createBlog(blog); } Blog result = blogRepository.save(blog); return ResponseEntity.ok() .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, blog.getId().toString())) .body(result); } /** * GET /blogs : get all the blogs. * * @return the ResponseEntity with status 200 (OK) and the list of blogs in body */ @GetMapping("/blogs") @Timed public List<Blog> getAllBlogs() { log.debug("REST request to get all Blogs"); List<Blog> blogs = blogRepository.findByUserIsCurrentUser(); return blogs; } /** * GET /blogs/:id : get the "id" blog. * * @param id the id of the blog to retrieve * @return the ResponseEntity with status 200 (OK) and with body the blog, or with status 404 (Not Found) */ @GetMapping("/blogs/{id}") @Timed public ResponseEntity<Blog> getBlog(@PathVariable Long id) { log.debug("REST request to get Blog : {}", id); Blog blog = blogRepository.findOne(id); return ResponseUtil.wrapOrNotFound(Optional.ofNullable(blog)); } /** * DELETE /blogs/:id : delete the "id" blog. * * @param id the id of the blog to delete * @return the ResponseEntity with status 200 (OK) */ @DeleteMapping("/blogs/{id}") @Timed public ResponseEntity<Void> deleteBlog(@PathVariable Long id) { log.debug("REST request to delete Blog : {}", id); blogRepository.delete(id); return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build(); } }