修改文章完善

This commit is contained in:
温文静WWW 2025-07-23 10:16:44 +08:00
parent 0d83fa5be1
commit d6ff1bc6ce
8 changed files with 158 additions and 97 deletions

View File

@ -51,23 +51,19 @@ public class CpArticleController {
@Autowired
private CpArticleService entityService;
@Autowired
private CpArticleMapper cpArticleMapper;
@Autowired
private CpTextService cpTextService;
@Autowired
private CpLabelMapper cpLabelMapper;
@ApiOperation(value = "分页查询文章(支持标题模糊查询)", notes = "分页查询文章,仅根据文章标题进行模糊查询")
@ApiOperation(value = "分页查询文章(title模糊查询)", notes = "分页查询文章,仅根据文章标题进行模糊查询")
@RequestMapping(method = RequestMethod.GET)
@ApiImplicitParams({
@ApiImplicitParam(name = "size", value = "分页大小", paramType = "query"),
@ApiImplicitParam(name = "current", value = "当前页面从1开始", paramType = "query"),
@ApiImplicitParam(name = "titleKeyword", value = "搜索关键词,用于模糊查询文章标题", paramType = "query")
@ApiImplicitParam(name = "current", value = "当前页面从1开始", paramType = "query")
})
public ResponseEntity<IPage<CpArticleVo>> findByPage(
final Page<CpArticleVo> page,
@RequestParam(required = false) String titleKeyword) {
IPage<CpArticleVo> records = entityService.findByPage(page, titleKeyword);
final Page<CpArticleVo> page, CpArticleDTO cpArticleDTO) {
IPage<CpArticleVo> records = entityService.findByPage(page, cpArticleDTO);
return new ResponseEntity<>(records, HttpStatus.OK);
}
@ -121,6 +117,12 @@ public class CpArticleController {
@ApiOperation(value = "添加文章")
@RequestMapping(method = {RequestMethod.POST})
@ApiImplicitParams({
@ApiImplicitParam(name = "title", value = "文章标题", required = true, paramType = "query"),
@ApiImplicitParam(name = "text", value = "文章正文内容", required = true, paramType = "query"),
@ApiImplicitParam(name = "labelId", value = "标签id", required = true, paramType = "query"),
@ApiImplicitParam(name = "moduleId", value = "模块id", required = true, paramType = "query")
})
public ResponseEntity<?> add(@Valid @RequestBody final CpArticleDTO cpArticleDTO, final BindingResult result) {
try {
// 处理参数校验错误
@ -197,72 +199,9 @@ public class CpArticleController {
@ApiOperation(value = "修改文章")
@RequestMapping(method = {RequestMethod.PUT})
public ResponseEntity<?> update(@Valid @RequestBody final CpArticleVo entity, final BindingResult result) {
try {
// 处理参数校验错误
if (result.hasErrors()) {
Map<String, Object> error = new HashMap<>();
error.put("code", 400);
// 提取第一个错误信息
FieldError firstError = result.getFieldErrors().get(0);
error.put("msg", firstError.getField() + "" + firstError.getDefaultMessage());
return ResponseEntity.badRequest().body(error);
}
// 校验ID是否存在
if (entity.getId() == null) {
Map<String, Object> error = new HashMap<>();
error.put("code", 400);
error.put("msg", "id不能为空");
return ResponseEntity.badRequest().body(error);
}
// 查找标签信息
CpLabel cpLabelVo = cpLabelMapper.findById(entity.getCplabelId());
if (cpLabelVo == null) {
Map<String, Object> error = new HashMap<>();
error.put("code", 400);
error.put("msg", "标签不存在");
return ResponseEntity.badRequest().body(error);
}
// 校验文章是否存在
CpArticle existingArticle = entityService.getById(entity.getId());
if (existingArticle == null) {
Map<String, Object> error = new HashMap<>();
error.put("code", 404);
error.put("msg", "文章不存在");
return ResponseEntity.status(404).body(error);
}
// 更新文章内容
if (existingArticle.getTextid() == null) {
Map<String, Object> error = new HashMap<>();
error.put("code", 400);
error.put("msg", "文章内容ID不存在");
return ResponseEntity.badRequest().body(error);
}
boolean textUpdated = cpTextService.lambdaUpdate()
.set(CpText::getText, entity.getText())
.eq(CpText::getId, existingArticle.getTextid())
.update();
if (!textUpdated) {
Map<String, Object> error = new HashMap<>();
error.put("code", 500);
error.put("msg", "文章内容更新失败");
return ResponseEntity.status(500).body(error);
}
// 成功响应返回更新后的文章数据
return ResponseEntity.ok(entity);
} catch (Exception e) {
LOG.error("修改文章失败", e);
Map<String, Object> error = new HashMap<>();
error.put("code", 500);
error.put("msg", "修改文章失败:" + e.getMessage());
return ResponseEntity.status(500).body(error);
}
@ApiImplicitParam(name = "id", value = "文章id", required = true, paramType = "path")
public ResponseEntity<?> update(@Valid @RequestBody final CpArticleDTO entity, final BindingResult result) {
return entityService.updateById(entity, result);
}
@ApiOperation(value = "全部文章")

View File

@ -2,12 +2,13 @@ package com.aircraft.modules.article.domain.dto;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotBlank;
@Data
public class CpArticleDTO {
@NotBlank(message = "标题不能为空")
@ApiModelProperty(value = "文章标题", required = true)
@ApiModelProperty(value = "id")
private Long id;
@ApiModelProperty(value = "文章标题")
private String title;
@ApiModelProperty(value = "封面图片URL")
@ -19,14 +20,13 @@ public class CpArticleDTO {
@ApiModelProperty(value = "文章外链")
private String url;
@ApiModelProperty(value = "标签ID", required = true)
@ApiModelProperty(value = "标签ID")
private int labelId;
@ApiModelProperty(value = "模块ID", required = true)
@ApiModelProperty(value = "模块ID")
private int moduleId;
@NotBlank(message = "文章内容不能为空")
@ApiModelProperty(value = "文章正文内容", required = true)
@ApiModelProperty(value = "文章正文内容")
private String text;
@ApiModelProperty(value = "是否置顶1-置顶0-不置顶")

View File

@ -1,6 +1,7 @@
package com.aircraft.modules.article.mapper;
import com.aircraft.modules.article.domain.CpArticle;
import com.aircraft.modules.article.domain.dto.CpArticleDTO;
import com.aircraft.modules.article.domain.dto.CpArticleVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
@ -22,9 +23,11 @@ public interface CpArticleMapper extends BaseMapper<CpArticle> {
void updateDelFlagById(Long id, Integer delFlag);
IPage<CpArticleVo> selectVoPage(Page<CpArticleVo> page, String titleKeyword);
IPage<CpArticleVo> selectVoPage(Page<CpArticleVo> page, CpArticleDTO dto);
CpArticleVo selectVoById(Integer id);
void updateViewCountById(Integer id);
void updateById(CpArticleDTO entity);
}

View File

@ -32,4 +32,6 @@ public interface CpLabelMapper extends BaseMapper<CpLabel> {
CpLabel findById(int labelId);
IPage<CpLabelVo> selectVoPage(Page<CpLabelVo> page, String name);
void updateModuleIdById(int moduleId);
}

View File

@ -1,10 +1,13 @@
package com.aircraft.modules.article.service;
import com.aircraft.modules.article.domain.CpArticle;
import com.aircraft.modules.article.domain.dto.CpArticleDTO;
import com.aircraft.modules.article.domain.dto.CpArticleVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import java.util.List;
@ -22,7 +25,9 @@ public interface CpArticleService extends IService<CpArticle> {
void updateDelFlagById(Long id, Integer delFlag);
IPage<CpArticleVo> findByPage(Page<CpArticleVo> page, String titleKeyword);
IPage<CpArticleVo> findByPage(Page<CpArticleVo> page, CpArticleDTO cpArticleDTO);
CpArticleVo getArticleById(Integer id);
ResponseEntity<?> updateById(CpArticleDTO entity, BindingResult result);
}

View File

@ -1,25 +1,31 @@
package com.aircraft.modules.article.service.impl;
import com.aircraft.modules.article.controller.CpArticleController;
import com.aircraft.modules.article.domain.CpArticle;
import com.aircraft.modules.article.domain.CpLabel;
import com.aircraft.modules.article.domain.CpText;
import com.aircraft.modules.article.domain.dto.CpArticleDTO;
import com.aircraft.modules.article.domain.dto.CpArticleVo;
import com.aircraft.modules.article.mapper.CpArticleMapper;
import com.aircraft.modules.article.mapper.CpLabelMapper;
import com.aircraft.modules.article.mapper.CpModuleMapper;
import com.aircraft.modules.article.mapper.CpTextMapper;
import com.aircraft.modules.article.service.CpArticleService;
import com.aircraft.modules.article.service.CpTextService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Map;
/**
* <p>
@ -31,13 +37,19 @@ import java.util.stream.Collectors;
*/
@Service
public class CpArticleServiceImpl extends ServiceImpl<CpArticleMapper, CpArticle> implements CpArticleService {
private static final Logger LOG = LoggerFactory.getLogger(CpArticleController.class);
@Autowired
private CpArticleMapper cpArticleMapper;
@Autowired
private CpLabelMapper cpLabelMapper;
@Autowired
private CpTextService cpTextService;
@Override
public IPage<CpArticleVo> findByPage(Page<CpArticleVo> page, String titleKeyword) {
public IPage<CpArticleVo> findByPage(Page<CpArticleVo> page, CpArticleDTO cpArticleDTO) {
// 执行分页查询
IPage<CpArticleVo> cpArticleVoIPage = cpArticleMapper.selectVoPage(page, titleKeyword);
IPage<CpArticleVo> cpArticleVoIPage = cpArticleMapper.selectVoPage(page, cpArticleDTO);
return cpArticleVoIPage;
}
@ -47,6 +59,77 @@ public class CpArticleServiceImpl extends ServiceImpl<CpArticleMapper, CpArticle
return entity;
}
@Override
public ResponseEntity<?> updateById(CpArticleDTO entity, BindingResult result) {
try {
// 处理参数校验错误
if (result.hasErrors()) {
Map<String, Object> error = new HashMap<>();
error.put("code", 400);
// 提取第一个错误信息
FieldError firstError = result.getFieldErrors().get(0);
error.put("msg", firstError.getField() + "" + firstError.getDefaultMessage());
return ResponseEntity.badRequest().body(error);
}
// 校验ID是否存在
if (entity.getId() == null) {
Map<String, Object> error = new HashMap<>();
error.put("code", 400);
error.put("msg", "id不能为空");
return ResponseEntity.badRequest().body(error);
}
// 查找标签信息
CpLabel cpLabelVo = cpLabelMapper.findById(entity.getLabelId());
if (cpLabelVo == null) {
Map<String, Object> error = new HashMap<>();
error.put("code", 400);
error.put("msg", "标签不存在");
return ResponseEntity.badRequest().body(error);
}
//更新模块信息
cpLabelMapper.updateModuleIdById(entity.getModuleId());
// 校验文章是否存在
CpArticle existingArticle = getById(entity.getId());
if (existingArticle == null) {
Map<String, Object> error = new HashMap<>();
error.put("code", 404);
error.put("msg", "文章不存在");
return ResponseEntity.status(404).body(error);
}
// 更新文章内容
if (existingArticle.getTextid() == null) {
Map<String, Object> error = new HashMap<>();
error.put("code", 400);
error.put("msg", "文章内容ID不存在");
return ResponseEntity.badRequest().body(error);
}
boolean textUpdated = cpTextService.lambdaUpdate()
.set(CpText::getText, entity.getText())
.eq(CpText::getId, existingArticle.getTextid())
.update();
if (!textUpdated) {
Map<String, Object> error = new HashMap<>();
error.put("code", 500);
error.put("msg", "文章内容更新失败");
return ResponseEntity.status(500).body(error);
}
cpArticleMapper.updateById(entity);
return ResponseEntity.ok().build();
} catch (Exception e) {
LOG.error("修改文章失败", e);
Map<String, Object> error = new HashMap<>();
error.put("code", 500);
error.put("msg", "修改文章失败:" + e.getMessage());
return ResponseEntity.status(500).body(error);
}
}
@Override
public List<CpArticle> list(CpArticle example) {
// 转换example为QueryWrapper条件

View File

@ -20,17 +20,23 @@
a.*,
t.text,
l.name AS labelName,
m.id AS moduleId, -- 映射模块ID
m.module_name AS moduleName -- 映射模块名称
m.id AS moduleId, -- 映射模块ID
m.module_name AS moduleName -- 映射模块名称
FROM cp_article a
LEFT JOIN cp_text t ON a.textid = t.id
LEFT JOIN cp_label l ON a.cplabel_id = l.id
LEFT JOIN cp_module m ON l.module_id = m.id -- 添加模块表关联
LEFT JOIN cp_module m ON l.module_id = m.id -- 添加模块表关联
WHERE a.del_flag = 0
<if test="titleKeyword != null and titleKeyword != ''">
AND a.title LIKE CONCAT('%', #{titleKeyword}, '%')
<if test="dto.title != null and dto.title != ''">
AND a.title LIKE CONCAT('%', #{dto.title}, '%')
</if>
ORDER BY create_time DESC
<if test="dto.moduleId != null and dto.moduleId != 0 ">
AND m.id =#{dto.moduleId}
</if>
<if test="dto.labelId != null and dto.labelId != 0">
AND l.id =#{dto.labelId}
</if>
ORDER BY a.create_time DESC
</select>
<!--通过文章id查询文章-->
@ -55,4 +61,20 @@
WHERE id = #{id}
AND del_flag = 0 -- 确保只更新未删除的文章
</update>
<!--修改文章-->
<update id="updateById" parameterType="com.aircraft.modules.article.domain.dto.CpArticleDTO">
UPDATE cp_article
<set>
<if test="title != null">title = #{title},</if>
<if test="photo != null">photo = #{photo},</if>
<if test="articleType != null">article_type = #{articleType},</if>
<if test="url != null">url = #{url},</if>
<if test="labelId != null">cplabel_id = #{labelId},</if>
<if test="top != null">top = #{top},</if>
update_time = NOW()
</set>
WHERE id = #{id}
AND del_flag = 0
</update>
</mapper>

View File

@ -28,4 +28,11 @@
</if>
ORDER BY create_time DESC
</select>
<!--更新对应的模块id-->
<update id="updateModuleIdById">
UPDATE cp_label
SET module_id = #{moduleId}
WHERE id = #{id}
AND del_flag = 0
</update>
</mapper>