添加/修改 博客文章

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
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) {
return R.fail("新增博客文章记录失败!");
}

View File

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

View File

@ -45,7 +45,7 @@ public interface IBlogPostsService extends IBaseService<BlogPosts>
* @param blogPostsBo 博客文章Bo
* @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.List;
import java.util.Map;
import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
@ -15,16 +16,21 @@ 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;
/**
@ -34,8 +40,7 @@ import static com.ruoyi.blog.domain.table.BlogPostsTableDef.BLOG_POSTS;
* 2024-08-27
*/
@Service
public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogPosts> implements IBlogPostsService
{
public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogPosts> implements IBlogPostsService {
@Resource
private BlogPostsMapper blogPostsMapper;
@Resource
@ -72,8 +77,7 @@ public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogP
* @return 博客文章
*/
@Override
public BlogPostsVo selectById(Long id)
{
public BlogPostsVo selectById(Long id) {
return blogPostsMapper.selectOneWithRelationsByQueryAs(query().where(BLOG_POSTS.ID.eq(id)), BlogPostsVo.class);
}
@ -85,8 +89,7 @@ public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogP
* @return 博客文章集合
*/
@Override
public List<BlogPostsVo> selectList(BlogPostsBo blogPostsBo)
{
public List<BlogPostsVo> selectList(BlogPostsBo blogPostsBo) {
QueryWrapper queryWrapper = buildQueryWrapper(blogPostsBo);
return blogPostsMapper.selectListWithRelationsByQueryAs(queryWrapper, BlogPostsVo.class);
}
@ -98,8 +101,7 @@ public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogP
* @return 分页博客文章集合
*/
@Override
public TableDataInfo<BlogPostsVo> selectPage(BlogPostsBo blogPostsBo)
{
public TableDataInfo<BlogPostsVo> selectPage(BlogPostsBo blogPostsBo) {
QueryWrapper queryWrapper = buildQueryWrapper(blogPostsBo);
Page<BlogPostsVo> page = blogPostsMapper.paginateWithRelationsAs(PageQuery.build(), queryWrapper, BlogPostsVo.class);
return TableDataInfo.build(page);
@ -113,12 +115,14 @@ public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogP
*/
@Transactional
@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);
boolean inserted = this.save(blogPosts);
if (inserted && ObjectUtil.isNotNull(blogPosts)) {
int inserted = blogPostsMapper.insert(blogPosts,false);
if (inserted > 0 && ObjectUtil.isNotNull(blogPosts)) {
return insertBlogContents(blogPosts);
}
return false;
@ -132,8 +136,7 @@ public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogP
*/
@Transactional
@Override
public boolean update(BlogPostsBo blogPostsBo)
{
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);
@ -154,8 +157,7 @@ public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogP
*/
@Transactional
@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)));
blogContentsMapper.deleteByQuery(queryWrapper);
return this.removeByIds(Arrays.asList(ids));
@ -166,8 +168,7 @@ public class BlogPostsServiceImpl extends BaseServiceImpl<BlogPostsMapper, BlogP
*
* @param blogPosts 博客文章对象
*/
private boolean insertBlogContents(BlogPosts blogPosts)
{
private boolean insertBlogContents(BlogPosts blogPosts) {
BlogContents blogContents = blogPosts.getBlogContents();
Long id = blogPosts.getId();
blogContents.setPostId(id);