提交 90fd3753 编写于 作者: R Raysmond

Enable and display post tags

上级 18e6266c
...@@ -121,7 +121,7 @@ public class PostController { ...@@ -121,7 +121,7 @@ public class PostController {
return "admin/posts_edit"; return "admin/posts_edit";
} else { } else {
Post post = postService.getPost(postId); Post post = postRepository.findOne(postId);
DTOUtil.mapTo(postForm, post); DTOUtil.mapTo(postForm, post);
post.setTags(postService.parseTagNames(postForm.getPostTags())); post.setTags(postService.parseTagNames(postForm.getPostTags()));
......
...@@ -25,9 +25,6 @@ public class PostController { ...@@ -25,9 +25,6 @@ public class PostController {
@Autowired @Autowired
private PostService postService; private PostService postService;
@Autowired
private TagService tagService;
@RequestMapping(value = "archive", method = GET) @RequestMapping(value = "archive", method = GET)
public String archive(Model model){ public String archive(Model model){
model.addAttribute("posts", postService.getArchivePosts()); model.addAttribute("posts", postService.getArchivePosts());
...@@ -50,6 +47,7 @@ public class PostController { ...@@ -50,6 +47,7 @@ public class PostController {
throw new NotFoundException("Post with permalink " + permalink + " is not found"); throw new NotFoundException("Post with permalink " + permalink + " is not found");
model.addAttribute("post", post); model.addAttribute("post", post);
model.addAttribute("tags", postService.getPostTags(post));
return "posts/show"; return "posts/show";
} }
......
...@@ -42,6 +42,7 @@ public class PostService { ...@@ -42,6 +42,7 @@ public class PostService {
public static final String CACHE_NAME = "cache.post"; public static final String CACHE_NAME = "cache.post";
public static final String CACHE_NAME_ARCHIVE = CACHE_NAME + ".archive"; public static final String CACHE_NAME_ARCHIVE = CACHE_NAME + ".archive";
public static final String CACHE_NAME_PAGE = CACHE_NAME + ".page"; public static final String CACHE_NAME_PAGE = CACHE_NAME + ".page";
public static final String CACHE_NAME_TAGS = CACHE_NAME + ".tag";
private static final Logger logger = LoggerFactory.getLogger(PostService.class); private static final Logger logger = LoggerFactory.getLogger(PostService.class);
...@@ -61,6 +62,7 @@ public class PostService { ...@@ -61,6 +62,7 @@ public class PostService {
@Cacheable(CACHE_NAME) @Cacheable(CACHE_NAME)
public Post getPublishedPostByPermalink(String permalink){ public Post getPublishedPostByPermalink(String permalink){
logger.debug("Get post with permalink " + permalink); logger.debug("Get post with permalink " + permalink);
Post post = postRepository.findByPermalinkAndPostStatus(permalink, PostStatus.PUBLISHED); Post post = postRepository.findByPermalinkAndPostStatus(permalink, PostStatus.PUBLISHED);
if (post == null){ if (post == null){
...@@ -85,6 +87,7 @@ public class PostService { ...@@ -85,6 +87,7 @@ public class PostService {
@Caching(evict = { @Caching(evict = {
@CacheEvict(value = CACHE_NAME, key = "#post.id"), @CacheEvict(value = CACHE_NAME, key = "#post.id"),
@CacheEvict(value = CACHE_NAME, key = "#post.permalink", condition = "#post.permalink != null"), @CacheEvict(value = CACHE_NAME, key = "#post.permalink", condition = "#post.permalink != null"),
@CacheEvict(value = CACHE_NAME_TAGS, key = "#post.id"),
@CacheEvict(value = CACHE_NAME_ARCHIVE, allEntries = true), @CacheEvict(value = CACHE_NAME_ARCHIVE, allEntries = true),
@CacheEvict(value = CACHE_NAME_PAGE, allEntries = true) @CacheEvict(value = CACHE_NAME_PAGE, allEntries = true)
}) })
...@@ -99,6 +102,7 @@ public class PostService { ...@@ -99,6 +102,7 @@ public class PostService {
@Caching(evict = { @Caching(evict = {
@CacheEvict(value = CACHE_NAME, key = "#post.id"), @CacheEvict(value = CACHE_NAME, key = "#post.id"),
@CacheEvict(value = CACHE_NAME, key = "#post.permalink", condition = "#post.permalink != null"), @CacheEvict(value = CACHE_NAME, key = "#post.permalink", condition = "#post.permalink != null"),
@CacheEvict(value = CACHE_NAME_TAGS, key = "#post.id"),
@CacheEvict(value = CACHE_NAME_ARCHIVE, allEntries = true), @CacheEvict(value = CACHE_NAME_ARCHIVE, allEntries = true),
@CacheEvict(value = CACHE_NAME_PAGE, allEntries = true) @CacheEvict(value = CACHE_NAME_PAGE, allEntries = true)
}) })
...@@ -121,6 +125,20 @@ public class PostService { ...@@ -121,6 +125,20 @@ public class PostService {
return cachedPosts; return cachedPosts;
} }
@Cacheable(value = CACHE_NAME_TAGS, key = "#post.id")
public List<Tag> getPostTags(Post post){
logger.debug("Get tags of post " + post.getId());
List<Tag> tags = new ArrayList<>();
// Load the post first. If not, when the post is cached before while the tags not,
// then the LAZY loading of post tags will cause an initialization error because
// of not hibernate connection session
postRepository.findOne(post.getId()).getTags().forEach(tags::add);
return tags;
}
private Post extractPostMeta(Post post){ private Post extractPostMeta(Post post){
Post archivePost = new Post(); Post archivePost = new Post();
archivePost.setId(post.getId()); archivePost.setId(post.getId());
......
...@@ -124,6 +124,17 @@ form input, form button{ ...@@ -124,6 +124,17 @@ form input, form button{
border-top: 1px #eee solid; border-top: 1px #eee solid;
} }
.post ul.tags{
margin-left: 0;
padding-left: 0;
}
.post ul.tags li{
display: inline-block;
list-style: none;
margin-left: 10px;
}
.footer{ .footer{
text-align: center; text-align: center;
margin-bottom: 50px; margin-bottom: 50px;
......
...@@ -13,13 +13,18 @@ block content ...@@ -13,13 +13,18 @@ block content
!{post.getRenderedContent()} !{post.getRenderedContent()}
.info .info
if !tags.isEmpty()
ul.tags
li Tags:
for tag in tags
//- li: a(href="#") #{tag.getName()}
li #{tag.getName()}
.author .author
#{post.getCreatedAt() == post.getUpdatedAt() ? "Created" : "Updated"} #{post.getCreatedAt() == post.getUpdatedAt() ? "Created" : "Updated"}
| at #{viewHelper.getFormattedDate(post.getCreatedAt())} | at #{viewHelper.getFormattedDate(post.getCreatedAt())}
//- ul.tags
//- for tag in tags
//- li: a(href="#") #{tag.getName()}
.comments .comments
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册