feat(content): 添加内容服务参数验证和空值处理

- 在 CmsContentServiceImpl 中多个方法添加 Assert.notNull 参数验证
- 为 queryById、queryByIdWithPermission、update 等方法添加内容ID非空检查
- 处理点赞数为空时的默认值设置,避免空指针异常
- 在 controller 层修复注释参数名大小写问题
```
This commit is contained in:
wangzhiwei 2026-03-16 11:09:07 +08:00
parent ed220c9981
commit a92b668ac3
2 changed files with 19 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package com.kexue.skills.controller;
import com.github.pagehelper.PageInfo;
import com.kexue.skills.annotation.RequireAuth;
import com.kexue.skills.common.Assert;
import com.kexue.skills.common.CommonResult;
import com.kexue.skills.entity.CmsContent;
import com.kexue.skills.entity.base.IdDto;
@ -289,7 +290,7 @@ public class CmsContentController {
/**
* 获取CmsContent的content字段内容
*
* @param QueryContentDto 包含contentId和languageType的DTO对象
* @param queryContentDto 包含contentId和languageType的DTO对象
* @return content或contentEn字段的内容
*/
@PostMapping("/getContent")

View File

@ -3,6 +3,7 @@ package com.kexue.skills.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.kexue.skills.common.Assert;
import com.kexue.skills.common.LoginUserCacheUtil;
import com.kexue.skills.entity.CmsContent;
import com.kexue.skills.entity.CmsContentView;
@ -218,6 +219,7 @@ public class CmsContentServiceImpl implements CmsContentService {
*/
@Override
public CmsContent queryById(Long contentId) {
Assert.notNull(contentId, "内容ID不能为空");
CmsContent content = this.cmsContentMapper.queryById(contentId);
if (content == null) {
return null;
@ -265,6 +267,7 @@ public class CmsContentServiceImpl implements CmsContentService {
* @return 实例对象
*/
public CmsContent queryByIdWithPermission(Long contentId, Long userId) {
Assert.notNull(contentId, "内容ID不能为空");
CmsContent content = this.cmsContentMapper.queryById(contentId);
if (content == null) {
return null;
@ -324,6 +327,7 @@ public class CmsContentServiceImpl implements CmsContentService {
*/
@Override
public CmsContent update(CmsContent cmsContent) {
Assert.notNull(cmsContent.getContentId(), "内容ID不能为空");
// 设置更新时间
cmsContent.setUpdateTime(new Date());
// 更新数据
@ -344,6 +348,7 @@ public class CmsContentServiceImpl implements CmsContentService {
*/
@Override
public int updateAuditStatus(Long contentId, Integer auditStatus, Long reviewerId, String reviewerName, String auditComment, String updateBy) {
Assert.notNull(contentId, "内容ID不能为空");
return this.cmsContentMapper.updateAuditStatus(contentId, auditStatus, reviewerId, reviewerName, auditComment, updateBy);
}
@ -358,6 +363,7 @@ public class CmsContentServiceImpl implements CmsContentService {
*/
@Override
public int updatePublishStatus(Long contentId, Integer publishStatus, String publishTime, String updateBy) {
Assert.notNull(contentId, "内容ID不能为空");
return this.cmsContentMapper.updatePublishStatus(contentId, publishStatus, publishTime, updateBy);
}
@ -369,6 +375,7 @@ public class CmsContentServiceImpl implements CmsContentService {
*/
@Override
public int increaseViewCount(Long contentId) {
Assert.notNull(contentId, "内容ID不能为空");
// 增加阅读量
int result = this.cmsContentMapper.increaseViewCount(contentId);
@ -393,6 +400,7 @@ public class CmsContentServiceImpl implements CmsContentService {
*/
@Override
public int logicDeleteById(Long contentId, String updateBy) {
Assert.notNull(contentId, "内容ID不能为空");
return this.cmsContentMapper.logicDeleteById(contentId, updateBy);
}
@ -404,6 +412,7 @@ public class CmsContentServiceImpl implements CmsContentService {
*/
@Override
public int deleteById(Long contentId) {
Assert.notNull(contentId, "内容ID不能为空");
return this.cmsContentMapper.deleteById(contentId);
}
@ -415,6 +424,7 @@ public class CmsContentServiceImpl implements CmsContentService {
*/
@Override
public int addFavorite(Long contentId) {
Assert.notNull(contentId, "内容ID不能为空");
// 获取当前登录用户ID
Long userId = Long.parseLong(StpUtil.getLoginId().toString());
// 获取当前登录用户名
@ -425,6 +435,9 @@ public class CmsContentServiceImpl implements CmsContentService {
if (content == null) {
return 0;
}
if(Objects.isNull(content.getLikeCount())){
content.setLikeCount(0);
}
// 检查用户是否已经收藏过该内容
CmsContentLike existingLike = cmsContentLikeMapper.queryByUserIdAndContentId(userId, contentId);
@ -475,6 +488,7 @@ public class CmsContentServiceImpl implements CmsContentService {
*/
@Override
public int removeFavorite(Long contentId) {
Assert.notNull(contentId, "内容ID不能为空");
// 获取当前登录用户ID
Long userId = Long.parseLong(StpUtil.getLoginId().toString());
@ -513,6 +527,7 @@ public class CmsContentServiceImpl implements CmsContentService {
*/
@Override
public boolean isFavorited(Long contentId) {
Assert.notNull(contentId, "内容ID不能为空");
try {
// 获取当前登录用户ID
Long userId = Long.parseLong(StpUtil.getLoginId().toString());
@ -532,6 +547,7 @@ public class CmsContentServiceImpl implements CmsContentService {
*/
@Override
public int addView(Long contentId) {
Assert.notNull(contentId, "内容ID不能为空");
try {
// 获取当前登录用户ID
Long userId = Long.parseLong(StpUtil.getLoginId().toString());
@ -903,6 +919,7 @@ public class CmsContentServiceImpl implements CmsContentService {
@Override
public String getContent(Long contentId, Integer languageType) {
Assert.notNull(contentId, "内容ID不能为空");
CmsContent cmsContent = this.cmsContentMapper.queryById(contentId);
if (cmsContent == null) {
return null;