refactor: 新闻新增学段字段

11111
winter 10 months ago
parent 21790d5666
commit a539afb9e8

@ -25,29 +25,29 @@ public class NewsController {
@Resource @Resource
private NewsService newsService; private NewsService newsService;
@GetMapping("list") @GetMapping("list/{periodId}")
@ApiOperation("查询新闻") @ApiOperation("查询新闻")
public R<List<News>> listNews() { public R<List<News>> listNews(@PathVariable String periodId) {
List<News> Newss = newsService.listNews(); List<News> Newss = newsService.listNews(periodId);
return R.success(Newss); return R.success(Newss);
} }
@PostMapping("create") @PostMapping("create")
@ApiOperation("创建聊天应用") @ApiOperation("创建新闻")
public R<News> createNews(@RequestBody @Valid CreateNewsDto createNewsDto) { public R<News> createNews(@RequestBody @Valid CreateNewsDto createNewsDto) {
News News = newsService.createNews(createNewsDto); News News = newsService.createNews(createNewsDto);
return R.success(News); return R.success(News);
} }
@PostMapping("update") @PostMapping("update")
@ApiOperation("更新聊天应用") @ApiOperation("更新新闻")
public R<News> updateNews(@RequestBody @Valid UpdateNewsDto updateNewsDto) { public R<News> updateNews(@RequestBody @Valid UpdateNewsDto updateNewsDto) {
News News = newsService.updateNews(updateNewsDto); News News = newsService.updateNews(updateNewsDto);
return R.success(News); return R.success(News);
} }
@PostMapping("remove") @PostMapping("remove")
@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("删除新闻成功");

@ -11,6 +11,10 @@ public class CreateNewsDto {
@NotBlank(message = "新闻标题不能为空") @NotBlank(message = "新闻标题不能为空")
private String title; private String title;
@ApiModelProperty("学段ID")
@NotBlank(message = "学段ID不能为空")
private String periodId;
@ApiModelProperty("新闻描述") @ApiModelProperty("新闻描述")
private String desc; private String desc;

@ -14,6 +14,10 @@ public class UpdateNewsDto {
@NotBlank(message = "新闻标题不能为空") @NotBlank(message = "新闻标题不能为空")
private String title; private String title;
@ApiModelProperty("学段ID")
@NotBlank(message = "学段ID不能为空")
private String periodId;
@ApiModelProperty("新闻描述") @ApiModelProperty("新闻描述")
private String desc; private String desc;

@ -17,6 +17,7 @@ import lombok.EqualsAndHashCode;
@JsonInclude(JsonInclude.Include.NON_NULL) @JsonInclude(JsonInclude.Include.NON_NULL)
public class News extends BaseItem { public class News extends BaseItem {
private String title; private String title;
private String periodId;
private String desc; private String desc;
private String content; private String content;
private String cover; private String cover;

@ -15,4 +15,5 @@ public interface NewsRepository extends CosmosRepository<News, String> {
News findNewsByIdAndCode(String id, String code); News findNewsByIdAndCode(String id, String code);
List<News> findAllByCode(String code); List<News> findAllByCode(String code);
List<News> findAllByCodeAndPeriodId(String code, String periodId);
} }

@ -13,8 +13,7 @@ import java.util.List;
*/ */
public interface NewsService { public interface NewsService {
List<News> listNews(String periodId);
List<News> listNews();
News createNews(CreateNewsDto createNewsDto); News createNews(CreateNewsDto createNewsDto);

@ -12,6 +12,7 @@ import cn.teammodel.repository.NewsRepository;
import cn.teammodel.security.utils.SecurityUtil; import cn.teammodel.security.utils.SecurityUtil;
import cn.teammodel.service.NewsService; import cn.teammodel.service.NewsService;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -30,13 +31,19 @@ public class NewsServiceImpl implements NewsService {
@Resource @Resource
private NewsRepository newsRepository; private NewsRepository newsRepository;
@Override @Override
public List<News> listNews() { public List<News> listNews(String periodId) {
User user = SecurityUtil.getLoginUser(); User user = SecurityUtil.getLoginUser();
String schoolId = user.getSchoolId(); String schoolId = user.getSchoolId();
List<News> newsList = newsRepository.findAllByCode(String.format(PK.NEWS, schoolId)); List<News> 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()); res = res.stream().sorted((o1, o2) -> o2.getCreateTime().compareTo(o1.getCreateTime())).collect(Collectors.toList());
return newsList; return res;
} }
@Override @Override

@ -144,15 +144,15 @@ public class TestWithoutSpring {
@Test @Test
public void testLocalDateTime() { public void testLocalDateTime() {
List<School.Semester> semesters = Arrays.asList( List<School.Semester> semesters = Arrays.asList(
new School.Semester("1", "上学期", 1, 2, 1), new School.Semester("1", "上学期", 0, 2, 1), // 2024-3 , 正确: 2023-3
new School.Semester("3", "下学期", 0, 8, 1), new School.Semester("3", "下学期", 1, 8, 1)
new School.Semester("4", "下学期", 0, 11, 1), // new School.Semester("4", "下学期", 0, 11, 1),
new School.Semester("2", "下学期", 0, 5, 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); System.out.println(id);
} }

Loading…
Cancel
Save