11package com .raysmond .blog .admin .controllers ;
22
3- import com .raysmond .blog .config .interceptors .RequestProcessingTimeInterceptor ;
43import com .raysmond .blog .forms .PostForm ;
54import com .raysmond .blog .models .Post ;
65import com .raysmond .blog .models .User ;
76import com .raysmond .blog .models .support .PostFormat ;
87import com .raysmond .blog .models .support .PostStatus ;
98import com .raysmond .blog .repositories .PostRepository ;
109import com .raysmond .blog .repositories .UserRepository ;
11- import com .raysmond .blog .services .MarkdownService ;
1210import com .raysmond .blog .services .PostService ;
13- import org .slf4j .Logger ;
14- import org .slf4j .LoggerFactory ;
11+ import com .raysmond .blog .utils .DTOUtil ;
1512import org .springframework .beans .factory .annotation .Autowired ;
1613import org .springframework .data .domain .Page ;
1714import org .springframework .data .domain .PageRequest ;
1815import org .springframework .data .domain .Sort ;
1916import org .springframework .stereotype .Controller ;
2017import org .springframework .ui .Model ;
2118import org .springframework .validation .BindingResult ;
19+ import org .springframework .validation .Errors ;
2220import org .springframework .web .bind .annotation .PathVariable ;
2321import org .springframework .web .bind .annotation .RequestMapping ;
2422import org .springframework .web .bind .annotation .RequestMethod ;
2826import java .security .Principal ;
2927
3028/**
31- * Created by Raysmond on 9/25/15.
32- *
29+ * @author Raysmond<jiankunlei@gmail.com>
3330 * TODO place all view templates into sub dirs under /admin/
3431 */
3532@ Controller (value = "adminPostController" )
3633@ RequestMapping (value = "/admin/posts" )
3734public class PostController {
3835
3936 @ Autowired
40- private PostRepository posts ;
37+ private PostRepository postRepository ;
4138
4239 @ Autowired
4340 private PostService postService ;
@@ -49,62 +46,68 @@ public class PostController {
4946
5047 @ RequestMapping (value = "" )
5148 public String index (@ RequestParam (defaultValue = "0" ) int page , Model model ){
52- Page <Post > _posts = posts .findAll (new PageRequest (page , PAGE_SIZE , Sort .Direction .DESC , "id" ));
53- model .addAttribute ("totalPages" , _posts .getTotalPages ());
49+ Page <Post > posts = postRepository .findAll (new PageRequest (page , PAGE_SIZE , Sort .Direction .DESC , "id" ));
50+
51+ model .addAttribute ("totalPages" , posts .getTotalPages ());
5452 model .addAttribute ("page" , page );
55- model .addAttribute ("posts" , _posts );
53+ model .addAttribute ("posts" , posts );
54+
5655 return "admin/posts_index" ;
5756 }
5857
5958 @ RequestMapping (value = "new" )
6059 public String newPost (Model model ){
6160 model .addAttribute ("postForm" , new PostForm ());
6261 model .addAttribute ("postFormats" , PostFormat .values ());
62+
6363 return "admin/posts_new" ;
6464 }
6565
6666 @ RequestMapping (value = "{postId:[0-9]+}/edit" )
6767 public String editPost (@ PathVariable Long postId , Model model ){
68- Post post = posts .findOne (postId );
68+ Post post = postService .getPost (postId );
69+ PostForm postForm = DTOUtil .map (post , PostForm .class );
70+
6971 model .addAttribute ("post" , post );
70- model .addAttribute ("postForm" , new PostForm ( post ) );
72+ model .addAttribute ("postForm" , postForm );
7173 model .addAttribute ("postFormats" , PostFormat .values ());
74+
7275 return "admin/posts_edit" ;
7376 }
7477
7578 @ RequestMapping (value = "{postId:[0-9]+}/delete" , method = {RequestMethod .DELETE , RequestMethod .POST })
7679 public String deletePost (@ PathVariable Long postId ){
77- postService .deletePost (posts .findOne (postId ));
80+ postService .deletePost (postRepository .findOne (postId ));
7881 return "redirect:/admin/posts" ;
7982 }
8083
8184 @ RequestMapping (value = "" , method = RequestMethod .POST )
82- public String create (Principal principal , @ Valid PostForm postForm , BindingResult bindingResult , Model model ){
83- if (bindingResult .hasErrors ()) {
85+ public String create (Principal principal , @ Valid PostForm postForm , Errors errors , Model model ){
86+ if (errors .hasErrors ()) {
8487 model .addAttribute ("postFormats" , PostFormat .values ());
8588 return "admin/posts_new" ;
8689 } else {
8790 User user = users .findByEmail (principal .getName ());
88- Post post = postForm .toPost ();
91+ Post post = DTOUtil .map (postForm , Post .class );
92+ post .setUser (user );
8993
90- postService .createPost (user , post . getTitle (), post . getContent (), post . getPostFormat (), PostStatus . PUBLISHED );
94+ postService .createPost (post );
9195
9296 return "redirect:/admin/posts" ;
9397 }
9498 }
9599
96100 @ RequestMapping (value = "{postId:[0-9]+}" , method = {RequestMethod .PUT , RequestMethod .POST })
97- public String update (@ PathVariable Long postId , @ Valid PostForm postForm , BindingResult bindingResult , Model model ){
98- if (bindingResult .hasErrors ()){
101+ public String update (@ PathVariable Long postId , @ Valid PostForm postForm , Errors errors , Model model ){
102+ if (errors .hasErrors ()){
99103 model .addAttribute ("postFormats" , PostFormat .values ());
100104 return "admin/posts_edit" ;
101105 } else {
102- Post post = posts .findOne (postId );
103- post .setTitle (postForm .getTitle ());
104- post .setContent (postForm .getContent ());
105- post .setPostFormat (postForm .getPostFormat ());
106+ Post post = postService .getPost (postId );
106107
108+ DTOUtil .mapTo (postForm , post );
107109 postService .updatePost (post );
110+
108111 return "redirect:/admin/posts" ;
109112 }
110113 }
0 commit comments