NoticeServiceImpl.java 3.6 KB

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