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

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; private Long type;
/** 状态0已发布1草稿2已删除3私密 */ /** 状态0已发布1草稿2已删除3私密 */
private Long status; private String status;
/** 总览 */ /** 总览 */
private String summary; private String summary;

View File

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

View File

@ -60,7 +60,7 @@ public class BlogPostsVo extends BaseEntity implements Serializable
/** 状态0已发布1草稿2已删除3私密 */ /** 状态0已发布1草稿2已删除3私密 */
@ExcelProperty(value = "状态0已发布1草稿2已删除3私密") @ExcelProperty(value = "状态0已发布1草稿2已删除3私密")
private Long status; private String status;
/** 总览 */ /** 总览 */
@ExcelProperty(value = "总览") @ExcelProperty(value = "总览")
@ -87,4 +87,9 @@ public class BlogPostsVo extends BaseEntity implements Serializable
@RelationOneToOne(selfField = "id", targetField = "postId") @RelationOneToOne(selfField = "id", targetField = "postId")
private BlogContents blogContents; 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; package com.ruoyi.blog.mapper;
import com.mybatisflex.core.BaseMapper; import com.mybatisflex.core.BaseMapper;
import com.ruoyi.blog.domain.vo.BlogPostsVo;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import com.ruoyi.blog.domain.BlogPosts; import com.ruoyi.blog.domain.BlogPosts;
import com.ruoyi.blog.domain.BlogContents; import com.ruoyi.blog.domain.BlogContents;
@ -14,5 +15,4 @@ import com.ruoyi.blog.domain.BlogContents;
@Mapper @Mapper
public interface BlogPostsMapper extends BaseMapper<BlogPosts> public interface BlogPostsMapper extends BaseMapper<BlogPosts>
{ {
} }

View File

@ -7,6 +7,8 @@ import java.util.Map;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page; import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper; 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.MapstructUtils;
import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.orm.core.page.PageQuery; 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.domain.vo.BlogPostsVo;
import com.ruoyi.blog.service.IBlogPostsService; 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; import static com.ruoyi.blog.domain.table.BlogPostsTableDef.BLOG_POSTS;
/** /**
@ -45,6 +48,8 @@ public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogP
private BlogPostsMapper blogPostsMapper; private BlogPostsMapper blogPostsMapper;
@Resource @Resource
private BlogContentsMapper blogContentsMapper; private BlogContentsMapper blogContentsMapper;
@Resource
private BlogPostCategoriesMapper blogPostCategoriesMapper;
@Override @Override
public QueryWrapper query() { public QueryWrapper query() {
@ -78,8 +83,15 @@ public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogP
*/ */
@Override @Override
public BlogPostsVo selectById(Long id) { 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;
} }
/** /**
@ -116,13 +128,20 @@ public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogP
@Transactional @Transactional
@Override @Override
public boolean insert(BlogPostsBo blogPostsBo) throws Exception { public boolean insert(BlogPostsBo blogPostsBo) throws Exception {
long cnt = blogPostsMapper.selectCountByQuery(QueryWrapper.create().eq("title",blogPostsBo.getTitle())); long cnt = blogPostsMapper.selectCountByQuery(QueryWrapper.create().eq("title", blogPostsBo.getTitle()));
if (cnt>0){ if (cnt > 0) {
throw new Exception("标题已存在"); throw new Exception("标题已存在");
} }
BlogPosts blogPosts = MapstructUtils.convert(blogPostsBo, BlogPosts.class); BlogPosts blogPosts = MapstructUtils.convert(blogPostsBo, BlogPosts.class);
int inserted = blogPostsMapper.insert(blogPosts,false); int inserted = blogPostsMapper.insert(blogPosts, false);
if (inserted > 0 && ObjectUtil.isNotNull(blogPosts)) { 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 insertBlogContents(blogPosts);
} }
return false; return false;
@ -141,8 +160,25 @@ public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogP
if (ObjectUtil.isNotNull(blogPosts) && ObjectUtil.isNotNull(blogPosts.getId())) { if (ObjectUtil.isNotNull(blogPosts) && ObjectUtil.isNotNull(blogPosts.getId())) {
boolean updated = this.updateById(blogPosts); boolean updated = this.updateById(blogPosts);
if (updated) { if (updated) {
QueryWrapper queryWrapper = QueryWrapper.create().from(BLOG_CONTENTS).where(BLOG_CONTENTS.POST_ID.eq(blogPosts.getId())); blogPostCategoriesMapper.deleteByQuery(
blogContentsMapper.deleteByQuery(queryWrapper); 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); 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" PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.blog.mapper.BlogPostsMapper"> <mapper namespace="com.ruoyi.blog.mapper.BlogPostsMapper">
</mapper> </mapper>

View File

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