22
33import alexp .blog .model .Comment ;
44import alexp .blog .model .Post ;
5+ import alexp .blog .model .PostEditDto ;
56import alexp .blog .service .PostService ;
67import alexp .blog .service .UserService ;
78import org .springframework .beans .factory .annotation .Autowired ;
@@ -32,13 +33,13 @@ public String showPostsList(@RequestParam(value = "page", defaultValue = "0") In
3233 }
3334
3435 @ RequestMapping (value = "/tag" , method = RequestMethod .GET )
35- public @ ResponseBody String searchByTag (@ RequestParam (value = "name" ) String tagName , ModelMap model ) {
36+ public @ ResponseBody String searchByTag (@ RequestParam ("name" ) String tagName , ModelMap model ) {
3637
3738 return "search by tag: TODO" ;
3839 }
3940
4041 @ RequestMapping (value = "/post" , method = RequestMethod .GET )
41- public String showPost (@ RequestParam (value = "id" ) Long postId , ModelMap model ) {
42+ public String showPost (@ RequestParam ("id" ) Long postId , ModelMap model ) {
4243 Post post = postService .getPost (postId );
4344
4445 if (post == null )
@@ -56,33 +57,56 @@ public String showPost(@RequestParam(value = "id") Long postId, ModelMap model)
5657 @ PreAuthorize ("hasRole('ROLE_ADMIN')" )
5758 @ RequestMapping (value = "/post/create" , method = RequestMethod .GET )
5859 public String showCreatePostForm (ModelMap model ) {
59- model .addAttribute ("post" , new Post ());
60+ model .addAttribute ("post" , new PostEditDto ());
6061
6162 model .addAttribute ("edit" , false );
6263
63- return "createpost " ;
64+ return "editpost " ;
6465 }
6566
6667 @ PreAuthorize ("hasRole('ROLE_ADMIN')" )
6768 @ RequestMapping (value = "/post/create" , method = RequestMethod .POST )
68- public String createPost (ModelMap model , @ Valid @ ModelAttribute (value = "post" ) Post post , BindingResult result ,
69- @ RequestParam (value = "tagstext" , defaultValue = "" ) String tagstext ) {
69+ public String createPost (ModelMap model , @ Valid @ ModelAttribute ("post" ) PostEditDto post , BindingResult result ) {
7070 if (result .hasErrors ()) {
7171 model .addAttribute ("edit" , false );
7272
73- return "createpost " ;
73+ return "editpost " ;
7474 }
7575
76- postService .saveNewPost (post , tagstext );
76+ postService .saveNewPost (post );
7777
7878 return "redirect:/posts" ;
7979 }
8080
8181 @ PreAuthorize ("hasRole('ROLE_ADMIN')" )
8282 @ RequestMapping (value = "/post/edit" , method = RequestMethod .GET )
83- public String showEditPostForm (ModelMap model ) {
83+ public String showEditPostForm (@ RequestParam ("id" ) Long postId , ModelMap model ) {
84+ PostEditDto post = postService .getEditablePost (postId );
85+
86+ if (post == null )
87+ throw new ResourceNotFoundException ();
88+
89+ model .addAttribute ("post" , post );
90+
8491 model .addAttribute ("edit" , true );
8592
86- return "createpost" ;
93+ return "editpost" ;
94+ }
95+
96+ @ PreAuthorize ("hasRole('ROLE_ADMIN')" )
97+ @ RequestMapping (value = "/post/edit" , method = RequestMethod .POST )
98+ public String showEditPostForm (ModelMap model , @ Valid @ ModelAttribute ("post" ) PostEditDto post , BindingResult result ,
99+ @ RequestParam ("id" ) Long postId ) {
100+ post .setId (postId );
101+
102+ if (result .hasErrors ()) {
103+ model .addAttribute ("edit" , true );
104+
105+ return "editpost" ;
106+ }
107+
108+ postService .updatePost (post );
109+
110+ return "redirect:/post?id=" + postId ;
87111 }
88112}
0 commit comments