From 21790d56668c6276b64e9b204af54de7bbe05c20 Mon Sep 17 00:00:00 2001 From: winter <2436197699@qq.com> Date: Tue, 27 Feb 2024 10:57:25 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E5=96=84=20News=20CRUD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/frontend/NewsController.java | 4 +--- .../cn/teammodel/service/impl/NewsServiceImpl.java | 13 +++++++++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/cn/teammodel/controller/frontend/NewsController.java b/src/main/java/cn/teammodel/controller/frontend/NewsController.java index 6061ffa..cc03a31 100644 --- a/src/main/java/cn/teammodel/controller/frontend/NewsController.java +++ b/src/main/java/cn/teammodel/controller/frontend/NewsController.java @@ -2,8 +2,6 @@ package cn.teammodel.controller.frontend; import cn.teammodel.common.IdRequest; import cn.teammodel.common.R; -import cn.teammodel.model.dto.ai.CreateNewsDto; -import cn.teammodel.model.dto.ai.UpdateNewsDto; import cn.teammodel.model.dto.news.CreateNewsDto; import cn.teammodel.model.dto.news.UpdateNewsDto; import cn.teammodel.model.entity.news.News; @@ -52,6 +50,6 @@ public class NewsController { @ApiOperation("删除聊天应用") public R deleteNews(@RequestBody @Valid IdRequest idRequest) { newsService.deleteNews(idRequest); - return R.success("删除应用成功"); + return R.success("删除新闻成功"); } } diff --git a/src/main/java/cn/teammodel/service/impl/NewsServiceImpl.java b/src/main/java/cn/teammodel/service/impl/NewsServiceImpl.java index a179266..798664d 100644 --- a/src/main/java/cn/teammodel/service/impl/NewsServiceImpl.java +++ b/src/main/java/cn/teammodel/service/impl/NewsServiceImpl.java @@ -63,7 +63,7 @@ public class NewsServiceImpl implements NewsService { if (ObjectUtils.isEmpty(news)) { throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "该新闻不存在"); } - if (!userId.equals(news.getCreatorId())) { + if (!userId.equals(news.getCreatorId()) && !user.getRoles().contains("admin")) { throw new ServiceException(ErrorCode.NO_AUTH_ERROR.getCode(), "您没有权限修改该新闻"); } BeanUtils.copyProperties(updateNewsDto, news); @@ -72,6 +72,15 @@ public class NewsServiceImpl implements NewsService { @Override public void deleteNews(IdRequest idRequest) { - + String newsId = idRequest.getId(); + User user = SecurityUtil.getLoginUser(); + String userId = user.getId(); + String schoolId = user.getSchoolId(); + News news = newsRepository.findNewsByIdAndCode(newsId, String.format(PK.NEWS, schoolId)); + if (userId.equals(news.getCreatorId()) && user.getRoles().contains("admin")) { + newsRepository.deleteById(newsId, PK.buildOf(PK.NEWS, schoolId)); + } else { + throw new ServiceException(ErrorCode.NO_AUTH_ERROR.getCode(), "您没有权限删除该新闻"); + } } }