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

Enable and display post tags

上级 18e6266c
......@@ -121,7 +121,7 @@ public class PostController {
return "admin/posts_edit";
} else {
Post post = postService.getPost(postId);
Post post = postRepository.findOne(postId);
DTOUtil.mapTo(postForm, post);
post.setTags(postService.parseTagNames(postForm.getPostTags()));
......
......@@ -25,9 +25,6 @@ public class PostController {
@Autowired
private PostService postService;
@Autowired
private TagService tagService;
@RequestMapping(value = "archive", method = GET)
public String archive(Model model){
model.addAttribute("posts", postService.getArchivePosts());
......@@ -50,6 +47,7 @@ public class PostController {
throw new NotFoundException("Post with permalink " + permalink + " is not found");
model.addAttribute("post", post);
model.addAttribute("tags", postService.getPostTags(post));
return "posts/show";
}
......
......@@ -42,6 +42,7 @@ public class PostService {
public static final String CACHE_NAME = "cache.post";
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_TAGS = CACHE_NAME + ".tag";
private static final Logger logger = LoggerFactory.getLogger(PostService.class);
......@@ -61,6 +62,7 @@ public class PostService {
@Cacheable(CACHE_NAME)
public Post getPublishedPostByPermalink(String permalink){
logger.debug("Get post with permalink " + permalink);
Post post = postRepository.findByPermalinkAndPostStatus(permalink, PostStatus.PUBLISHED);
if (post == null){
......@@ -85,6 +87,7 @@ public class PostService {
@Caching(evict = {
@CacheEvict(value = CACHE_NAME, key = "#post.id"),
@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_PAGE, allEntries = true)
})
......@@ -99,6 +102,7 @@ public class PostService {
@Caching(evict = {
@CacheEvict(value = CACHE_NAME, key = "#post.id"),
@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_PAGE, allEntries = true)
})
......@@ -121,6 +125,20 @@ public class PostService {
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){
Post archivePost = new Post();
archivePost.setId(post.getId());
......
......@@ -124,6 +124,17 @@ form input, form button{
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{
text-align: center;
margin-bottom: 50px;
......
......@@ -13,13 +13,18 @@ block content
!{post.getRenderedContent()}
.info
if !tags.isEmpty()
ul.tags
li Tags:
for tag in tags
//- li: a(href="#") #{tag.getName()}
li #{tag.getName()}
.author
#{post.getCreatedAt() == post.getUpdatedAt() ? "Created" : "Updated"}
| at #{viewHelper.getFormattedDate(post.getCreatedAt())}
//- ul.tags
//- for tag in tags
//- li: a(href="#") #{tag.getName()}
.comments
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册