Search in sources :

Example 1 with PostMeta

use of run.halo.app.model.entity.PostMeta in project halo by halo-dev.

the class PostServiceImpl method createOrUpdate.

private PostDetailVO createOrUpdate(@NonNull Post post, Set<Integer> tagIds, Set<Integer> categoryIds, Set<PostMeta> metas) {
    Assert.notNull(post, "Post param must not be null");
    post = super.createOrUpdateBy(post);
    postTagService.removeByPostId(post.getId());
    postCategoryService.removeByPostId(post.getId());
    // List all tags
    List<Tag> tags = tagService.listAllByIds(tagIds);
    // List all categories
    List<Category> categories = categoryService.listAllByIds(categoryIds);
    // Create post tags
    List<PostTag> postTags = postTagService.mergeOrCreateByIfAbsent(post.getId(), ServiceUtils.fetchProperty(tags, Tag::getId));
    log.debug("Created post tags: [{}]", postTags);
    // Create post categories
    List<PostCategory> postCategories = postCategoryService.mergeOrCreateByIfAbsent(post.getId(), ServiceUtils.fetchProperty(categories, Category::getId));
    log.debug("Created post categories: [{}]", postCategories);
    // Create post meta data
    List<PostMeta> postMetaList = postMetaService.createOrUpdateByPostId(post.getId(), metas);
    log.debug("Created post metas: [{}]", postMetaList);
    // Publish post updated event.
    applicationContext.publishEvent(new PostUpdatedEvent(this, post));
    // get draft content by head patch log id
    Content postContent = postContentService.getById(post.getId());
    post.setContent(postContentPatchLogService.getPatchedContentById(postContent.getHeadPatchLogId()));
    // Convert to post detail vo
    return postAssembler.convertTo(post, tags, categories, postMetaList);
}
Also used : PostCategory(run.halo.app.model.entity.PostCategory) Category(run.halo.app.model.entity.Category) PostMeta(run.halo.app.model.entity.PostMeta) PostCategory(run.halo.app.model.entity.PostCategory) Content(run.halo.app.model.entity.Content) PatchedContent(run.halo.app.model.entity.Content.PatchedContent) PostUpdatedEvent(run.halo.app.event.post.PostUpdatedEvent) PostTag(run.halo.app.model.entity.PostTag) Tag(run.halo.app.model.entity.Tag) PostTag(run.halo.app.model.entity.PostTag)

Example 2 with PostMeta

use of run.halo.app.model.entity.PostMeta in project halo by halo-dev.

the class PostServiceImpl method removeById.

@Override
@Transactional(rollbackFor = Exception.class)
public Post removeById(Integer postId) {
    Assert.notNull(postId, "Post id must not be null");
    log.debug("Removing post: [{}]", postId);
    // Remove post tags
    List<PostTag> postTags = postTagService.removeByPostId(postId);
    log.debug("Removed post tags: [{}]", postTags);
    // Remove post categories
    List<PostCategory> postCategories = postCategoryService.removeByPostId(postId);
    log.debug("Removed post categories: [{}]", postCategories);
    // Remove metas
    List<PostMeta> metas = postMetaService.removeByPostId(postId);
    log.debug("Removed post metas: [{}]", metas);
    // Remove post comments
    List<PostComment> postComments = postCommentService.removeByPostId(postId);
    log.debug("Removed post comments: [{}]", postComments);
    // Remove post content
    Content postContent = postContentService.removeById(postId);
    log.debug("Removed post content: [{}]", postContent);
    Post deletedPost = super.removeById(postId);
    deletedPost.setContent(PatchedContent.of(postContent));
    // Log it
    eventPublisher.publishEvent(new LogEvent(this, postId.toString(), LogType.POST_DELETED, deletedPost.getTitle()));
    return deletedPost;
}
Also used : PostMeta(run.halo.app.model.entity.PostMeta) PostCategory(run.halo.app.model.entity.PostCategory) LogEvent(run.halo.app.event.logger.LogEvent) Content(run.halo.app.model.entity.Content) PatchedContent(run.halo.app.model.entity.Content.PatchedContent) Post(run.halo.app.model.entity.Post) PostComment(run.halo.app.model.entity.PostComment) PostTag(run.halo.app.model.entity.PostTag) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with PostMeta

use of run.halo.app.model.entity.PostMeta in project halo by halo-dev.

the class PostServiceImpl method exportMarkdown.

@Override
public String exportMarkdown(Post post) {
    Assert.notNull(post, "Post must not be null");
    StringBuilder content = new StringBuilder("---\n");
    content.append("type: ").append("post").append("\n");
    content.append("title: ").append(post.getTitle()).append("\n");
    content.append("permalink: ").append(post.getSlug()).append("\n");
    content.append("thumbnail: ").append(post.getThumbnail()).append("\n");
    content.append("status: ").append(post.getStatus()).append("\n");
    content.append("date: ").append(post.getCreateTime()).append("\n");
    content.append("updated: ").append(post.getEditTime()).append("\n");
    content.append("comments: ").append(!post.getDisallowComment()).append("\n");
    List<Tag> tags = postTagService.listTagsBy(post.getId());
    if (tags.size() > 0) {
        content.append("tags:").append("\n");
        for (Tag tag : tags) {
            content.append("  - ").append(tag.getName()).append("\n");
        }
    }
    List<Category> categories = postCategoryService.listCategoriesBy(post.getId());
    if (categories.size() > 0) {
        content.append("categories:").append("\n");
        for (Category category : categories) {
            content.append("  - ").append(category.getName()).append("\n");
        }
    }
    List<PostMeta> metas = postMetaService.listBy(post.getId());
    if (metas.size() > 0) {
        content.append("metas:").append("\n");
        for (PostMeta postMeta : metas) {
            content.append("  - ").append(postMeta.getKey()).append(" :  ").append(postMeta.getValue()).append("\n");
        }
    }
    content.append("---\n\n");
    PatchedContent postContent = post.getContent();
    content.append(postContent.getOriginalContent());
    return content.toString();
}
Also used : PostCategory(run.halo.app.model.entity.PostCategory) Category(run.halo.app.model.entity.Category) PostMeta(run.halo.app.model.entity.PostMeta) PostTag(run.halo.app.model.entity.PostTag) Tag(run.halo.app.model.entity.Tag) PatchedContent(run.halo.app.model.entity.Content.PatchedContent)

Example 4 with PostMeta

use of run.halo.app.model.entity.PostMeta in project halo by halo-dev.

the class PostAssembler method convertToListVo.

/**
 * Converts to a page of post list vo.
 *
 * @param postPage post page must not be null
 * @return a page of post list vo
 */
@NonNull
public Page<PostListVO> convertToListVo(Page<Post> postPage) {
    Assert.notNull(postPage, "Post page must not be null");
    List<Post> posts = postPage.getContent();
    Set<Integer> postIds = ServiceUtils.fetchProperty(posts, Post::getId);
    // Get tag list map
    Map<Integer, List<Tag>> tagListMap = postTagService.listTagListMapBy(postIds);
    // Get category list map
    Map<Integer, List<Category>> categoryListMap = postCategoryService.listCategoryListMap(postIds);
    // Get comment count
    Map<Integer, Long> commentCountMap = postCommentService.countByStatusAndPostIds(CommentStatus.PUBLISHED, postIds);
    // Get post meta list map
    Map<Integer, List<PostMeta>> postMetaListMap = postMetaService.listPostMetaAsMap(postIds);
    return postPage.map(post -> {
        PostListVO postListVO = new PostListVO().convertFrom(post);
        generateAndSetSummaryIfAbsent(post, postListVO);
        Optional.ofNullable(tagListMap.get(post.getId())).orElseGet(LinkedList::new);
        // Set tags
        postListVO.setTags(Optional.ofNullable(tagListMap.get(post.getId())).orElseGet(LinkedList::new).stream().filter(Objects::nonNull).map(tagService::convertTo).collect(Collectors.toList()));
        // Set categories
        postListVO.setCategories(Optional.ofNullable(categoryListMap.get(post.getId())).orElseGet(LinkedList::new).stream().filter(Objects::nonNull).map(categoryService::convertTo).collect(Collectors.toList()));
        // Set post metas
        List<PostMeta> metas = Optional.ofNullable(postMetaListMap.get(post.getId())).orElseGet(LinkedList::new);
        postListVO.setMetas(postMetaService.convertToMap(metas));
        // Set comment count
        postListVO.setCommentCount(commentCountMap.getOrDefault(post.getId(), 0L));
        postListVO.setFullPath(buildFullPath(post));
        // Post currently drafting in process
        Boolean isInProcess = contentService.draftingInProgress(post.getId());
        postListVO.setInProgress(isInProcess);
        return postListVO;
    });
}
Also used : Post(run.halo.app.model.entity.Post) LinkedList(java.util.LinkedList) PostListVO(run.halo.app.model.vo.PostListVO) PostMeta(run.halo.app.model.entity.PostMeta) LinkedList(java.util.LinkedList) List(java.util.List) NonNull(org.springframework.lang.NonNull)

Example 5 with PostMeta

use of run.halo.app.model.entity.PostMeta in project halo by halo-dev.

the class PostParam method getPostMetas.

public Set<PostMeta> getPostMetas() {
    Set<PostMeta> postMetaSet = new HashSet<>();
    if (CollectionUtils.isEmpty(metas)) {
        return postMetaSet;
    }
    for (PostMetaParam postMetaParam : metas) {
        PostMeta postMeta = postMetaParam.convertTo();
        postMetaSet.add(postMeta);
    }
    return postMetaSet;
}
Also used : PostMeta(run.halo.app.model.entity.PostMeta) HashSet(java.util.HashSet)

Aggregations

PostMeta (run.halo.app.model.entity.PostMeta)23 Category (run.halo.app.model.entity.Category)14 Tag (run.halo.app.model.entity.Tag)14 PostCategory (run.halo.app.model.entity.PostCategory)12 PostTag (run.halo.app.model.entity.PostTag)12 Post (run.halo.app.model.entity.Post)9 PatchedContent (run.halo.app.model.entity.Content.PatchedContent)8 Content (run.halo.app.model.entity.Content)6 PostComment (run.halo.app.model.entity.PostComment)6 NonNull (org.springframework.lang.NonNull)5 TypeReference (com.fasterxml.jackson.core.type.TypeReference)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 LinkedList (java.util.LinkedList)3 List (java.util.List)3 LogEvent (run.halo.app.event.logger.LogEvent)3 OptionUpdatedEvent (run.halo.app.event.options.OptionUpdatedEvent)3 ThemeUpdatedEvent (run.halo.app.event.theme.ThemeUpdatedEvent)3 ForbiddenException (run.halo.app.exception.ForbiddenException)3