diff --git a/src/main/java/cn/teammodel/common/PK.java b/src/main/java/cn/teammodel/common/PK.java index b0a1d04..43c5a63 100644 --- a/src/main/java/cn/teammodel/common/PK.java +++ b/src/main/java/cn/teammodel/common/PK.java @@ -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"; /** * 构建分区键 diff --git a/src/main/java/cn/teammodel/controller/frontend/NewsController.java b/src/main/java/cn/teammodel/controller/frontend/NewsController.java new file mode 100644 index 0000000..6061ffa --- /dev/null +++ b/src/main/java/cn/teammodel/controller/frontend/NewsController.java @@ -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> listNews() { + List Newss = newsService.listNews(); + return R.success(Newss); + } + + @PostMapping("create") + @ApiOperation("创建聊天应用") + public R createNews(@RequestBody @Valid CreateNewsDto createNewsDto) { + News News = newsService.createNews(createNewsDto); + return R.success(News); + } + + @PostMapping("update") + @ApiOperation("更新聊天应用") + public R updateNews(@RequestBody @Valid UpdateNewsDto updateNewsDto) { + News News = newsService.updateNews(updateNewsDto); + return R.success(News); + } + + @PostMapping("remove") + @ApiOperation("删除聊天应用") + public R deleteNews(@RequestBody @Valid IdRequest idRequest) { + newsService.deleteNews(idRequest); + return R.success("删除应用成功"); + } +} diff --git a/src/main/java/cn/teammodel/model/dto/news/CreateNewsDto.java b/src/main/java/cn/teammodel/model/dto/news/CreateNewsDto.java new file mode 100644 index 0000000..3cb3cad --- /dev/null +++ b/src/main/java/cn/teammodel/model/dto/news/CreateNewsDto.java @@ -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; +} \ No newline at end of file diff --git a/src/main/java/cn/teammodel/model/dto/news/UpdateNewsDto.java b/src/main/java/cn/teammodel/model/dto/news/UpdateNewsDto.java new file mode 100644 index 0000000..59608af --- /dev/null +++ b/src/main/java/cn/teammodel/model/dto/news/UpdateNewsDto.java @@ -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; +} \ No newline at end of file diff --git a/src/main/java/cn/teammodel/model/entity/news/News.java b/src/main/java/cn/teammodel/model/entity/news/News.java new file mode 100644 index 0000000..44c6d43 --- /dev/null +++ b/src/main/java/cn/teammodel/model/entity/news/News.java @@ -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; +} diff --git a/src/main/java/cn/teammodel/repository/NewsRepository.java b/src/main/java/cn/teammodel/repository/NewsRepository.java new file mode 100644 index 0000000..b8b36c0 --- /dev/null +++ b/src/main/java/cn/teammodel/repository/NewsRepository.java @@ -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 findNewsByIdAndCode(String id, String code); + + List findAllByCode(String code); +} diff --git a/src/main/java/cn/teammodel/service/NewsService.java b/src/main/java/cn/teammodel/service/NewsService.java new file mode 100644 index 0000000..0f8d0f5 --- /dev/null +++ b/src/main/java/cn/teammodel/service/NewsService.java @@ -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 listNews(); + + News createNews(CreateNewsDto createNewsDto); + + News updateNews(UpdateNewsDto updateNewsDto); + + void deleteNews(IdRequest idRequest); +} diff --git a/src/main/java/cn/teammodel/service/impl/NewsServiceImpl.java b/src/main/java/cn/teammodel/service/impl/NewsServiceImpl.java new file mode 100644 index 0000000..a179266 --- /dev/null +++ b/src/main/java/cn/teammodel/service/impl/NewsServiceImpl.java @@ -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 listNews() { + User user = SecurityUtil.getLoginUser(); + String schoolId = user.getSchoolId(); + List 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) { + + } +}