Skip to content

Commit 19fcbea

Browse files
committed
tags counting page
1 parent 0f71931 commit 19fcbea

File tree

7 files changed

+51
-8
lines changed

7 files changed

+51
-8
lines changed

src/main/java/com/raysmond/blog/controllers/TagController.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.raysmond.blog.error.NotFoundException;
44
import com.raysmond.blog.models.Post;
55
import com.raysmond.blog.models.Tag;
6+
import com.raysmond.blog.repositories.PostRepository;
7+
import com.raysmond.blog.repositories.TagRepository;
68
import com.raysmond.blog.services.AppSetting;
79
import com.raysmond.blog.services.PostService;
810
import com.raysmond.blog.services.TagService;
@@ -15,6 +17,9 @@
1517
import static org.springframework.web.bind.annotation.RequestMethod.*;
1618
import org.springframework.web.bind.annotation.RequestParam;
1719

20+
import java.util.List;
21+
import java.util.Map;
22+
1823
/**
1924
* @author Raysmond<i@raysmond.com>.
2025
*/
@@ -31,9 +36,13 @@ public class TagController {
3136
private AppSetting appSetting;
3237

3338
@RequestMapping(value = "", method = GET)
34-
public String index(){
39+
public String index(Model model){
3540
// TODO show all tags
3641

42+
List<Map<String, Long>> counts = postService.countPostsByTags();
43+
44+
model.addAttribute("tags", counts);
45+
3746
return "tags/index";
3847
}
3948

src/main/java/com/raysmond/blog/repositories/PostRepository.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
import org.springframework.data.domain.Pageable;
88
import org.springframework.data.jpa.repository.JpaRepository;
99
import org.springframework.data.jpa.repository.Query;
10-
import org.springframework.data.repository.CrudRepository;
11-
import org.springframework.data.repository.PagingAndSortingRepository;
1210
import org.springframework.data.repository.query.Param;
1311
import org.springframework.stereotype.Repository;
1412
import org.springframework.transaction.annotation.Transactional;
13+
import java.util.Map;
1514

1615
import java.util.List;
1716

@@ -29,5 +28,8 @@ public interface PostRepository extends JpaRepository<Post, Long> {
2928

3029
@Query("SELECT p FROM Post p INNER JOIN p.tags t WHERE t.name = :tag")
3130
Page<Post> findByTag(@Param("tag") String tag, Pageable pageable);
31+
32+
@Query("SELECT t.name, count(p) as count from Post p INNER JOIN p.tags t GROUP BY t.id")
33+
List<Map<String, Long>> countPostsByTags();
3234
}
3335

src/main/java/com/raysmond/blog/services/PostService.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class PostService {
4040
public static final String CACHE_NAME_ARCHIVE = CACHE_NAME + ".archive";
4141
public static final String CACHE_NAME_PAGE = CACHE_NAME + ".page";
4242
public static final String CACHE_NAME_TAGS = CACHE_NAME + ".tag";
43+
public static final String CACHE_NAME_COUNTS = CACHE_NAME + ".counts_tags";
4344

4445
private static final Logger logger = LoggerFactory.getLogger(PostService.class);
4546

@@ -71,7 +72,8 @@ public Post getPublishedPostByPermalink(String permalink){
7172

7273
@Caching(evict = {
7374
@CacheEvict(value = CACHE_NAME_ARCHIVE, allEntries = true),
74-
@CacheEvict(value = CACHE_NAME_PAGE, allEntries = true)
75+
@CacheEvict(value = CACHE_NAME_PAGE, allEntries = true),
76+
@CacheEvict(value = CACHE_NAME_COUNTS, allEntries = true)
7577
})
7678
public Post createPost(Post post) {
7779
if (post.getPostFormat() == PostFormat.MARKDOWN) {
@@ -86,7 +88,8 @@ public Post createPost(Post post) {
8688
@CacheEvict(value = CACHE_NAME, key = "#post.permalink", condition = "#post.permalink != null"),
8789
@CacheEvict(value = CACHE_NAME_TAGS, key = "#post.id.toString().concat('-tags')"),
8890
@CacheEvict(value = CACHE_NAME_ARCHIVE, allEntries = true),
89-
@CacheEvict(value = CACHE_NAME_PAGE, allEntries = true)
91+
@CacheEvict(value = CACHE_NAME_PAGE, allEntries = true),
92+
@CacheEvict(value = CACHE_NAME_COUNTS, allEntries = true)
9093
})
9194
public Post updatePost(Post post) {
9295
if (post.getPostFormat() == PostFormat.MARKDOWN) {
@@ -101,7 +104,8 @@ public Post updatePost(Post post) {
101104
@CacheEvict(value = CACHE_NAME, key = "#post.permalink", condition = "#post.permalink != null"),
102105
@CacheEvict(value = CACHE_NAME_TAGS, key = "#post.id.toString().concat('-tags')"),
103106
@CacheEvict(value = CACHE_NAME_ARCHIVE, allEntries = true),
104-
@CacheEvict(value = CACHE_NAME_PAGE, allEntries = true)
107+
@CacheEvict(value = CACHE_NAME_PAGE, allEntries = true),
108+
@CacheEvict(value = CACHE_NAME_COUNTS, allEntries = true)
105109
})
106110
public void deletePost(Post post) {
107111
postRepository.delete(post);
@@ -173,6 +177,7 @@ public Set<Tag> parseTagNames(String tagNames){
173177
Set<Tag> tags = new HashSet<>();
174178

175179
if (tagNames != null && !tagNames.isEmpty()){
180+
tagNames = tagNames.toLowerCase();
176181
String[] names = tagNames.split("\\s*,\\s*");
177182
for (String name : names){
178183
tags.add(tagService.findOrCreateByName(name));
@@ -193,7 +198,15 @@ public String getTagNames(Set<Tag> tags){
193198
return names.toString();
194199
}
195200

201+
// cache or not?
196202
public Page<Post> findPostsByTag(String tagName, int page, int pageSize){
197203
return postRepository.findByTag(tagName, new PageRequest(page, pageSize, Sort.Direction.DESC, "createdAt"));
198204
}
205+
206+
@Cacheable(value = CACHE_NAME_COUNTS, key = "#root.method.name")
207+
public List<Map<String, Long>> countPostsByTags(){
208+
logger.debug("Count posts group by tags.");
209+
210+
return postRepository.countPostsByTags();
211+
}
199212
}

src/main/resources/resources/css/theme.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ body {
1313

1414
/* override typo css */
1515
.typo a{
16-
border-bottom: none;
1716
color: #337ab7;
1817
}
1918

@@ -166,6 +165,10 @@ ul.archive-list li span.month{
166165
display: inline-block;
167166
}
168167

168+
ul.tags li{
169+
margin: 5px 10px;
170+
}
171+
169172
.footer{
170173
text-align: center;
171174
margin-bottom: 50px;

src/main/resources/resources/vendors/typo.css-2.1.2/typo.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/resources/templates/layout/navbar.jade

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ nav.navbar.navbar-bright
66
li.active
77
a(href="/") Home
88
li: a(href="/posts/archive") Archive
9+
li: a(href="/tags") Tags
910
li: a(href="/about") About
1011

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
extends ../layout/app
2+
3+
block title
4+
| Tags
5+
6+
block page_title
7+
h1 Tags
8+
9+
block content
10+
.post
11+
.content
12+
ul.tags
13+
for tag in tags
14+
li: a.btn.btn-default(href="/tags/#{tag[0]}") #{tag[0]}(#{tag[1]})
15+
hr

0 commit comments

Comments
 (0)