@RequestMapping(method = RequestMethod.GET) @PreAuthorize("permitAll") public ResponseEntity<BlogListResource> findAllBlogs() { BlogList blogList = blogService.findAllBlogs(); BlogListResource blogListRes = new BlogListResourceAsm().toResource(blogList); return new ResponseEntity<BlogListResource>(blogListRes, HttpStatus.OK); }
@RequestMapping(value = "/{blogId}/blog-entries") public ResponseEntity<BlogEntryListResource> findAllBlogEntries(@PathVariable Long blogId) { try { BlogEntryList list = blogService.findAllBlogEntries(blogId); BlogEntryListResource res = new BlogEntryListResourceAsm().toResource(list); return new ResponseEntity<BlogEntryListResource>(res, HttpStatus.OK); } catch (BlogNotFoundException exception) { throw new NotFoundException(exception); } }
@RequestMapping(value = "/{blogId}", method = RequestMethod.GET) public ResponseEntity<BlogResource> getBlog(@PathVariable Long blogId) { Blog blog = blogService.findBlog(blogId); if (blog != null) { BlogResource res = new BlogResourceAsm().toResource(blog); return new ResponseEntity<BlogResource>(res, HttpStatus.OK); } else { return new ResponseEntity<BlogResource>(HttpStatus.NOT_FOUND); } }
@RequestMapping(value = "/{blogId}/blog-entries", method = RequestMethod.POST) public ResponseEntity<BlogEntryResource> createBlogEntry( @PathVariable Long blogId, @RequestBody BlogEntryResource sentBlogEntry) { BlogEntry createdBlogEntry = null; try { createdBlogEntry = blogService.createBlogEntry(blogId, sentBlogEntry.toBlogEntry()); BlogEntryResource createdResource = new BlogEntryResourceAsm().toResource(createdBlogEntry); HttpHeaders headers = new HttpHeaders(); headers.setLocation(URI.create(createdResource.getLink("self").getHref())); return new ResponseEntity<BlogEntryResource>(createdResource, headers, HttpStatus.CREATED); } catch (BlogNotFoundException e) { throw new NotFoundException(e); } }