|
|
@@ -8,6 +8,7 @@ import nju.seec.helper.dto.LoginUser;
|
|
|
import nju.seec.helper.dto.modify.ModifyCourseDTO;
|
|
|
import nju.seec.helper.entity.Choose;
|
|
|
import nju.seec.helper.entity.Course;
|
|
|
+import nju.seec.helper.service.AuthUtil;
|
|
|
import nju.seec.helper.service.CourseService;
|
|
|
import nju.seec.helper.util.EncryptUtils;
|
|
|
import nju.seec.helper.util.exception.HelperException;
|
|
|
@@ -35,12 +36,11 @@ public class CourseServiceImpl implements CourseService {
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
@Override
|
|
|
public CourseVO createCourse(LoginUser teacher, CourseDTO courseDTO) {
|
|
|
- Integer teacherId = teacher.getId();
|
|
|
- if (courseDAO.existsByNameAndTeacherId(courseDTO.getName(), teacherId)) {
|
|
|
- throw HelperException.of(HelperException.ExceptionType.FORBIDDEN, "该课程名已使用");
|
|
|
- }
|
|
|
+ // 检查
|
|
|
+ checkSameCourseName(teacher.getId(), courseDTO.getName());
|
|
|
+
|
|
|
Course course = new Course()
|
|
|
- .setTeacherId(teacherId)
|
|
|
+ .setTeacherId(teacher.getId())
|
|
|
.setTeacherName(teacher.getName())
|
|
|
.setName(courseDTO.getName())
|
|
|
.setBio(courseDTO.getBio())
|
|
|
@@ -50,19 +50,29 @@ public class CourseServiceImpl implements CourseService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public CourseVO modifyCourse(LoginUser teacher, ModifyCourseDTO modifyCourseDTO) {
|
|
|
+ public CourseVO modifyCourse(LoginUser user, ModifyCourseDTO modifyCourseDTO) {
|
|
|
Course course = courseDAO.findCourseById(modifyCourseDTO.getId());
|
|
|
- if (!course.getTeacherId().equals(teacher.getId())) {
|
|
|
- throw HelperException.of(HelperException.ExceptionType.FORBIDDEN, "您无权修改该课程");
|
|
|
- }
|
|
|
+
|
|
|
+ // 检查
|
|
|
+ AuthUtil.checkDataAuth(user.getId(), course.getTeacherId(), "您无权修改该课程");
|
|
|
+ checkSameCourseName(user.getId(), modifyCourseDTO.getName());
|
|
|
+
|
|
|
course.setName(modifyCourseDTO.getName())
|
|
|
- .setCode(EncryptUtils.encode(modifyCourseDTO.getCode()))
|
|
|
.setBio(modifyCourseDTO.getBio())
|
|
|
- .setCover(modifyCourseDTO.getCover());
|
|
|
+ .setCode(EncryptUtils.encode(modifyCourseDTO.getCode()));
|
|
|
course = courseDAO.save(course);
|
|
|
return new CourseVO(course);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void removeCourse(LoginUser user, Integer courseId) {
|
|
|
+ Course course = courseDAO.findCourseById(courseId);
|
|
|
+ AuthUtil.checkDataAuth(user.getId(), course.getTeacherId(), "您无权删除该课程");
|
|
|
+
|
|
|
+ course.setDeleteAt(System.currentTimeMillis());
|
|
|
+ courseDAO.save(course);
|
|
|
+ }
|
|
|
+
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
@Override
|
|
|
public void chooseCourse(LoginUser student, ChooseDTO chooseDTO) {
|
|
|
@@ -85,9 +95,9 @@ public class CourseServiceImpl implements CourseService {
|
|
|
|
|
|
@Transactional(readOnly = true)
|
|
|
@Override
|
|
|
- public List<CourseVO> teacherGetCourses(LoginUser teacher, String key, Pageable pageable) {
|
|
|
+ public List<CourseVO> getCourses(String key, Pageable pageable) {
|
|
|
return courseDAO
|
|
|
- .findByTeacherIdAndNameContains(teacher.getId(), key, pageable)
|
|
|
+ .findByKey(key, pageable)
|
|
|
.stream()
|
|
|
.map(CourseVO::new)
|
|
|
.collect(Collectors.toList());
|
|
|
@@ -95,9 +105,9 @@ public class CourseServiceImpl implements CourseService {
|
|
|
|
|
|
@Transactional(readOnly = true)
|
|
|
@Override
|
|
|
- public List<CourseVO> getCourses(String key, Pageable pageable) {
|
|
|
+ public List<CourseVO> teacherGetCourses(LoginUser teacher, String key, Pageable pageable) {
|
|
|
return courseDAO
|
|
|
- .findByNameContains(key, pageable)
|
|
|
+ .findByTeacherIdAndKey(teacher.getId(), key, pageable)
|
|
|
.stream()
|
|
|
.map(CourseVO::new)
|
|
|
.collect(Collectors.toList());
|
|
|
@@ -107,7 +117,7 @@ public class CourseServiceImpl implements CourseService {
|
|
|
@Override
|
|
|
public List<CourseVO> studentGetCourses(LoginUser student, String key, Pageable pageable) {
|
|
|
return courseDAO
|
|
|
- .findByIdInAndNameContains(
|
|
|
+ .findByIdsAndKey(
|
|
|
chooseDAO.findCourseIdsByStudentId(student.getId()),
|
|
|
key,
|
|
|
pageable)
|
|
|
@@ -115,4 +125,13 @@ public class CourseServiceImpl implements CourseService {
|
|
|
.map(CourseVO::new)
|
|
|
.collect(Collectors.toList());
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查同名课程
|
|
|
+ */
|
|
|
+ private void checkSameCourseName(Integer teacherId, String name) {
|
|
|
+ if (courseDAO.existsByTeacherIdAndName(teacherId, name)) {
|
|
|
+ throw HelperException.of(HelperException.ExceptionType.CONFLICT, "该课程名已使用");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|