feat: 新闻的增删改查

11111
winter 10 months ago
parent 079f2fbf6f
commit 53f9492eb0

@ -27,6 +27,7 @@ public interface PK {
String WEEK_DUTY = "Duty";
String WEEK_DUTY_RECORD = "DutyRecord-%s";
String CHAT_APP = "ChatApp";
String NEWS = "News-%s";
/**
*

@ -0,0 +1,57 @@
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;
import cn.teammodel.service.NewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
/**
* @author winter
* @create 2024-02-26 17:04
*/
@RestController
@RequestMapping("/news")
@Api(tags = "新闻设置")
public class NewsController {
@Resource
private NewsService newsService;
@GetMapping("list")
@ApiOperation("查询新闻")
public R<List<News>> listNews() {
List<News> Newss = newsService.listNews();
return R.success(Newss);
}
@PostMapping("create")
@ApiOperation("创建聊天应用")
public R<News> createNews(@RequestBody @Valid CreateNewsDto createNewsDto) {
News News = newsService.createNews(createNewsDto);
return R.success(News);
}
@PostMapping("update")
@ApiOperation("更新聊天应用")
public R<News> updateNews(@RequestBody @Valid UpdateNewsDto updateNewsDto) {
News News = newsService.updateNews(updateNewsDto);
return R.success(News);
}
@PostMapping("remove")
@ApiOperation("删除聊天应用")
public R<String> deleteNews(@RequestBody @Valid IdRequest idRequest) {
newsService.deleteNews(idRequest);
return R.success("删除应用成功");
}
}

@ -0,0 +1,23 @@
package cn.teammodel.model.dto.news;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotBlank;
@Data
public class CreateNewsDto {
@ApiModelProperty("新闻标题")
@NotBlank(message = "新闻标题不能为空")
private String title;
@ApiModelProperty("新闻描述")
private String desc;
@ApiModelProperty("新闻正文")
@NotBlank(message = "新闻正文不能为空")
private String content;
@ApiModelProperty("新闻封面")
private String cover;
}

@ -0,0 +1,26 @@
package cn.teammodel.model.dto.news;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotBlank;
@Data
public class UpdateNewsDto {
@ApiModelProperty("新闻 id")
private String id;
@ApiModelProperty("新闻标题")
@NotBlank(message = "新闻标题不能为空")
private String title;
@ApiModelProperty("新闻描述")
private String desc;
@ApiModelProperty("新闻正文")
@NotBlank(message = "新闻正文不能为空")
private String content;
@ApiModelProperty("新闻封面")
private String cover;
}

@ -0,0 +1,26 @@
package cn.teammodel.model.entity.news;
import cn.teammodel.model.entity.BaseItem;
import com.azure.spring.data.cosmos.core.mapping.Container;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* code: News-{schoolId}
* @author winter
* @create 2024-02-26 17:05
*/
@EqualsAndHashCode(callSuper = true)
@Container(containerName = "School")
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class News extends BaseItem {
private String title;
private String desc;
private String content;
private String cover;
private String creator;
private String creatorId;
private Long createTime;
}

@ -0,0 +1,18 @@
package cn.teammodel.repository;
import cn.teammodel.model.entity.news.News;
import com.azure.spring.data.cosmos.repository.CosmosRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author winter
* @create 2023-11-28 17:39
*/
@Repository
public interface NewsRepository extends CosmosRepository<News, String> {
News findNewsByIdAndCode(String id, String code);
List<News> findAllByCode(String code);
}

@ -0,0 +1,24 @@
package cn.teammodel.service;
import cn.teammodel.common.IdRequest;
import cn.teammodel.model.dto.news.CreateNewsDto;
import cn.teammodel.model.dto.news.UpdateNewsDto;
import cn.teammodel.model.entity.news.News;
import java.util.List;
/**
* @author winter
* @create 2024-02-26 17:10
*/
public interface NewsService {
List<News> listNews();
News createNews(CreateNewsDto createNewsDto);
News updateNews(UpdateNewsDto updateNewsDto);
void deleteNews(IdRequest idRequest);
}

@ -0,0 +1,77 @@
package cn.teammodel.service.impl;
import cn.teammodel.common.ErrorCode;
import cn.teammodel.common.IdRequest;
import cn.teammodel.common.PK;
import cn.teammodel.config.exception.ServiceException;
import cn.teammodel.model.dto.news.CreateNewsDto;
import cn.teammodel.model.dto.news.UpdateNewsDto;
import cn.teammodel.model.entity.User;
import cn.teammodel.model.entity.news.News;
import cn.teammodel.repository.NewsRepository;
import cn.teammodel.security.utils.SecurityUtil;
import cn.teammodel.service.NewsService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.Instant;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author winter
* @create 2024-02-26 17:10
*/
@Service
public class NewsServiceImpl implements NewsService {
@Resource
private NewsRepository newsRepository;
@Override
public List<News> listNews() {
User user = SecurityUtil.getLoginUser();
String schoolId = user.getSchoolId();
List<News> newsList = newsRepository.findAllByCode(String.format(PK.NEWS, schoolId));
// 降序排序
newsList = newsList.stream().sorted((o1, o2) -> o2.getCreateTime().compareTo(o1.getCreateTime())).collect(Collectors.toList());
return newsList;
}
@Override
public News createNews(CreateNewsDto createNewsDto) {
User user = SecurityUtil.getLoginUser();
News newApp = new News();
BeanUtils.copyProperties(createNewsDto, newApp);
newApp.setCreator(user.getName());
newApp.setCreatorId(user.getId());
newApp.setCreateTime(Instant.now().toEpochMilli());
newApp.setCode(String.format(PK.NEWS, user.getSchoolId()));
return newsRepository.save(newApp);
}
@Override
public News updateNews(UpdateNewsDto updateNewsDto) {
String id = updateNewsDto.getId();
User user = SecurityUtil.getLoginUser();
String userId = user.getId();
String schoolId = user.getSchoolId();
News news = newsRepository.findNewsByIdAndCode(id, String.format(PK.NEWS, schoolId));
if (ObjectUtils.isEmpty(news)) {
throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "该新闻不存在");
}
if (!userId.equals(news.getCreatorId())) {
throw new ServiceException(ErrorCode.NO_AUTH_ERROR.getCode(), "您没有权限修改该新闻");
}
BeanUtils.copyProperties(updateNewsDto, news);
return newsRepository.save(news);
}
@Override
public void deleteNews(IdRequest idRequest) {
}
}
Loading…
Cancel
Save