NoticeServiceImpl.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package nju.seec.helper.service.impl;
  2. import nju.seec.helper.aspect.auth.LoginUser;
  3. import nju.seec.helper.dao.ChooseDAO;
  4. import nju.seec.helper.dao.CourseDAO;
  5. import nju.seec.helper.dao.NoticeDAO;
  6. import nju.seec.helper.dto.notice.NoticeDTO;
  7. import nju.seec.helper.entity.Course;
  8. import nju.seec.helper.entity.Notice;
  9. import nju.seec.helper.enums.ExceptionType;
  10. import nju.seec.helper.enums.MessageType;
  11. import nju.seec.helper.exception.HelperException;
  12. import nju.seec.helper.service.MessageService;
  13. import nju.seec.helper.service.NoticeService;
  14. import nju.seec.helper.vo.NoticeVO;
  15. import org.springframework.data.domain.Page;
  16. import org.springframework.data.domain.Pageable;
  17. import org.springframework.stereotype.Service;
  18. import org.springframework.transaction.annotation.Transactional;
  19. import java.util.Set;
  20. /**
  21. * @author cst
  22. */
  23. @Service
  24. public class NoticeServiceImpl implements NoticeService {
  25. private final CourseDAO courseDAO;
  26. private final ChooseDAO chooseDAO;
  27. private final NoticeDAO noticeDAO;
  28. private final MessageService messageService;
  29. public NoticeServiceImpl(CourseDAO courseDAO, ChooseDAO chooseDAO, NoticeDAO noticeDAO, MessageService messageService) {
  30. this.courseDAO = courseDAO;
  31. this.chooseDAO = chooseDAO;
  32. this.noticeDAO = noticeDAO;
  33. this.messageService = messageService;
  34. }
  35. @Transactional(rollbackFor = Exception.class)
  36. @Override
  37. public NoticeVO createNotice(LoginUser user, NoticeDTO noticeDTO) {
  38. Course course = courseDAO.findCourseById(noticeDTO.getCourseId());
  39. Notice notice = new Notice()
  40. .setCourseId(noticeDTO.getCourseId())
  41. .setTeacherId(user.getId())
  42. .setTitle(noticeDTO.getTitle())
  43. .setContent(noticeDTO.getContent());
  44. if (!existsAddAuth(user, notice)) {
  45. throw HelperException.of(ExceptionType.FORBIDDEN, "您无权创建该公告");
  46. }
  47. Set<Long> studentIds = chooseDAO.findStudentIdsByCourseId(noticeDTO.getCourseId());
  48. messageService.createMessage(studentIds, MessageType.NOTICE_NEW, String.format("[%s] 发布了新公告", course.getName()), course.getId());
  49. return new NoticeVO(noticeDAO.save(notice));
  50. }
  51. @Transactional(rollbackFor = Exception.class)
  52. @Override
  53. public NoticeVO modifyNotice(LoginUser user, Long noticeId, NoticeDTO noticeDTO) {
  54. Notice notice = noticeDAO.findNoticeById(noticeId);
  55. if (!existsUpdateAuth(user, notice)) {
  56. throw HelperException.of(ExceptionType.FORBIDDEN, "您无权修改该公告");
  57. }
  58. return new NoticeVO(
  59. noticeDAO.save(
  60. notice.setTitle(noticeDTO.getTitle())
  61. .setContent(noticeDTO.getContent())
  62. )
  63. );
  64. }
  65. @Transactional(readOnly = true)
  66. @Override
  67. public Page<NoticeVO> getNoticesByCourse(LoginUser user, Long courseId, String key, Pageable pageable) {
  68. return noticeDAO.findByCourseIdAndKey(courseId, key, pageable).map(NoticeVO::new);
  69. }
  70. @Transactional(rollbackFor = Exception.class)
  71. @Override
  72. public void deleteNotice(LoginUser user, Long noticeId) {
  73. Notice notice = noticeDAO.findNoticeById(noticeId);
  74. if (!existsDelAuth(user, notice)) {
  75. throw HelperException.of(ExceptionType.FORBIDDEN, "您无权删除该公告");
  76. }
  77. noticeDAO.delete(notice);
  78. }
  79. @Transactional(readOnly = true)
  80. @Override
  81. public NoticeVO getOneNotice(LoginUser user, Long noticeId) {
  82. return new NoticeVO(noticeDAO.findNoticeById(noticeId));
  83. }
  84. @Override
  85. public boolean existsAddAuth(LoginUser user, Notice notice) {
  86. return courseDAO.findCourseById(notice.getCourseId()).getTeacher().getId().equals(user.getId());
  87. }
  88. @Override
  89. public boolean existsDelAuth(LoginUser user, Notice notice) {
  90. return notice.getTeacherId().equals(user.getId());
  91. }
  92. @Override
  93. public boolean existsUpdateAuth(LoginUser user, Notice notice) {
  94. return notice.getTeacherId().equals(user.getId());
  95. }
  96. @Override
  97. public boolean existsGetAuth(LoginUser user, Notice notice) {
  98. return true;
  99. }
  100. }