添加博客文章相关功能(未完成)

This commit is contained in:
huangge1199 2024-08-27 16:03:15 +08:00
parent db8a2e9cf9
commit a7ec38e78b
11 changed files with 679 additions and 0 deletions

View File

@ -0,0 +1,116 @@
package com.ruoyi.blog.controller;
import java.util.List;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.core.core.domain.R;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.web.annotation.RepeatSubmit;
import com.ruoyi.common.web.core.BaseController;
import jakarta.annotation.Resource;
import com.ruoyi.blog.domain.vo.BlogPostsVo;
import com.ruoyi.blog.domain.bo.BlogPostsBo;
import com.ruoyi.blog.service.IBlogPostsService;
import com.ruoyi.common.orm.core.page.TableDataInfo;
/**
* 博客文章Controller
*
* @author huangge1199
* 2024-08-27
*/
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/blog/posts")
public class BlogPostsController extends BaseController
{
@Resource
private IBlogPostsService blogPostsService;
/**
* 查询博客文章列表
*/
@SaCheckPermission("blog:posts:list")
@GetMapping("/list")
public TableDataInfo<BlogPostsVo> list(BlogPostsBo blogPostsBo)
{
return blogPostsService.selectPage(blogPostsBo);
}
/**
* 导出博客文章列表
*/
@SaCheckPermission("blog:posts:export")
@Log(title = "博客文章", businessType = BusinessType.EXPORT)
@PostMapping("/export")
public void export(HttpServletResponse response, BlogPostsBo blogPostsBo)
{
List<BlogPostsVo> list = blogPostsService.selectList(blogPostsBo);
ExcelUtil.exportExcel(list, "博客文章", BlogPostsVo.class, response);
}
/**
* 获取博客文章详细信息
*/
@SaCheckPermission("blog:posts:query")
@GetMapping(value = "/{id}")
public R<BlogPostsVo> getInfo(@PathVariable Long id)
{
return R.ok(blogPostsService.selectById(id));
}
/**
* 新增博客文章
*/
@SaCheckPermission("blog:posts:add")
@Log(title = "博客文章", businessType = BusinessType.INSERT)
@RepeatSubmit()
@PostMapping
public R<Void> add(@Validated @RequestBody BlogPostsBo blogPostsBo)
{
boolean inserted = blogPostsService.insert(blogPostsBo);
if (!inserted) {
return R.fail("新增博客文章记录失败!");
}
return R.ok();
}
/**
* 修改博客文章
*/
@SaCheckPermission("blog:posts:edit")
@Log(title = "博客文章", businessType = BusinessType.UPDATE)
@RepeatSubmit()
@PutMapping
public R<Void> edit(@Validated @RequestBody BlogPostsBo blogPostsBo)
{
boolean updated = blogPostsService.update(blogPostsBo);
if (!updated) {
R.fail("修改博客文章记录失败!");
}
return R.ok();
}
/**
* 删除博客文章
*/
@SaCheckPermission("blog:posts:remove")
@Log(title = "博客文章", businessType = BusinessType.DELETE)
@DeleteMapping("/{ids}")
public R<Void> remove(@PathVariable Long[] ids)
{
boolean deleted = blogPostsService.deleteByIds(ids);
if (!deleted) {
R.fail("删除博客文章记录失败!");
}
return R.ok();
}
}

View File

@ -0,0 +1,39 @@
package com.ruoyi.blog.domain;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serial;
import java.io.Serializable;
import com.ruoyi.common.orm.core.domain.BaseEntity;
/**
* 博客文章内容对象 blog_contents
*
* @author huangge1199
* 2024-08-27
*/
@Data
@EqualsAndHashCode(callSuper = true)
@Table(value = "blog_contents")
public class BlogContents extends BaseEntity
{
@Serial
private static final long serialVersionUID = 1L;
/** */
@Id
private Long id;
/** 文章ID */
private Long postId;
/** html */
private String content;
/** md */
private String originalContent;
}

View File

@ -0,0 +1,62 @@
package com.ruoyi.blog.domain;
import java.util.List;
import com.ruoyi.blog.domain.BlogContents;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;
import com.ruoyi.common.orm.core.domain.BaseEntity;
/**
* 博客文章对象 blog_posts
*
* @author huangge1199
* 2024-08-27
*/
@Data
@EqualsAndHashCode(callSuper = true)
@Table(value = "blog_posts")
public class BlogPosts extends BaseEntity
{
/** 主键 */
@Id
private Long id;
/** 是否可评论01不能 */
private Integer disallowComment;
/** 关键字 */
private String keywords;
/** 密码 */
private String password;
/** 固定链接地址 */
private String slug;
/** 类型0文章1自定义页面 */
private Long type;
/** 状态0已发布1草稿2已删除3私密 */
private Long status;
/** 总览 */
private String summary;
/** 标题 */
private String title;
/** 置顶 */
private Long topPriority;
/** 拜访数量 */
private Long visits;
/** 字数 */
private Long wordCount;
/** 博客文章内容信息 */
private BlogContents blogContents;
}

View File

@ -0,0 +1,81 @@
package com.ruoyi.blog.domain.bo;
import com.ruoyi.blog.domain.BlogPosts;
import com.ruoyi.blog.domain.BlogContents;
import io.github.linpeilie.annotations.AutoMapper;
import lombok.Data;
import lombok.EqualsAndHashCode;
import jakarta.validation.constraints.*;
import java.util.List;
import com.ruoyi.common.orm.core.domain.BaseEntity;
/**
* 博客文章业务对象 blog_posts
*
* @author huangge1199
* @date 2024-08-27
*/
@Data
@EqualsAndHashCode(callSuper = true)
@AutoMapper(target = BlogPosts.class, reverseConvertGenerate = false)
public class BlogPostsBo extends BaseEntity
{
/**
* 主键
*/
@NotNull(message = "主键不能为空")
private Long id;
/**
* 是否可评论01不能
*/
private Integer disallowComment;
/**
* 关键字
*/
private String keywords;
/**
* 密码
*/
private String password;
/**
* 固定链接地址
*/
@NotBlank(message = "固定链接地址不能为空")
private String slug;
/**
* 类型0文章1自定义页面
*/
@NotNull(message = "类型0文章1自定义页面不能为空")
private Long type;
/**
* 状态0已发布1草稿2已删除3私密
*/
private Long status;
/**
* 总览
*/
private String summary;
/**
* 标题
*/
@NotBlank(message = "标题不能为空")
private String title;
/**
* 置顶
*/
private Long topPriority;
/** 博客文章内容信息 */
private BlogContents blogContents;
}

View File

@ -0,0 +1,90 @@
package com.ruoyi.blog.domain.vo;
import java.util.List;
import com.mybatisflex.annotation.RelationOneToOne;
import com.ruoyi.blog.domain.BlogPosts;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.ruoyi.common.excel.annotation.ExcelDictFormat;
import com.ruoyi.common.excel.convert.ExcelDictConvert;
import io.github.linpeilie.annotations.AutoMapper;
import lombok.Data;
import lombok.EqualsAndHashCode;
import com.mybatisflex.annotation.RelationOneToMany;
import com.ruoyi.blog.domain.BlogContents;
import java.io.Serial;
import java.io.Serializable;
import java.util.Date;
import com.ruoyi.common.orm.core.domain.BaseEntity;
/**
* 博客文章视图对象 blog_posts
*
* @author huangge1199
* @date 2024-08-27
*/
@Data
@ExcelIgnoreUnannotated
@EqualsAndHashCode(callSuper = true)
@AutoMapper(target = BlogPosts.class)
public class BlogPostsVo extends BaseEntity implements Serializable
{
@Serial
private static final long serialVersionUID = 1L;
/** 主键 */
@ExcelProperty(value = "主键")
private Long id;
/** 是否可评论01不能 */
@ExcelProperty(value = "是否可评论01不能")
private Integer disallowComment;
/** 关键字 */
@ExcelProperty(value = "关键字")
private String keywords;
/** 密码 */
@ExcelProperty(value = "密码")
private String password;
/** 固定链接地址 */
@ExcelProperty(value = "固定链接地址")
private String slug;
/** 类型0文章1自定义页面 */
@ExcelProperty(value = "类型0文章1自定义页面")
private Long type;
/** 状态0已发布1草稿2已删除3私密 */
@ExcelProperty(value = "状态0已发布1草稿2已删除3私密")
private Long status;
/** 总览 */
@ExcelProperty(value = "总览")
private String summary;
/** 标题 */
@ExcelProperty(value = "标题")
private String title;
/** 置顶 */
@ExcelProperty(value = "置顶")
private Long topPriority;
/** 拜访数量 */
@ExcelProperty(value = "拜访数量")
private Long visits;
/** 字数 */
@ExcelProperty(value = "字数")
private Long wordCount;
/** 博客文章内容信息 */
@RelationOneToOne(selfField = "id", targetField = "postId")
private BlogContents blogContents;
}

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.BlogContents;
/**
* 博客文章内容Mapper接口
*
* @author huangge1199
* 2024-08-27
*/
@Mapper
public interface BlogContentsMapper extends BaseMapper<BlogContents>
{
}

View File

@ -0,0 +1,18 @@
package com.ruoyi.blog.mapper;
import com.mybatisflex.core.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import com.ruoyi.blog.domain.BlogPosts;
import com.ruoyi.blog.domain.BlogContents;
/**
* 博客文章Mapper接口
*
* @author huangge1199
* 2024-08-27
*/
@Mapper
public interface BlogPostsMapper extends BaseMapper<BlogPosts>
{
}

View File

@ -0,0 +1,66 @@
package com.ruoyi.blog.service;
import java.util.List;
import com.ruoyi.blog.domain.BlogPosts;
import com.ruoyi.blog.domain.vo.BlogPostsVo;
import com.ruoyi.blog.domain.bo.BlogPostsBo;
import com.ruoyi.common.orm.core.service.IBaseService;
import com.ruoyi.common.orm.core.page.TableDataInfo;
/**
* 博客文章Service接口
*
* @author huangge1199
* 2024-08-27
*/
public interface IBlogPostsService extends IBaseService<BlogPosts>
{
/**
* 查询博客文章
*
* @param id 博客文章主键
* @return 博客文章
*/
BlogPostsVo selectById(Long id);
/**
* 查询博客文章列表
*
* @param blogPostsBo 博客文章Bo
* @return 博客文章集合
*/
List<BlogPostsVo> selectList(BlogPostsBo blogPostsBo);
/**
* 分页查询博客文章列表
*
* @param blogPostsBo 博客文章Bo
* @return 分页博客文章集合
*/
TableDataInfo<BlogPostsVo> selectPage(BlogPostsBo blogPostsBo);
/**
* 新增博客文章
*
* @param blogPostsBo 博客文章Bo
* @return 结果:true 操作成功false 操作失败
*/
boolean insert(BlogPostsBo blogPostsBo);
/**
* 修改博客文章
*
* @param blogPostsBo 博客文章Bo
* @return 结果:true 更新成功false 更新失败
*/
boolean update(BlogPostsBo blogPostsBo);
/**
* 批量删除博客文章
*
* @param ids 需要删除的博客文章主键集合
* @return 结果:true 删除成功false 删除失败
*/
boolean deleteByIds(Long[] ids);
}

View File

@ -0,0 +1,176 @@
package com.ruoyi.blog.service.impl;
import java.util.Arrays;
import java.util.List;
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.common.core.utils.MapstructUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.orm.core.page.PageQuery;
import com.ruoyi.common.orm.core.page.TableDataInfo;
import com.ruoyi.common.orm.core.service.impl.BaseServiceImpl;
import com.ruoyi.common.core.utils.DateUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Arrays;
import com.ruoyi.blog.domain.BlogContents;
import com.ruoyi.blog.mapper.BlogContentsMapper;
import static com.ruoyi.blog.domain.table.BlogContentsTableDef.BLOG_CONTENTS;
import com.ruoyi.blog.mapper.BlogPostsMapper;
import com.ruoyi.blog.domain.BlogPosts;
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.BlogPostsTableDef.BLOG_POSTS;
/**
* 博客文章Service业务层处理
*
* @author huangge1199
* 2024-08-27
*/
@Service
public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogPosts> implements IBlogPostsService
{
@Resource
private BlogPostsMapper blogPostsMapper;
@Resource
private BlogContentsMapper blogContentsMapper;
@Override
public QueryWrapper query() {
return super.query().from(BLOG_POSTS);
}
private QueryWrapper buildQueryWrapper(BlogPostsBo blogPostsBo) {
QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
if (StringUtils.isNotBlank(blogPostsBo.getSlug())) {
queryWrapper.and(BLOG_POSTS.SLUG.eq(blogPostsBo.getSlug()));
}
if (blogPostsBo.getType() != null) {
queryWrapper.and(BLOG_POSTS.TYPE.eq(blogPostsBo.getType()));
}
if (blogPostsBo.getStatus() != null) {
queryWrapper.and(BLOG_POSTS.STATUS.eq(blogPostsBo.getStatus()));
}
if (StringUtils.isNotBlank(blogPostsBo.getTitle())) {
queryWrapper.and(BLOG_POSTS.TITLE.eq(blogPostsBo.getTitle()));
}
return queryWrapper;
}
/**
* 查询博客文章
*
* @param id 博客文章主键
* @return 博客文章
*/
@Override
public BlogPostsVo selectById(Long id)
{
return blogPostsMapper.selectOneWithRelationsByQueryAs(query().where(BLOG_POSTS.ID.eq(id)), BlogPostsVo.class);
}
/**
* 查询博客文章列表
*
* @param blogPostsBo 博客文章Bo
* @return 博客文章集合
*/
@Override
public List<BlogPostsVo> selectList(BlogPostsBo blogPostsBo)
{
QueryWrapper queryWrapper = buildQueryWrapper(blogPostsBo);
return blogPostsMapper.selectListWithRelationsByQueryAs(queryWrapper, BlogPostsVo.class);
}
/**
* 分页查询博客文章列表
*
* @param blogPostsBo 博客文章Bo
* @return 分页博客文章集合
*/
@Override
public TableDataInfo<BlogPostsVo> selectPage(BlogPostsBo blogPostsBo)
{
QueryWrapper queryWrapper = buildQueryWrapper(blogPostsBo);
Page<BlogPostsVo> page = blogPostsMapper.paginateWithRelationsAs(PageQuery.build(), queryWrapper, BlogPostsVo.class);
return TableDataInfo.build(page);
}
/**
* 新增博客文章
*
* @param blogPostsBo 博客文章Bo
* @return 结果:true 操作成功false 操作失败
*/
@Transactional
@Override
public boolean insert(BlogPostsBo blogPostsBo)
{
BlogPosts blogPosts = MapstructUtils.convert(blogPostsBo, BlogPosts.class);
boolean inserted = this.save(blogPosts);
if (inserted && ObjectUtil.isNotNull(blogPosts)) {
return insertBlogContents(blogPosts);
}
return false;
}
/**
* 修改博客文章
*
* @param blogPostsBo 博客文章Bo
* @return 结果:true 更新成功false 更新失败
*/
@Transactional
@Override
public boolean update(BlogPostsBo blogPostsBo)
{
BlogPosts blogPosts = MapstructUtils.convert(blogPostsBo, BlogPosts.class);
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);
return insertBlogContents(blogPosts);
}
}
return false;
}
/**
* 批量删除博客文章
*
* @param ids 需要删除的博客文章主键集合
* @return 结果:true 删除成功false 删除失败
*/
@Transactional
@Override
public boolean deleteByIds(Long[] ids)
{
QueryWrapper queryWrapper = QueryWrapper.create().from(BLOG_CONTENTS).where(BLOG_CONTENTS.POST_ID.in(Arrays.asList(ids)));
blogContentsMapper.deleteByQuery(queryWrapper);
return this.removeByIds(Arrays.asList(ids));
}
/**
* 新增博客文章内容信息
*
* @param blogPosts 博客文章对象
*/
private boolean insertBlogContents(BlogPosts blogPosts)
{
BlogContents blogContents = blogPosts.getBlogContents();
Long id = blogPosts.getId();
blogContents.setPostId(id);
return blogContentsMapper.insert(blogContents) > 0;
}
}

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.BlogContentsMapper">
</mapper>

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.BlogPostsMapper">
</mapper>