feat: 完善 News CRUD

11111
winter 10 months ago
parent 53f9492eb0
commit 21790d5666

@ -2,8 +2,6 @@ package cn.teammodel.controller.frontend;
import cn.teammodel.common.IdRequest; import cn.teammodel.common.IdRequest;
import cn.teammodel.common.R; 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.CreateNewsDto;
import cn.teammodel.model.dto.news.UpdateNewsDto; import cn.teammodel.model.dto.news.UpdateNewsDto;
import cn.teammodel.model.entity.news.News; import cn.teammodel.model.entity.news.News;
@ -52,6 +50,6 @@ public class NewsController {
@ApiOperation("删除聊天应用") @ApiOperation("删除聊天应用")
public R<String> deleteNews(@RequestBody @Valid IdRequest idRequest) { public R<String> deleteNews(@RequestBody @Valid IdRequest idRequest) {
newsService.deleteNews(idRequest); newsService.deleteNews(idRequest);
return R.success("删除应用成功"); return R.success("删除新闻成功");
} }
} }

@ -63,7 +63,7 @@ public class NewsServiceImpl implements NewsService {
if (ObjectUtils.isEmpty(news)) { if (ObjectUtils.isEmpty(news)) {
throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "该新闻不存在"); 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(), "您没有权限修改该新闻"); throw new ServiceException(ErrorCode.NO_AUTH_ERROR.getCode(), "您没有权限修改该新闻");
} }
BeanUtils.copyProperties(updateNewsDto, news); BeanUtils.copyProperties(updateNewsDto, news);
@ -72,6 +72,15 @@ public class NewsServiceImpl implements NewsService {
@Override @Override
public void deleteNews(IdRequest idRequest) { 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(), "您没有权限删除该新闻");
}
} }
} }

Loading…
Cancel
Save