diff --git a/src/main/java/cn/teammodel/controller/frontend/NewsController.java b/src/main/java/cn/teammodel/controller/frontend/NewsController.java index cc03a31..dacac5a 100644 --- a/src/main/java/cn/teammodel/controller/frontend/NewsController.java +++ b/src/main/java/cn/teammodel/controller/frontend/NewsController.java @@ -25,29 +25,29 @@ public class NewsController { @Resource private NewsService newsService; - @GetMapping("list") + @GetMapping("list/{periodId}") @ApiOperation("查询新闻") - public R> listNews() { - List Newss = newsService.listNews(); + public R> listNews(@PathVariable String periodId) { + List Newss = newsService.listNews(periodId); return R.success(Newss); } @PostMapping("create") - @ApiOperation("创建聊天应用") + @ApiOperation("创建新闻") public R createNews(@RequestBody @Valid CreateNewsDto createNewsDto) { News News = newsService.createNews(createNewsDto); return R.success(News); } @PostMapping("update") - @ApiOperation("更新聊天应用") + @ApiOperation("更新新闻") public R updateNews(@RequestBody @Valid UpdateNewsDto updateNewsDto) { News News = newsService.updateNews(updateNewsDto); return R.success(News); } @PostMapping("remove") - @ApiOperation("删除聊天应用") + @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 index 3cb3cad..bf702e3 100644 --- a/src/main/java/cn/teammodel/model/dto/news/CreateNewsDto.java +++ b/src/main/java/cn/teammodel/model/dto/news/CreateNewsDto.java @@ -11,6 +11,10 @@ public class CreateNewsDto { @NotBlank(message = "新闻标题不能为空") private String title; + @ApiModelProperty("学段ID") + @NotBlank(message = "学段ID不能为空") + private String periodId; + @ApiModelProperty("新闻描述") private String desc; diff --git a/src/main/java/cn/teammodel/model/dto/news/UpdateNewsDto.java b/src/main/java/cn/teammodel/model/dto/news/UpdateNewsDto.java index 59608af..a855424 100644 --- a/src/main/java/cn/teammodel/model/dto/news/UpdateNewsDto.java +++ b/src/main/java/cn/teammodel/model/dto/news/UpdateNewsDto.java @@ -14,6 +14,10 @@ public class UpdateNewsDto { @NotBlank(message = "新闻标题不能为空") private String title; + @ApiModelProperty("学段ID") + @NotBlank(message = "学段ID不能为空") + private String periodId; + @ApiModelProperty("新闻描述") private String desc; diff --git a/src/main/java/cn/teammodel/model/entity/news/News.java b/src/main/java/cn/teammodel/model/entity/news/News.java index 44c6d43..55d3aa1 100644 --- a/src/main/java/cn/teammodel/model/entity/news/News.java +++ b/src/main/java/cn/teammodel/model/entity/news/News.java @@ -17,6 +17,7 @@ import lombok.EqualsAndHashCode; @JsonInclude(JsonInclude.Include.NON_NULL) public class News extends BaseItem { private String title; + private String periodId; private String desc; private String content; private String cover; diff --git a/src/main/java/cn/teammodel/repository/NewsRepository.java b/src/main/java/cn/teammodel/repository/NewsRepository.java index b8b36c0..d28f859 100644 --- a/src/main/java/cn/teammodel/repository/NewsRepository.java +++ b/src/main/java/cn/teammodel/repository/NewsRepository.java @@ -15,4 +15,5 @@ public interface NewsRepository extends CosmosRepository { News findNewsByIdAndCode(String id, String code); List findAllByCode(String code); + List findAllByCodeAndPeriodId(String code, String periodId); } diff --git a/src/main/java/cn/teammodel/service/NewsService.java b/src/main/java/cn/teammodel/service/NewsService.java index 0f8d0f5..79b6e7a 100644 --- a/src/main/java/cn/teammodel/service/NewsService.java +++ b/src/main/java/cn/teammodel/service/NewsService.java @@ -13,8 +13,7 @@ import java.util.List; */ public interface NewsService { - - List listNews(); + List listNews(String periodId); News createNews(CreateNewsDto createNewsDto); diff --git a/src/main/java/cn/teammodel/service/impl/NewsServiceImpl.java b/src/main/java/cn/teammodel/service/impl/NewsServiceImpl.java index 798664d..5425bd9 100644 --- a/src/main/java/cn/teammodel/service/impl/NewsServiceImpl.java +++ b/src/main/java/cn/teammodel/service/impl/NewsServiceImpl.java @@ -12,6 +12,7 @@ import cn.teammodel.repository.NewsRepository; import cn.teammodel.security.utils.SecurityUtil; import cn.teammodel.service.NewsService; import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; @@ -30,13 +31,19 @@ public class NewsServiceImpl implements NewsService { @Resource private NewsRepository newsRepository; @Override - public List listNews() { + public List listNews(String periodId) { User user = SecurityUtil.getLoginUser(); String schoolId = user.getSchoolId(); - List newsList = newsRepository.findAllByCode(String.format(PK.NEWS, schoolId)); + List res; + + if (StringUtils.isBlank(periodId)) { + res = newsRepository.findAllByCode(String.format(PK.NEWS, schoolId)); + } else { + res = newsRepository.findAllByCodeAndPeriodId(String.format(PK.NEWS, schoolId), periodId); + } // 降序排序 - newsList = newsList.stream().sorted((o1, o2) -> o2.getCreateTime().compareTo(o1.getCreateTime())).collect(Collectors.toList()); - return newsList; + res = res.stream().sorted((o1, o2) -> o2.getCreateTime().compareTo(o1.getCreateTime())).collect(Collectors.toList()); + return res; } @Override diff --git a/src/test/java/cn/teammodel/TestWithoutSpring.java b/src/test/java/cn/teammodel/TestWithoutSpring.java index 489b835..2d14183 100644 --- a/src/test/java/cn/teammodel/TestWithoutSpring.java +++ b/src/test/java/cn/teammodel/TestWithoutSpring.java @@ -144,15 +144,15 @@ public class TestWithoutSpring { @Test public void testLocalDateTime() { List semesters = Arrays.asList( - new School.Semester("1", "上学期", 1, 2, 1), - new School.Semester("3", "下学期", 0, 8, 1), - new School.Semester("4", "下学期", 0, 11, 1), - new School.Semester("2", "下学期", 0, 5, 1) + new School.Semester("1", "上学期", 0, 2, 1), // 2024-3 , 正确: 2023-3 + new School.Semester("3", "下学期", 1, 8, 1) +// new School.Semester("4", "下学期", 0, 11, 1), +// new School.Semester("2", "下学期", 0, 5, 1) ); - LocalDate currentDate = LocalDate.of(2024, 11, 1); +// LocalDate currentDate = LocalDate.of(2024, 11, 1); - String id = SchoolDateUtil.calculateAcademicYearId(semesters, currentDate); + String id = SchoolDateUtil.calculateAcademicYearId(semesters, LocalDate.now()); System.out.println(id); }