From a92b668ac381add93112832cfa10100111cc4ec6 Mon Sep 17 00:00:00 2001 From: wangzhiwei Date: Mon, 16 Mar 2026 11:09:07 +0800 Subject: [PATCH] =?UTF-8?q?```=20feat(content):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E6=9C=8D=E5=8A=A1=E5=8F=82=E6=95=B0=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E5=92=8C=E7=A9=BA=E5=80=BC=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 CmsContentServiceImpl 中多个方法添加 Assert.notNull 参数验证 - 为 queryById、queryByIdWithPermission、update 等方法添加内容ID非空检查 - 处理点赞数为空时的默认值设置,避免空指针异常 - 在 controller 层修复注释参数名大小写问题 ``` --- .../skills/controller/CmsContentController.java | 3 ++- .../service/impl/CmsContentServiceImpl.java | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/kexue/skills/controller/CmsContentController.java b/src/main/java/com/kexue/skills/controller/CmsContentController.java index 148df7a..5143f37 100644 --- a/src/main/java/com/kexue/skills/controller/CmsContentController.java +++ b/src/main/java/com/kexue/skills/controller/CmsContentController.java @@ -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") diff --git a/src/main/java/com/kexue/skills/service/impl/CmsContentServiceImpl.java b/src/main/java/com/kexue/skills/service/impl/CmsContentServiceImpl.java index e9cd790..3000678 100644 --- a/src/main/java/com/kexue/skills/service/impl/CmsContentServiceImpl.java +++ b/src/main/java/com/kexue/skills/service/impl/CmsContentServiceImpl.java @@ -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;