11package com .raysmond .blog .controllers ;
22
3+ import com .raysmond .blog .error .NotFoundException ;
4+ import com .raysmond .blog .models .Post ;
35import com .raysmond .blog .models .Tag ;
6+ import com .raysmond .blog .services .AppSetting ;
7+ import com .raysmond .blog .services .PostService ;
48import com .raysmond .blog .services .TagService ;
59import org .springframework .beans .factory .annotation .Autowired ;
10+ import org .springframework .data .domain .Page ;
611import org .springframework .stereotype .Controller ;
712import org .springframework .ui .Model ;
813import org .springframework .web .bind .annotation .PathVariable ;
914import org .springframework .web .bind .annotation .RequestMapping ;
15+ import static org .springframework .web .bind .annotation .RequestMethod .*;
16+ import org .springframework .web .bind .annotation .RequestParam ;
17+
18+ import java .util .ArrayList ;
19+ import java .util .List ;
1020
1121/**
1222 * @author Raysmond<i@raysmond.com>.
@@ -17,9 +27,35 @@ public class TagController {
1727 @ Autowired
1828 private TagService tagService ;
1929
20- // @RequestMapping("{tagName}")
21- // public String showTag(@PathVariable String tagName, Model model){
22- // Tag tag = tagService.getTag(tagName);
23- // return "tags/show";
24- // }
30+ @ Autowired
31+ private PostService postService ;
32+
33+ @ Autowired
34+ private AppSetting appSetting ;
35+
36+ @ RequestMapping (value = "" , method = GET )
37+ public String index (){
38+ // TODO show all tags
39+
40+ return "tags/index" ;
41+ }
42+
43+ @ RequestMapping (value = "{tagName}" , method = GET )
44+ public String showTag (@ PathVariable String tagName , @ RequestParam (defaultValue = "1" ) int page , Model model ) {
45+ Tag tag = tagService .getTag (tagName );
46+
47+ if (tag == null ) {
48+ throw new NotFoundException ("Tag " + tagName + " is not found." );
49+ }
50+
51+ page = page < 1 ? 0 : page - 1 ;
52+ Page <Post > posts = postService .findPostsByTag (tagName , page , appSetting .getPageSize ());
53+
54+ model .addAttribute ("tag" , tag );
55+ model .addAttribute ("posts" , posts );
56+ model .addAttribute ("page" , page + 1 );
57+ model .addAttribute ("totalPages" , posts .getTotalPages ());
58+
59+ return "tags/show" ;
60+ }
2561}
0 commit comments