添加 博客文章关联分类的相关功能

This commit is contained in:
huangge1199 2024-09-10 15:19:50 +08:00
parent f4820177d2
commit 7bf9d1d78c
10 changed files with 121 additions and 13 deletions

View File

@ -0,0 +1,33 @@
package com.ruoyi.blog.domain;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.Table;
import lombok.*;
import com.ruoyi.common.orm.core.domain.BaseEntity;
/**
* 文章分类关联对象 blog_post_categories
*
* @author huangge1199
* 2024-09-10
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Table(value = "blog_post_categories")
public class BlogPostCategories extends BaseEntity
{
/** */
@Id
private Long id;
/** 分类ID */
private Long categoryId;
/** 文章ID */
private Long postId;
}

View File

@ -39,7 +39,7 @@ public class BlogPosts extends BaseEntity
private Long type;
/** 状态0已发布1草稿2已删除3私密 */
private Long status;
private String status;
/** 总览 */
private String summary;

View File

@ -56,7 +56,7 @@ public class BlogPostsBo extends BaseEntity
/**
* 状态0已发布1草稿2已删除3私密
*/
private Long status;
private String status;
/**
* 总览
@ -75,6 +75,14 @@ public class BlogPostsBo extends BaseEntity
private Long topPriority;
/** 博客文章内容信息 */
/**
* 博客文章内容信息
* */
private BlogContents blogContents;
/**
* 博客分类集合
*/
private List<Long> categories;
}

View File

@ -60,7 +60,7 @@ public class BlogPostsVo extends BaseEntity implements Serializable
/** 状态0已发布1草稿2已删除3私密 */
@ExcelProperty(value = "状态0已发布1草稿2已删除3私密")
private Long status;
private String status;
/** 总览 */
@ExcelProperty(value = "总览")
@ -87,4 +87,9 @@ public class BlogPostsVo extends BaseEntity implements Serializable
@RelationOneToOne(selfField = "id", targetField = "postId")
private BlogContents blogContents;
/**
* 博客分类集合
*/
private List<Long> categories;
}

View File

@ -0,0 +1,17 @@
package com.ruoyi.blog.mapper;
import com.mybatisflex.core.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import com.ruoyi.blog.domain.BlogPostCategories;
/**
* 文章分类关联Mapper接口
*
* @author huangge1199
* 2024-09-10
*/
@Mapper
public interface BlogPostCategoriesMapper extends BaseMapper<BlogPostCategories>
{
}

View File

@ -1,6 +1,7 @@
package com.ruoyi.blog.mapper;
import com.mybatisflex.core.BaseMapper;
import com.ruoyi.blog.domain.vo.BlogPostsVo;
import org.apache.ibatis.annotations.Mapper;
import com.ruoyi.blog.domain.BlogPosts;
import com.ruoyi.blog.domain.BlogContents;
@ -14,5 +15,4 @@ import com.ruoyi.blog.domain.BlogContents;
@Mapper
public interface BlogPostsMapper extends BaseMapper<BlogPosts>
{
}

View File

@ -7,6 +7,8 @@ import java.util.Map;
import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.ruoyi.blog.domain.BlogPostCategories;
import com.ruoyi.blog.mapper.BlogPostCategoriesMapper;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.orm.core.page.PageQuery;
@ -31,6 +33,7 @@ import com.ruoyi.blog.domain.bo.BlogPostsBo;
import com.ruoyi.blog.domain.vo.BlogPostsVo;
import com.ruoyi.blog.service.IBlogPostsService;
import static com.ruoyi.blog.domain.table.BlogPostCategoriesTableDef.BLOG_POST_CATEGORIES;
import static com.ruoyi.blog.domain.table.BlogPostsTableDef.BLOG_POSTS;
/**
@ -45,6 +48,8 @@ public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogP
private BlogPostsMapper blogPostsMapper;
@Resource
private BlogContentsMapper blogContentsMapper;
@Resource
private BlogPostCategoriesMapper blogPostCategoriesMapper;
@Override
public QueryWrapper query() {
@ -78,8 +83,15 @@ public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogP
*/
@Override
public BlogPostsVo selectById(Long id) {
return blogPostsMapper.selectOneWithRelationsByQueryAs(query().where(BLOG_POSTS.ID.eq(id)), BlogPostsVo.class);
BlogPostsVo blogPostsVo = blogPostsMapper.selectOneWithRelationsByQueryAs(query().where(BLOG_POSTS.ID.eq(id)), BlogPostsVo.class);
List<Long> categories = blogPostCategoriesMapper.selectListByQuery(
QueryWrapper
.create()
.from(BLOG_POST_CATEGORIES)
.where(BLOG_POST_CATEGORIES.POST_ID.eq(id))
).stream().map(BlogPostCategories::getCategoryId).toList();
blogPostsVo.setCategories(categories);
return blogPostsVo;
}
/**
@ -123,6 +135,13 @@ public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogP
BlogPosts blogPosts = MapstructUtils.convert(blogPostsBo, BlogPosts.class);
int inserted = blogPostsMapper.insert(blogPosts, false);
if (inserted > 0 && ObjectUtil.isNotNull(blogPosts)) {
for (Long categoryId : blogPostsBo.getCategories()) {
BlogPostCategories blogPostCategories = BlogPostCategories.builder()
.postId(blogPosts.getId())
.categoryId(categoryId)
.build();
blogPostCategoriesMapper.insert(blogPostCategories, false);
}
return insertBlogContents(blogPosts);
}
return false;
@ -141,8 +160,25 @@ public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogP
if (ObjectUtil.isNotNull(blogPosts) && ObjectUtil.isNotNull(blogPosts.getId())) {
boolean updated = this.updateById(blogPosts);
if (updated) {
QueryWrapper queryWrapper = QueryWrapper.create().from(BLOG_CONTENTS).where(BLOG_CONTENTS.POST_ID.eq(blogPosts.getId()));
blogContentsMapper.deleteByQuery(queryWrapper);
blogPostCategoriesMapper.deleteByQuery(
QueryWrapper
.create()
.from(BLOG_POST_CATEGORIES)
.where(BLOG_POST_CATEGORIES.POST_ID.eq(blogPosts.getId()))
);
for (Long categoryId : blogPostsBo.getCategories()) {
BlogPostCategories blogPostCategories = BlogPostCategories.builder()
.postId(blogPosts.getId())
.categoryId(categoryId)
.build();
blogPostCategoriesMapper.insert(blogPostCategories, false);
}
blogContentsMapper.deleteByQuery(
QueryWrapper
.create()
.from(BLOG_CONTENTS)
.where(BLOG_CONTENTS.POST_ID.eq(blogPosts.getId()))
);
return insertBlogContents(blogPosts);
}
}

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.blog.mapper.BlogPostCategoriesMapper">
</mapper>

View File

@ -3,5 +3,4 @@
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.blog.mapper.BlogPostsMapper">
</mapper>

View File

@ -28,6 +28,9 @@ import com.ruoyi.common.orm.core.domain.TreeEntity;
#set($Entity="TreeEntity")
#end
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Table(value = "${tableName}")
public class ${ClassName} extends ${Entity}