| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- 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<Long> 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<NoticeVO> 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);
- }
- @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;
- }
- }
|