66package controllers .admin ;
77
88import exceptions .PostNotFoundException ;
9+ import java .util .ArrayList ;
910import java .util .List ;
11+ import java .util .Locale ;
1012import javax .validation .Valid ;
1113import models .Post ;
1214import models .User ;
1315import org .slf4j .Logger ;
1416import org .slf4j .LoggerFactory ;
1517import org .springframework .beans .factory .annotation .Autowired ;
18+ import org .springframework .context .support .ReloadableResourceBundleMessageSource ;
1619import org .springframework .stereotype .Controller ;
1720import org .springframework .ui .Model ;
1821import org .springframework .validation .Errors ;
2124import org .springframework .web .bind .annotation .PathVariable ;
2225import org .springframework .web .bind .annotation .PostMapping ;
2326import org .springframework .web .bind .annotation .RequestMapping ;
27+ import org .springframework .web .servlet .mvc .support .RedirectAttributes ;
2428import services .PostService ;
2529import services .security .CurrentUser ;
2630import services .security .CurrentUserAttached ;
@@ -38,6 +42,8 @@ public class PostController {
3842
3943 @ Autowired
4044 private PostService postService ;
45+ @ Autowired
46+ private ReloadableResourceBundleMessageSource messageSource ;
4147
4248 @ GetMapping ("/all" )
4349 public String all (@ CurrentUser User activeUser , Model model ){
@@ -68,8 +74,11 @@ public String showDeletePostForm(@PathVariable Long postId, Model model) {
6874 }
6975
7076 @ PostMapping ("/delete" )
71- public String processDelete (@ ModelAttribute Post post , Model model ) {
77+ public String processDelete (@ ModelAttribute Post post , RedirectAttributes model ) {
7278 postService .delete (post );
79+ List <String > successMessages = new ArrayList ();
80+ successMessages .add (messageSource .getMessage ("message.post.remove.success" , new Object [] {}, Locale .getDefault ()));
81+ model .addFlashAttribute ("successFlashMessages" , successMessages );
7382 return "redirect:/admin/posts/all" ;
7483 }
7584
@@ -80,12 +89,16 @@ public String showCreatePostForm(Model model){
8089 }
8190
8291 @ PostMapping ("/save" )
83- public String processPost (@ ModelAttribute @ Valid Post post , Errors errors , @ CurrentUserAttached User activeUser ){
92+ public String processPost (@ ModelAttribute @ Valid Post post , Errors errors ,
93+ @ CurrentUserAttached User activeUser , RedirectAttributes model ){
8494 if (errors .hasErrors ()){
8595 return "admin/post/create" ;
8696 }
8797 post .setAuthor (activeUser );
8898 postService .create (post );
99+ List <String > successMessages = new ArrayList ();
100+ successMessages .add (messageSource .getMessage ("message.post.save.success" , new Object [] {post .getId ()}, Locale .getDefault ()));
101+ model .addFlashAttribute ("successFlashMessages" , successMessages );
89102 return "redirect:/admin/posts/all" ;
90103 }
91104}
0 commit comments