|
@@ -1,6 +1,6 @@
|
|
|
package cn.seecoder.seecoderportalserver.service;
|
|
package cn.seecoder.seecoderportalserver.service;
|
|
|
|
|
|
|
|
-import cn.seecoder.seecoderportalcommon.vo.CourseState;
|
|
|
|
|
|
|
+import cn.seecoder.seecoderportalcommon.vo.ObjectState;
|
|
|
import cn.seecoder.seecoderportalcommon.vo.CourseVO;
|
|
import cn.seecoder.seecoderportalcommon.vo.CourseVO;
|
|
|
import cn.seecoder.seecoderportalcommon.vo.UserRole;
|
|
import cn.seecoder.seecoderportalcommon.vo.UserRole;
|
|
|
import cn.seecoder.seecoderportalserver.domain.Course;
|
|
import cn.seecoder.seecoderportalserver.domain.Course;
|
|
@@ -50,11 +50,7 @@ public class CourseService {
|
|
|
|
|
|
|
|
public CourseVO getCourse(Long courseId) {
|
|
public CourseVO getCourse(Long courseId) {
|
|
|
Optional<Course> course = courseRepository.findById(courseId);
|
|
Optional<Course> course = courseRepository.findById(courseId);
|
|
|
- if (course.isPresent()) {
|
|
|
|
|
- return courseMapper.courseToCourseVO(course.get());
|
|
|
|
|
- } else {
|
|
|
|
|
- return null;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ return course.map(courseMapper::courseToCourseVO).orElse(null);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public CourseVO createCourse(CourseVO courseVO) {
|
|
public CourseVO createCourse(CourseVO courseVO) {
|
|
@@ -65,7 +61,7 @@ public class CourseService {
|
|
|
if (!teacher.isPresent()) {
|
|
if (!teacher.isPresent()) {
|
|
|
throw new PortalException(ExceptionType.ERROR, "该老师不存在");
|
|
throw new PortalException(ExceptionType.ERROR, "该老师不存在");
|
|
|
}
|
|
}
|
|
|
- if(!teacher.get().getRole().equals(UserRole.TEACHER) && !teacher.get().getRole().equals(UserRole.ADMIN)){
|
|
|
|
|
|
|
+ if (!teacher.get().getRole().equals(UserRole.TEACHER) && !teacher.get().getRole().equals(UserRole.ADMIN)) {
|
|
|
throw new PortalException(ExceptionType.ERROR, "该老师不存在");
|
|
throw new PortalException(ExceptionType.ERROR, "该老师不存在");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -79,7 +75,7 @@ public class CourseService {
|
|
|
throw new PortalException(ExceptionType.ERROR, "Coder: " + coderRes.get("msg"));
|
|
throw new PortalException(ExceptionType.ERROR, "Coder: " + coderRes.get("msg"));
|
|
|
}
|
|
}
|
|
|
Course course = courseMapper.courseVOToCourse(courseVO);
|
|
Course course = courseMapper.courseVOToCourse(courseVO);
|
|
|
- course.setCourseState(CourseState.NORMAL);
|
|
|
|
|
|
|
+ course.setState(ObjectState.NORMAL);
|
|
|
Course result = courseRepository.save(course);
|
|
Course result = courseRepository.save(course);
|
|
|
return courseMapper.courseToCourseVO(result);
|
|
return courseMapper.courseToCourseVO(result);
|
|
|
}
|
|
}
|
|
@@ -92,32 +88,33 @@ public class CourseService {
|
|
|
|
|
|
|
|
// 伪删除
|
|
// 伪删除
|
|
|
public String deleteCourse(Long courseId) {
|
|
public String deleteCourse(Long courseId) {
|
|
|
- Course course = courseRepository.findById(courseId).get();
|
|
|
|
|
- course.setCourseState(CourseState.DELETED);
|
|
|
|
|
|
|
+ Optional<Course> optionalCourse = courseRepository.findById(courseId);
|
|
|
|
|
+ if (!optionalCourse.isPresent()) {
|
|
|
|
|
+ throw new PortalException(ExceptionType.ERROR, "该课程不存在!");
|
|
|
|
|
+ }
|
|
|
|
|
+ Course course = optionalCourse.get();
|
|
|
|
|
+ course.setState(ObjectState.DELETED);
|
|
|
courseRepository.save(course);
|
|
courseRepository.save(course);
|
|
|
return "课程删除成功";
|
|
return "课程删除成功";
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public List<CourseVO> getAllCourses() {
|
|
public List<CourseVO> getAllCourses() {
|
|
|
List<Course> courses = courseRepository.findAll();
|
|
List<Course> courses = courseRepository.findAll();
|
|
|
- List<CourseVO> courseVOs = courses.stream()
|
|
|
|
|
- .filter(course -> course.getCourseState().equals(CourseState.NORMAL))
|
|
|
|
|
|
|
+ return courses.stream()
|
|
|
|
|
+ .filter(course -> course.getState().equals(ObjectState.NORMAL))
|
|
|
.map(courseMapper::courseToCourseVO)
|
|
.map(courseMapper::courseToCourseVO)
|
|
|
.collect(Collectors.toList());
|
|
.collect(Collectors.toList());
|
|
|
- return courseVOs;
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public List<CourseVO> getTeacherCourses(Long teacherId) {
|
|
public List<CourseVO> getTeacherCourses(Long teacherId) {
|
|
|
List<Course> courses = courseRepository.findCoursesByTeacherId(teacherId);
|
|
List<Course> courses = courseRepository.findCoursesByTeacherId(teacherId);
|
|
|
- List<CourseVO> courseVOs = courses.stream().map(courseMapper::courseToCourseVO).collect(Collectors.toList());
|
|
|
|
|
- return courseVOs;
|
|
|
|
|
|
|
+ return courses.stream().map(courseMapper::courseToCourseVO).collect(Collectors.toList());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public List<CourseVO> getStudentCourses(Long studentId) {
|
|
public List<CourseVO> getStudentCourses(Long studentId) {
|
|
|
List<CourseSelection> courseSelections = courseSelectionRepository.findCourseSelectionsByUserId(studentId);
|
|
List<CourseSelection> courseSelections = courseSelectionRepository.findCourseSelectionsByUserId(studentId);
|
|
|
- List<CourseVO> courseVOs = courseSelections.stream()
|
|
|
|
|
|
|
+ return courseSelections.stream()
|
|
|
.map(courseSelection -> courseMapper.courseToCourseVO(courseRepository.findById(courseSelection.getCourseId()).get()))
|
|
.map(courseSelection -> courseMapper.courseToCourseVO(courseRepository.findById(courseSelection.getCourseId()).get()))
|
|
|
.collect(Collectors.toList());
|
|
.collect(Collectors.toList());
|
|
|
- return courseVOs;
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|