| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- 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.UserDAO;
- import nju.seec.helper.dto.course.ChooseDTO;
- import nju.seec.helper.dto.course.CourseDTO;
- import nju.seec.helper.entity.Choose;
- import nju.seec.helper.entity.Course;
- import nju.seec.helper.enums.ExceptionType;
- import nju.seec.helper.enums.UserType;
- import nju.seec.helper.exception.HelperException;
- import nju.seec.helper.service.CourseService;
- import nju.seec.helper.util.EncryptUtils;
- import nju.seec.helper.vo.CourseVO;
- import nju.seec.helper.vo.UserVO;
- import org.springframework.dao.DataIntegrityViolationException;
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.Pageable;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- /**
- * @author cst
- */
- @Service
- public class CourseServiceImpl implements CourseService {
- private final UserDAO userDAO;
- private final CourseDAO courseDAO;
- private final ChooseDAO chooseDAO;
- public CourseServiceImpl(UserDAO userDAO, CourseDAO courseDAO, ChooseDAO chooseDAO) {
- this.userDAO = userDAO;
- this.courseDAO = courseDAO;
- this.chooseDAO = chooseDAO;
- }
- @Transactional(rollbackFor = Exception.class)
- @Override
- public CourseVO createCourse(LoginUser teacher, CourseDTO courseDTO) {
- try {
- return new CourseVO(
- courseDAO.save(
- new Course()
- .setTeacher(userDAO.findUserById(teacher.getId()))
- .setName(courseDTO.getName())
- .setBio(courseDTO.getBio())
- .setCode(EncryptUtils.encode(courseDTO.getCode()))
- )
- );
- } catch (DataIntegrityViolationException e) {
- throw HelperException.of(ExceptionType.CONFLICT, "创建失败,请检查课程信息是否正确且不与已有课程冲突");
- }
- }
- @Transactional(rollbackFor = Exception.class)
- @Override
- public CourseVO modifyCourse(LoginUser user, Long courseId, CourseDTO courseDTO) {
- Course course = courseDAO.findCourseById(courseId);
- if (!existsUpdateAuth(user, course)) {
- throw HelperException.of(ExceptionType.FORBIDDEN, "您无权修改该课程");
- }
- try {
- return new CourseVO(
- courseDAO.save(
- course.setName(courseDTO.getName())
- .setBio(courseDTO.getBio())
- .setCode(EncryptUtils.encode(courseDTO.getCode()))
- )
- );
- } catch (DataIntegrityViolationException e) {
- throw HelperException.of(ExceptionType.CONFLICT, "修改失败,请检查课程信息是否正确且不与已有课程冲突");
- }
- }
- @Transactional(rollbackFor = Exception.class)
- @Override
- public void deleteCourse(LoginUser user, Long courseId) {
- Course course = courseDAO.findCourseById(courseId);
- if (!existsUpdateAuth(user, course)) {
- throw HelperException.of(ExceptionType.FORBIDDEN, "您无权删除该课程");
- }
- course.setDeleteAt(System.currentTimeMillis());
- courseDAO.save(course);
- }
- @Transactional(rollbackFor = Exception.class)
- @Override
- public void chooseCourse(LoginUser student, Long courseId, ChooseDTO chooseDTO) {
- Long studentId = student.getId();
- if (chooseDAO.existsByStudentIdAndCourseId(studentId, courseId)) {
- throw HelperException.of(ExceptionType.CONFLICT, "您已选课");
- }
- if (!courseDAO.existsByIdAndCode(courseId, EncryptUtils.encode(chooseDTO.getCode()))) {
- throw HelperException.of(ExceptionType.NOT_FOUND, "选课码错误");
- }
- chooseDAO.save(
- new Choose()
- .setCourseId(courseId)
- .setStudentId(studentId)
- );
- }
- @Transactional(rollbackFor = Exception.class)
- @Override
- public void quitCourse(LoginUser user, Long courseId) {
- chooseDAO.deleteByStudentIdAndCourseId(user.getId(), courseId);
- }
- @Transactional(readOnly = true)
- @Override
- public Page<CourseVO> getCourses(String key, Pageable pageable) {
- return courseDAO.findByKey(key, pageable).map(CourseVO::new);
- }
- @Transactional(readOnly = true)
- @Override
- public Page<CourseVO> getCreatedCourses(LoginUser user, String key, Pageable pageable) {
- return courseDAO.findByTeacherAndNameContains(userDAO.findUserById(user.getId()), key, pageable).map(CourseVO::new);
- }
- @Transactional(readOnly = true)
- @Override
- public Page<CourseVO> getChosenCourses(LoginUser user, String key, Pageable pageable) {
- return courseDAO.findByIdsAndKey(chooseDAO.findCourseIdsByStudentId(user.getId()), key, pageable).map(CourseVO::new);
- }
- @Transactional(readOnly = true)
- @Override
- public String getCourseCode(LoginUser user, Long courseId) {
- Course course = courseDAO.findCourseById(courseId);
- if (!existsGetAuth(user, course)) {
- throw HelperException.of(ExceptionType.FORBIDDEN, "您无权获取该课程选课码");
- }
- return EncryptUtils.decode(course.getCode());
- }
- @Transactional(readOnly = true)
- @Override
- public CourseVO getOneCourse(Long courseId) {
- return new CourseVO(courseDAO.findCourseById(courseId));
- }
- @Transactional(readOnly = true)
- @Override
- public Page<UserVO> getChooseStudents(LoginUser user, Long courseId, String key, Pageable pageable) {
- Course course = courseDAO.findCourseById(courseId);
- if (!existsGetAuth(user, course)) {
- throw HelperException.of(ExceptionType.FORBIDDEN, "您无权获取该课程的选课名单");
- }
- return userDAO.findByIdIn(chooseDAO.findStudentIdsByCourseId(courseId), pageable).map(UserVO::new);
- }
- @Override
- public boolean existsAddAuth(LoginUser user, Course course) {
- return user.getType() == UserType.TEACHER;
- }
- @Override
- public boolean existsDelAuth(LoginUser user, Course course) {
- return course.getTeacher().getId().equals(user.getId());
- }
- @Override
- public boolean existsUpdateAuth(LoginUser user, Course course) {
- return course.getTeacher().getId().equals(user.getId());
- }
- @Override
- public boolean existsGetAuth(LoginUser user, Course course) {
- return course.getTeacher().getId().equals(user.getId())
- || chooseDAO.existsByStudentIdAndCourseId(user.getId(), course.getId());
- }
- }
|