CourseServiceImpl.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.UserDAO;
  6. import nju.seec.helper.dto.course.ChooseDTO;
  7. import nju.seec.helper.dto.course.CourseDTO;
  8. import nju.seec.helper.entity.Choose;
  9. import nju.seec.helper.entity.Course;
  10. import nju.seec.helper.enums.ExceptionType;
  11. import nju.seec.helper.enums.UserType;
  12. import nju.seec.helper.exception.HelperException;
  13. import nju.seec.helper.service.CourseService;
  14. import nju.seec.helper.util.EncryptUtils;
  15. import nju.seec.helper.vo.CourseVO;
  16. import nju.seec.helper.vo.UserVO;
  17. import org.springframework.dao.DataIntegrityViolationException;
  18. import org.springframework.data.domain.Page;
  19. import org.springframework.data.domain.Pageable;
  20. import org.springframework.stereotype.Service;
  21. import org.springframework.transaction.annotation.Transactional;
  22. /**
  23. * @author cst
  24. */
  25. @Service
  26. public class CourseServiceImpl implements CourseService {
  27. private final UserDAO userDAO;
  28. private final CourseDAO courseDAO;
  29. private final ChooseDAO chooseDAO;
  30. public CourseServiceImpl(UserDAO userDAO, CourseDAO courseDAO, ChooseDAO chooseDAO) {
  31. this.userDAO = userDAO;
  32. this.courseDAO = courseDAO;
  33. this.chooseDAO = chooseDAO;
  34. }
  35. @Transactional(rollbackFor = Exception.class)
  36. @Override
  37. public CourseVO createCourse(LoginUser teacher, CourseDTO courseDTO) {
  38. try {
  39. return new CourseVO(
  40. courseDAO.save(
  41. new Course()
  42. .setTeacher(userDAO.findUserById(teacher.getId()))
  43. .setName(courseDTO.getName())
  44. .setBio(courseDTO.getBio())
  45. .setCode(EncryptUtils.encode(courseDTO.getCode()))
  46. )
  47. );
  48. } catch (DataIntegrityViolationException e) {
  49. throw HelperException.of(ExceptionType.CONFLICT, "创建失败,请检查课程信息是否正确且不与已有课程冲突");
  50. }
  51. }
  52. @Transactional(rollbackFor = Exception.class)
  53. @Override
  54. public CourseVO modifyCourse(LoginUser user, Long courseId, CourseDTO courseDTO) {
  55. Course course = courseDAO.findCourseById(courseId);
  56. if (!existsUpdateAuth(user, course)) {
  57. throw HelperException.of(ExceptionType.FORBIDDEN, "您无权修改该课程");
  58. }
  59. try {
  60. return new CourseVO(
  61. courseDAO.save(
  62. course.setName(courseDTO.getName())
  63. .setBio(courseDTO.getBio())
  64. .setCode(EncryptUtils.encode(courseDTO.getCode()))
  65. )
  66. );
  67. } catch (DataIntegrityViolationException e) {
  68. throw HelperException.of(ExceptionType.CONFLICT, "修改失败,请检查课程信息是否正确且不与已有课程冲突");
  69. }
  70. }
  71. @Transactional(rollbackFor = Exception.class)
  72. @Override
  73. public void deleteCourse(LoginUser user, Long courseId) {
  74. Course course = courseDAO.findCourseById(courseId);
  75. if (!existsUpdateAuth(user, course)) {
  76. throw HelperException.of(ExceptionType.FORBIDDEN, "您无权删除该课程");
  77. }
  78. course.setDeleteAt(System.currentTimeMillis());
  79. courseDAO.save(course);
  80. }
  81. @Transactional(rollbackFor = Exception.class)
  82. @Override
  83. public void chooseCourse(LoginUser student, Long courseId, ChooseDTO chooseDTO) {
  84. Long studentId = student.getId();
  85. if (chooseDAO.existsByStudentIdAndCourseId(studentId, courseId)) {
  86. throw HelperException.of(ExceptionType.CONFLICT, "您已选课");
  87. }
  88. if (!courseDAO.existsByIdAndCode(courseId, EncryptUtils.encode(chooseDTO.getCode()))) {
  89. throw HelperException.of(ExceptionType.NOT_FOUND, "选课码错误");
  90. }
  91. chooseDAO.save(
  92. new Choose()
  93. .setCourseId(courseId)
  94. .setStudentId(studentId)
  95. );
  96. }
  97. @Transactional(rollbackFor = Exception.class)
  98. @Override
  99. public void quitCourse(LoginUser user, Long courseId) {
  100. chooseDAO.deleteByStudentIdAndCourseId(user.getId(), courseId);
  101. }
  102. @Transactional(readOnly = true)
  103. @Override
  104. public Page<CourseVO> getCourses(String key, Pageable pageable) {
  105. return courseDAO.findByKey(key, pageable).map(CourseVO::new);
  106. }
  107. @Transactional(readOnly = true)
  108. @Override
  109. public Page<CourseVO> getCreatedCourses(LoginUser user, String key, Pageable pageable) {
  110. return courseDAO.findByTeacherAndNameContains(userDAO.findUserById(user.getId()), key, pageable).map(CourseVO::new);
  111. }
  112. @Transactional(readOnly = true)
  113. @Override
  114. public Page<CourseVO> getChosenCourses(LoginUser user, String key, Pageable pageable) {
  115. return courseDAO.findByIdsAndKey(chooseDAO.findCourseIdsByStudentId(user.getId()), key, pageable).map(CourseVO::new);
  116. }
  117. @Transactional(readOnly = true)
  118. @Override
  119. public String getCourseCode(LoginUser user, Long courseId) {
  120. Course course = courseDAO.findCourseById(courseId);
  121. if (!existsGetAuth(user, course)) {
  122. throw HelperException.of(ExceptionType.FORBIDDEN, "您无权获取该课程选课码");
  123. }
  124. return EncryptUtils.decode(course.getCode());
  125. }
  126. @Transactional(readOnly = true)
  127. @Override
  128. public CourseVO getOneCourse(Long courseId) {
  129. return new CourseVO(courseDAO.findCourseById(courseId));
  130. }
  131. @Transactional(readOnly = true)
  132. @Override
  133. public Page<UserVO> getChooseStudents(LoginUser user, Long courseId, String key, Pageable pageable) {
  134. Course course = courseDAO.findCourseById(courseId);
  135. if (!existsGetAuth(user, course)) {
  136. throw HelperException.of(ExceptionType.FORBIDDEN, "您无权获取该课程的选课名单");
  137. }
  138. return userDAO.findByIdIn(chooseDAO.findStudentIdsByCourseId(courseId), pageable).map(UserVO::new);
  139. }
  140. @Override
  141. public boolean existsAddAuth(LoginUser user, Course course) {
  142. return user.getType() == UserType.TEACHER;
  143. }
  144. @Override
  145. public boolean existsDelAuth(LoginUser user, Course course) {
  146. return course.getTeacher().getId().equals(user.getId());
  147. }
  148. @Override
  149. public boolean existsUpdateAuth(LoginUser user, Course course) {
  150. return course.getTeacher().getId().equals(user.getId());
  151. }
  152. @Override
  153. public boolean existsGetAuth(LoginUser user, Course course) {
  154. return course.getTeacher().getId().equals(user.getId())
  155. || chooseDAO.existsByStudentIdAndCourseId(user.getId(), course.getId());
  156. }
  157. }