Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 90fd375

Browse filesBrowse files
committed
Enable and display post tags
1 parent 18e6266 commit 90fd375
Copy full SHA for 90fd375

File tree

Expand file treeCollapse file tree

5 files changed

+39
-7
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+39
-7
lines changed
Open diff view settings
Collapse file

‎src/main/java/com/raysmond/blog/admin/controllers/PostController.java‎

Copy file name to clipboardExpand all lines: src/main/java/com/raysmond/blog/admin/controllers/PostController.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public String update(@PathVariable Long postId, @Valid PostForm postForm, Errors
121121

122122
return "admin/posts_edit";
123123
} else {
124-
Post post = postService.getPost(postId);
124+
Post post = postRepository.findOne(postId);
125125
DTOUtil.mapTo(postForm, post);
126126
post.setTags(postService.parseTagNames(postForm.getPostTags()));
127127

Collapse file

‎src/main/java/com/raysmond/blog/controllers/PostController.java‎

Copy file name to clipboardExpand all lines: src/main/java/com/raysmond/blog/controllers/PostController.java
+1-3Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ public class PostController {
2525
@Autowired
2626
private PostService postService;
2727

28-
@Autowired
29-
private TagService tagService;
30-
3128
@RequestMapping(value = "archive", method = GET)
3229
public String archive(Model model){
3330
model.addAttribute("posts", postService.getArchivePosts());
@@ -50,6 +47,7 @@ public String show(@PathVariable String permalink, Model model){
5047
throw new NotFoundException("Post with permalink " + permalink + " is not found");
5148

5249
model.addAttribute("post", post);
50+
model.addAttribute("tags", postService.getPostTags(post));
5351

5452
return "posts/show";
5553
}
Collapse file

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

Copy file name to clipboardExpand all lines: src/main/java/com/raysmond/blog/services/PostService.java
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class PostService {
4242
public static final String CACHE_NAME = "cache.post";
4343
public static final String CACHE_NAME_ARCHIVE = CACHE_NAME + ".archive";
4444
public static final String CACHE_NAME_PAGE = CACHE_NAME + ".page";
45+
public static final String CACHE_NAME_TAGS = CACHE_NAME + ".tag";
4546

4647
private static final Logger logger = LoggerFactory.getLogger(PostService.class);
4748

@@ -61,6 +62,7 @@ public Post getPost(Long postId) {
6162
@Cacheable(CACHE_NAME)
6263
public Post getPublishedPostByPermalink(String permalink){
6364
logger.debug("Get post with permalink " + permalink);
65+
6466
Post post = postRepository.findByPermalinkAndPostStatus(permalink, PostStatus.PUBLISHED);
6567

6668
if (post == null){
@@ -85,6 +87,7 @@ public Post createPost(Post post) {
8587
@Caching(evict = {
8688
@CacheEvict(value = CACHE_NAME, key = "#post.id"),
8789
@CacheEvict(value = CACHE_NAME, key = "#post.permalink", condition = "#post.permalink != null"),
90+
@CacheEvict(value = CACHE_NAME_TAGS, key = "#post.id"),
8891
@CacheEvict(value = CACHE_NAME_ARCHIVE, allEntries = true),
8992
@CacheEvict(value = CACHE_NAME_PAGE, allEntries = true)
9093
})
@@ -99,6 +102,7 @@ public Post updatePost(Post post) {
99102
@Caching(evict = {
100103
@CacheEvict(value = CACHE_NAME, key = "#post.id"),
101104
@CacheEvict(value = CACHE_NAME, key = "#post.permalink", condition = "#post.permalink != null"),
105+
@CacheEvict(value = CACHE_NAME_TAGS, key = "#post.id"),
102106
@CacheEvict(value = CACHE_NAME_ARCHIVE, allEntries = true),
103107
@CacheEvict(value = CACHE_NAME_PAGE, allEntries = true)
104108
})
@@ -121,6 +125,20 @@ public List<Post> getArchivePosts() {
121125
return cachedPosts;
122126
}
123127

128+
@Cacheable(value = CACHE_NAME_TAGS, key = "#post.id")
129+
public List<Tag> getPostTags(Post post){
130+
logger.debug("Get tags of post " + post.getId());
131+
132+
List<Tag> tags = new ArrayList<>();
133+
134+
// Load the post first. If not, when the post is cached before while the tags not,
135+
// then the LAZY loading of post tags will cause an initialization error because
136+
// of not hibernate connection session
137+
postRepository.findOne(post.getId()).getTags().forEach(tags::add);
138+
return tags;
139+
}
140+
141+
124142
private Post extractPostMeta(Post post){
125143
Post archivePost = new Post();
126144
archivePost.setId(post.getId());
Collapse file

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

Copy file name to clipboardExpand all lines: src/main/resources/resources/css/theme.css
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,17 @@ form input, form button{
124124
border-top: 1px #eee solid;
125125
}
126126

127+
.post ul.tags{
128+
margin-left: 0;
129+
padding-left: 0;
130+
}
131+
132+
.post ul.tags li{
133+
display: inline-block;
134+
list-style: none;
135+
margin-left: 10px;
136+
}
137+
127138
.footer{
128139
text-align: center;
129140
margin-bottom: 50px;
Collapse file

‎src/main/resources/templates/posts/show.jade‎

Copy file name to clipboardExpand all lines: src/main/resources/templates/posts/show.jade
+8-3Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@ block content
1313
!{post.getRenderedContent()}
1414

1515
.info
16+
if !tags.isEmpty()
17+
ul.tags
18+
li Tags:
19+
for tag in tags
20+
//- li: a(href="#") #{tag.getName()}
21+
li #{tag.getName()}
22+
1623
.author
1724
#{post.getCreatedAt() == post.getUpdatedAt() ? "Created" : "Updated"}
1825
| at #{viewHelper.getFormattedDate(post.getCreatedAt())}
1926

20-
//- ul.tags
21-
//- for tag in tags
22-
//- li: a(href="#") #{tag.getName()}
27+
2328

2429

2530
.comments

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.