Guest User

Untitled

a guest
Nov 15th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1.  
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.ui.Model;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.PathVariable;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestMethod;
  11.  
  12.  
  13. @Controller
  14. public class PostsController {
  15.  
  16.     private final Logger logger = LoggerFactory.getLogger(this.getClass());
  17.  
  18.     @Autowired
  19.     private TitlesService titlesService;
  20.  
  21.     @Autowired
  22.     private PostService postService;
  23.  
  24.     @RequestMapping(value = "/addPost", method = RequestMethod.POST)
  25.     public String addPost(Post post) {
  26.         logger.info("Post object has been captured: " + post.toString());
  27.         postService.savePost(post);
  28.         titlesService.saveTitle(post);
  29.         return "redirect:/";
  30.     }
  31.  
  32.     @RequestMapping(value = "/add_post", method = RequestMethod.GET)
  33.     public String admin(Model model) {
  34.         model.addAttribute("post", new Post());
  35.         return "addPost";
  36.     }
  37.  
  38.     @RequestMapping(value = "/posts", method = RequestMethod.GET)
  39.     public String showAllPosts(Model model) {
  40.         return "posts";
  41.     }
  42.  
  43.     @GetMapping("/post/{postId}")
  44.     public String showSpecificPost(Model model, @PathVariable("postId") String id) {
  45.         Post post = postService.findPostById(id);
  46.         model.addAttribute("specificPost", post);
  47.         model.addAttribute("numberOfComments", post.getComments() != null ? post.getComments().size() : 0);
  48.         return "post";
  49.     }
  50.  
  51.     @RequestMapping(value = "/addComment", method = RequestMethod.POST)
  52.     public String addComment(Post post) {
  53.         logger.info("Post with a comment has been captured: " + post.getComments().toString());
  54.         postService.updatePostWithComment(post);
  55.         return "redirect:/";
  56.     }
  57.  
  58.  
  59. }
Add Comment
Please, Sign In to add comment