package nju.seec.helper.service.impl; import nju.seec.helper.aspect.auth.LoginUser; import nju.seec.helper.dao.ChooseDAO; import nju.seec.helper.dao.CourseDAO; import nju.seec.helper.dao.NoticeDAO; import nju.seec.helper.dto.notice.NoticeDTO; import nju.seec.helper.entity.Course; import nju.seec.helper.entity.Notice; import nju.seec.helper.enums.ExceptionType; import nju.seec.helper.enums.MessageType; import nju.seec.helper.exception.HelperException; import nju.seec.helper.service.MessageService; import nju.seec.helper.service.NoticeService; import nju.seec.helper.vo.NoticeVO; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.Set; /** * @author cst */ @Service public class NoticeServiceImpl implements NoticeService { private final CourseDAO courseDAO; private final ChooseDAO chooseDAO; private final NoticeDAO noticeDAO; private final MessageService messageService; public NoticeServiceImpl(CourseDAO courseDAO, ChooseDAO chooseDAO, NoticeDAO noticeDAO, MessageService messageService) { this.courseDAO = courseDAO; this.chooseDAO = chooseDAO; this.noticeDAO = noticeDAO; this.messageService = messageService; } @Transactional(rollbackFor = Exception.class) @Override public NoticeVO createNotice(LoginUser user, NoticeDTO noticeDTO) { Course course = courseDAO.findCourseById(noticeDTO.getCourseId()); Notice notice = new Notice() .setCourseId(noticeDTO.getCourseId()) .setTeacherId(user.getId()) .setTitle(noticeDTO.getTitle()) .setContent(noticeDTO.getContent()); if (!existsAddAuth(user, notice)) { throw HelperException.of(ExceptionType.FORBIDDEN, "您无权创建该公告"); } Set studentIds = chooseDAO.findStudentIdsByCourseId(noticeDTO.getCourseId()); messageService.createMessage(studentIds, MessageType.NOTICE_NEW, String.format("[%s] 发布了新公告", course.getName()), course.getId()); return new NoticeVO(noticeDAO.save(notice)); } @Transactional(rollbackFor = Exception.class) @Override public NoticeVO modifyNotice(LoginUser user, Long noticeId, NoticeDTO noticeDTO) { Notice notice = noticeDAO.findNoticeById(noticeId); if (!existsUpdateAuth(user, notice)) { throw HelperException.of(ExceptionType.FORBIDDEN, "您无权修改该公告"); } return new NoticeVO( noticeDAO.save( notice.setTitle(noticeDTO.getTitle()) .setContent(noticeDTO.getContent()) ) ); } @Transactional(readOnly = true) @Override public Page getNoticesByCourse(LoginUser user, Long courseId, String key, Pageable pageable) { return noticeDAO.findByCourseIdAndKey(courseId, key, pageable).map(NoticeVO::new); } @Transactional(rollbackFor = Exception.class) @Override public void deleteNotice(LoginUser user, Long noticeId) { Notice notice = noticeDAO.findNoticeById(noticeId); if (!existsDelAuth(user, notice)) { throw HelperException.of(ExceptionType.FORBIDDEN, "您无权删除该公告"); } noticeDAO.delete(notice); messageService.deleteMessages(noticeId, MessageType.NOTICE_NEW); } @Transactional(readOnly = true) @Override public NoticeVO getOneNotice(LoginUser user, Long noticeId) { return new NoticeVO(noticeDAO.findNoticeById(noticeId)); } @Override public boolean existsAddAuth(LoginUser user, Notice notice) { return courseDAO.findCourseById(notice.getCourseId()).getTeacher().getId().equals(user.getId()); } @Override public boolean existsDelAuth(LoginUser user, Notice notice) { return notice.getTeacherId().equals(user.getId()); } @Override public boolean existsUpdateAuth(LoginUser user, Notice notice) { return notice.getTeacherId().equals(user.getId()); } @Override public boolean existsGetAuth(LoginUser user, Notice notice) { return true; } }