|
|
@@ -14,10 +14,13 @@ import cn.seecoder.seecoderportalserver.service.UserService;
|
|
|
import cn.seecoder.seecoderportalserver.serviceImpl.subsystem.CoderService;
|
|
|
import cn.seecoder.seecoderportalserver.serviceImpl.subsystem.EvalService;
|
|
|
import cn.seecoder.seecoderportalserver.serviceImpl.subsystem.HelperService;
|
|
|
+import cn.seecoder.seecoderportalserver.utility.OssUtil;
|
|
|
import cn.seecoder.seecoderportalserver.web.rest.errors.CustomErrorException;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
import java.util.List;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@@ -30,8 +33,9 @@ public class CourseServiceImpl implements CourseService {
|
|
|
private final CoderService coderService;
|
|
|
private final HelperService helperService;
|
|
|
private final EvalService evalService;
|
|
|
+ private final OssUtil ossUtil;
|
|
|
|
|
|
- public CourseServiceImpl(CourseRepository courseRepository, CourseMapper courseMapper, CourseSelectionRepository courseSelectionRepository, CoderService coderService, HelperService helperService, EvalService evalService, UserService userService) {
|
|
|
+ public CourseServiceImpl(CourseRepository courseRepository, CourseMapper courseMapper, CourseSelectionRepository courseSelectionRepository, CoderService coderService, HelperService helperService, EvalService evalService, UserService userService, OssUtil ossUtil) {
|
|
|
this.courseRepository = courseRepository;
|
|
|
this.courseMapper = courseMapper;
|
|
|
this.courseSelectionRepository = courseSelectionRepository;
|
|
|
@@ -39,8 +43,10 @@ public class CourseServiceImpl implements CourseService {
|
|
|
this.coderService = coderService;
|
|
|
this.helperService = helperService;
|
|
|
this.evalService = evalService;
|
|
|
+ this.ossUtil = ossUtil;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public CourseVO createCourse(CourseVO courseVO, Long userId) {
|
|
|
// 确认授课教师
|
|
|
UserVO teacher = userService.findUserById(userId);
|
|
|
@@ -48,9 +54,9 @@ public class CourseServiceImpl implements CourseService {
|
|
|
throw new CustomErrorException("当前用户无权创建课程");
|
|
|
}
|
|
|
|
|
|
+ // 子系统:eval、coder
|
|
|
Integer coderId;
|
|
|
Integer evalId;
|
|
|
-
|
|
|
// 创建Coder课程
|
|
|
if (courseVO.getCoderId() != null) {
|
|
|
JSONObject coderRes = coderService.createCourse(courseVO);
|
|
|
@@ -73,12 +79,14 @@ public class CourseServiceImpl implements CourseService {
|
|
|
courseVO.setEvalId(evalId);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
Course course = courseMapper.courseVOToCourse(courseVO);
|
|
|
course.setState(ObjectState.NORMAL);
|
|
|
Course result = courseRepository.save(course);
|
|
|
return courseMapper.courseToCourseVO(result);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public CourseVO updateCourse(CourseVO courseVO, Long userId) {
|
|
|
CourseVO course = this.findCourseById(courseVO.getId());
|
|
|
if (!userId.equals(course.getTeacher().getId())) {
|
|
|
@@ -97,6 +105,7 @@ public class CourseServiceImpl implements CourseService {
|
|
|
return courseMapper.courseToCourseVO(result);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public String deleteCourse(Long courseId, Long userId) {
|
|
|
CourseVO course = this.findCourseById(courseId);
|
|
|
if (!userId.equals(course.getTeacher().getId())) {
|
|
|
@@ -109,6 +118,7 @@ public class CourseServiceImpl implements CourseService {
|
|
|
return "课程删除成功";
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public List<CourseVO> findAllCourses() {
|
|
|
List<Course> courses = courseRepository.findAll();
|
|
|
return courses.stream()
|
|
|
@@ -117,6 +127,7 @@ public class CourseServiceImpl implements CourseService {
|
|
|
.collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public List<CourseVO> findTeacherCourses(Long teacherId) {
|
|
|
List<Course> courses = courseRepository.findCoursesByTeacherId(teacherId);
|
|
|
return courses.stream()
|
|
|
@@ -125,6 +136,7 @@ public class CourseServiceImpl implements CourseService {
|
|
|
.collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public List<CourseVO> findStudentCourses(Long studentId) {
|
|
|
List<CourseSelection> courseSelections = courseSelectionRepository.findCourseSelectionsByUserId(studentId);
|
|
|
return courseSelections.stream()
|
|
|
@@ -133,6 +145,7 @@ public class CourseServiceImpl implements CourseService {
|
|
|
.collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public List<CourseVO> findRecommendCourses() {
|
|
|
List<Course> courses = courseRepository.findCoursesByState(ObjectState.EXAMPLE);
|
|
|
return courses.stream()
|
|
|
@@ -140,24 +153,29 @@ public class CourseServiceImpl implements CourseService {
|
|
|
.collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public CourseVO findCourseByCoderId(Integer coderId) {
|
|
|
Course course = courseRepository.findCourseByCoderId(coderId).orElseThrow(CustomErrorException::courseNotFound);
|
|
|
return course.getState() == ObjectState.DELETED ? null : courseMapper.courseToCourseVO(course);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public Boolean checkCourseNameUnique(Long id, String name) {
|
|
|
Course course = courseRepository.findCourseByName(name).orElse(null);
|
|
|
return course == null || course.getId().equals(id);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public CourseVO findCourseById(Long id) {
|
|
|
Course course = courseRepository.findById(id)
|
|
|
.orElseThrow(CustomErrorException::courseNotFound);
|
|
|
- if (course.getState().equals(ObjectState.DELETED))
|
|
|
+ if (course.getState() == ObjectState.DELETED) {
|
|
|
throw CustomErrorException.courseNotFound();
|
|
|
+ }
|
|
|
return courseMapper.courseToCourseVO(course);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public String confirmCourseSelection(Long courseId, Long userId) {
|
|
|
UserVO userVO = userService.findUserById(userId);
|
|
|
assert userService.isStudent(userVO.getRole());
|
|
|
@@ -166,9 +184,6 @@ public class CourseServiceImpl implements CourseService {
|
|
|
// coder系统选课
|
|
|
if (course.getCoderId() != null) {
|
|
|
JSONObject coderRes = coderService.chooseCourse(course.getInvitationCode());
|
|
|
-// if (!coderRes.containsKey("res")) {
|
|
|
-// throw new CustomErrorException("Coder: " + coderRes.get("msg"));
|
|
|
-// }
|
|
|
}
|
|
|
// eval系统选课
|
|
|
if (course.getEvalId() != null) {
|
|
|
@@ -184,6 +199,7 @@ public class CourseServiceImpl implements CourseService {
|
|
|
return "选课成功";
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public String cancelCourseSelection(CourseSelectionKey courseSelectionKey, Long userId) {
|
|
|
UserVO user = userService.findUserById(userId);
|
|
|
if (!user.getId().equals(courseSelectionKey.getUserId())) {
|
|
|
@@ -199,24 +215,37 @@ public class CourseServiceImpl implements CourseService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public Boolean checkSelection(Long userId, Long courseId) {
|
|
|
CourseSelectionKey key = new CourseSelectionKey(userId, courseId);
|
|
|
CourseSelection record = courseSelectionRepository.findById(key).orElse(null);
|
|
|
return record != null && record.getState();
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public Boolean checkCourseCodeUnique(Long id, String code) {
|
|
|
Course course = courseRepository.findCourseByInvitationCode(code).orElse(null);
|
|
|
return course == null || course.getId().equals(id);
|
|
|
}
|
|
|
|
|
|
- public CourseVO findCourseByEvalId(int evalId){
|
|
|
+ @Override
|
|
|
+ public CourseVO findCourseByEvalId(int evalId) {
|
|
|
Course course = courseRepository.findCourseByEvalId(evalId).orElseThrow(CustomErrorException::courseNotFound);
|
|
|
return course.getState() == ObjectState.DELETED ? null : courseMapper.courseToCourseVO(course);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public Boolean isCourseNameExisted(String name) {
|
|
|
Course course = courseRepository.findCourseByName(name).orElse(null);
|
|
|
return course != null;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String uploadCourseImage(String name, MultipartFile file) {
|
|
|
+ try {
|
|
|
+ return ossUtil.upload(name + ".jpg", file.getInputStream());
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|