添加/修改 博客文章

This commit is contained in:
huangge1199 2024-09-09 11:17:31 +08:00
parent fb7b49feb7
commit f4820177d2
4 changed files with 40 additions and 35 deletions

View File

@ -76,7 +76,12 @@ public class BlogPostsController extends BaseController
@PostMapping @PostMapping
public R<Void> add(@Validated @RequestBody BlogPostsBo blogPostsBo) public R<Void> add(@Validated @RequestBody BlogPostsBo blogPostsBo)
{ {
boolean inserted = blogPostsService.insert(blogPostsBo); boolean inserted = false;
try {
inserted = blogPostsService.insert(blogPostsBo);
} catch (Exception e) {
return R.fail(e.getCause().getMessage());
}
if (!inserted) { if (!inserted) {
return R.fail("新增博客文章记录失败!"); return R.fail("新增博客文章记录失败!");
} }

View File

@ -24,7 +24,6 @@ public class BlogPostsBo extends BaseEntity
/** /**
* 主键 * 主键
*/ */
@NotNull(message = "主键不能为空")
private Long id; private Long id;
/** /**

View File

@ -45,7 +45,7 @@ public interface IBlogPostsService extends IBaseService<BlogPosts>
* @param blogPostsBo 博客文章Bo * @param blogPostsBo 博客文章Bo
* @return 结果:true 操作成功false 操作失败 * @return 结果:true 操作成功false 操作失败
*/ */
boolean insert(BlogPostsBo blogPostsBo); boolean insert(BlogPostsBo blogPostsBo) throws Exception;
/** /**
* 修改博客文章 * 修改博客文章

View File

@ -3,6 +3,7 @@ package com.ruoyi.blog.service.impl;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map; 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;
@ -15,16 +16,21 @@ import com.ruoyi.common.core.utils.DateUtils;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import com.ruoyi.blog.domain.BlogContents; import com.ruoyi.blog.domain.BlogContents;
import com.ruoyi.blog.mapper.BlogContentsMapper; import com.ruoyi.blog.mapper.BlogContentsMapper;
import static com.ruoyi.blog.domain.table.BlogContentsTableDef.BLOG_CONTENTS; import static com.ruoyi.blog.domain.table.BlogContentsTableDef.BLOG_CONTENTS;
import com.ruoyi.blog.mapper.BlogPostsMapper; import com.ruoyi.blog.mapper.BlogPostsMapper;
import com.ruoyi.blog.domain.BlogPosts; import com.ruoyi.blog.domain.BlogPosts;
import com.ruoyi.blog.domain.bo.BlogPostsBo; 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.BlogPostsTableDef.BLOG_POSTS; import static com.ruoyi.blog.domain.table.BlogPostsTableDef.BLOG_POSTS;
/** /**
@ -34,8 +40,7 @@ import static com.ruoyi.blog.domain.table.BlogPostsTableDef.BLOG_POSTS;
* 2024-08-27 * 2024-08-27
*/ */
@Service @Service
public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogPosts> implements IBlogPostsService public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogPosts> implements IBlogPostsService {
{
@Resource @Resource
private BlogPostsMapper blogPostsMapper; private BlogPostsMapper blogPostsMapper;
@Resource @Resource
@ -49,18 +54,18 @@ public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogP
private QueryWrapper buildQueryWrapper(BlogPostsBo blogPostsBo) { private QueryWrapper buildQueryWrapper(BlogPostsBo blogPostsBo) {
QueryWrapper queryWrapper = super.buildBaseQueryWrapper(); QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
if (StringUtils.isNotBlank(blogPostsBo.getSlug())) { if (StringUtils.isNotBlank(blogPostsBo.getSlug())) {
queryWrapper.and(BLOG_POSTS.SLUG.eq(blogPostsBo.getSlug())); queryWrapper.and(BLOG_POSTS.SLUG.eq(blogPostsBo.getSlug()));
} }
if (blogPostsBo.getType() != null) { if (blogPostsBo.getType() != null) {
queryWrapper.and(BLOG_POSTS.TYPE.eq(blogPostsBo.getType())); queryWrapper.and(BLOG_POSTS.TYPE.eq(blogPostsBo.getType()));
} }
if (blogPostsBo.getStatus() != null) { if (blogPostsBo.getStatus() != null) {
queryWrapper.and(BLOG_POSTS.STATUS.eq(blogPostsBo.getStatus())); queryWrapper.and(BLOG_POSTS.STATUS.eq(blogPostsBo.getStatus()));
} }
if (StringUtils.isNotBlank(blogPostsBo.getTitle())) { if (StringUtils.isNotBlank(blogPostsBo.getTitle())) {
queryWrapper.and(BLOG_POSTS.TITLE.eq(blogPostsBo.getTitle())); queryWrapper.and(BLOG_POSTS.TITLE.eq(blogPostsBo.getTitle()));
} }
return queryWrapper; return queryWrapper;
} }
@ -72,8 +77,7 @@ public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogP
* @return 博客文章 * @return 博客文章
*/ */
@Override @Override
public BlogPostsVo selectById(Long id) public BlogPostsVo selectById(Long id) {
{
return blogPostsMapper.selectOneWithRelationsByQueryAs(query().where(BLOG_POSTS.ID.eq(id)), BlogPostsVo.class); return blogPostsMapper.selectOneWithRelationsByQueryAs(query().where(BLOG_POSTS.ID.eq(id)), BlogPostsVo.class);
} }
@ -85,8 +89,7 @@ public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogP
* @return 博客文章集合 * @return 博客文章集合
*/ */
@Override @Override
public List<BlogPostsVo> selectList(BlogPostsBo blogPostsBo) public List<BlogPostsVo> selectList(BlogPostsBo blogPostsBo) {
{
QueryWrapper queryWrapper = buildQueryWrapper(blogPostsBo); QueryWrapper queryWrapper = buildQueryWrapper(blogPostsBo);
return blogPostsMapper.selectListWithRelationsByQueryAs(queryWrapper, BlogPostsVo.class); return blogPostsMapper.selectListWithRelationsByQueryAs(queryWrapper, BlogPostsVo.class);
} }
@ -98,8 +101,7 @@ public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogP
* @return 分页博客文章集合 * @return 分页博客文章集合
*/ */
@Override @Override
public TableDataInfo<BlogPostsVo> selectPage(BlogPostsBo blogPostsBo) public TableDataInfo<BlogPostsVo> selectPage(BlogPostsBo blogPostsBo) {
{
QueryWrapper queryWrapper = buildQueryWrapper(blogPostsBo); QueryWrapper queryWrapper = buildQueryWrapper(blogPostsBo);
Page<BlogPostsVo> page = blogPostsMapper.paginateWithRelationsAs(PageQuery.build(), queryWrapper, BlogPostsVo.class); Page<BlogPostsVo> page = blogPostsMapper.paginateWithRelationsAs(PageQuery.build(), queryWrapper, BlogPostsVo.class);
return TableDataInfo.build(page); return TableDataInfo.build(page);
@ -113,12 +115,14 @@ public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogP
*/ */
@Transactional @Transactional
@Override @Override
public boolean insert(BlogPostsBo blogPostsBo) public boolean insert(BlogPostsBo blogPostsBo) throws Exception {
{ long cnt = blogPostsMapper.selectCountByQuery(QueryWrapper.create().eq("title",blogPostsBo.getTitle()));
if (cnt>0){
throw new Exception("标题已存在");
}
BlogPosts blogPosts = MapstructUtils.convert(blogPostsBo, BlogPosts.class); BlogPosts blogPosts = MapstructUtils.convert(blogPostsBo, BlogPosts.class);
int inserted = blogPostsMapper.insert(blogPosts,false);
boolean inserted = this.save(blogPosts); if (inserted > 0 && ObjectUtil.isNotNull(blogPosts)) {
if (inserted && ObjectUtil.isNotNull(blogPosts)) {
return insertBlogContents(blogPosts); return insertBlogContents(blogPosts);
} }
return false; return false;
@ -132,10 +136,9 @@ public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogP
*/ */
@Transactional @Transactional
@Override @Override
public boolean update(BlogPostsBo blogPostsBo) public boolean update(BlogPostsBo blogPostsBo) {
{
BlogPosts blogPosts = MapstructUtils.convert(blogPostsBo, BlogPosts.class); BlogPosts blogPosts = MapstructUtils.convert(blogPostsBo, BlogPosts.class);
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())); QueryWrapper queryWrapper = QueryWrapper.create().from(BLOG_CONTENTS).where(BLOG_CONTENTS.POST_ID.eq(blogPosts.getId()));
@ -154,8 +157,7 @@ public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogP
*/ */
@Transactional @Transactional
@Override @Override
public boolean deleteByIds(Long[] ids) public boolean deleteByIds(Long[] ids) {
{
QueryWrapper queryWrapper = QueryWrapper.create().from(BLOG_CONTENTS).where(BLOG_CONTENTS.POST_ID.in(Arrays.asList(ids))); QueryWrapper queryWrapper = QueryWrapper.create().from(BLOG_CONTENTS).where(BLOG_CONTENTS.POST_ID.in(Arrays.asList(ids)));
blogContentsMapper.deleteByQuery(queryWrapper); blogContentsMapper.deleteByQuery(queryWrapper);
return this.removeByIds(Arrays.asList(ids)); return this.removeByIds(Arrays.asList(ids));
@ -166,8 +168,7 @@ public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogP
* *
* @param blogPosts 博客文章对象 * @param blogPosts 博客文章对象
*/ */
private boolean insertBlogContents(BlogPosts blogPosts) private boolean insertBlogContents(BlogPosts blogPosts) {
{
BlogContents blogContents = blogPosts.getBlogContents(); BlogContents blogContents = blogPosts.getBlogContents();
Long id = blogPosts.getId(); Long id = blogPosts.getId();
blogContents.setPostId(id); blogContents.setPostId(id);