Quellcode durchsuchen

fix:修复门户更新课程信息失效问题

BaiQi vor 1 Jahr
Ursprung
Commit
3e9d4c4eb5

+ 1 - 0
src/main/java/com/njuzr/eaibackend/config/JWTAuthenticationFilter.java

@@ -79,6 +79,7 @@ public class JWTAuthenticationFilter extends OncePerRequestFilter {
                         reg.setPassword("no_password");
                         userService.createUser(reg);
                     } else {
+                        log.info("用户存在,更新用户{}", phone);
                         // 否则更新用户信息
                         UserUpdateDTO update = new UserUpdateDTO();
                         update.setContentEmail(email);

+ 21 - 0
src/main/java/com/njuzr/eaibackend/controller/CourseController.java

@@ -11,6 +11,8 @@ import com.njuzr.eaibackend.po.MyUserDetails;
 import com.njuzr.eaibackend.po.User;
 import com.njuzr.eaibackend.service.CourseService;
 import com.njuzr.eaibackend.vo.CourseVO;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.security.core.annotation.AuthenticationPrincipal;
@@ -26,6 +28,7 @@ import org.springframework.web.bind.annotation.*;
 @RestController
 @RequestMapping("/api/course")
 public class CourseController {
+    private static final Logger log = LoggerFactory.getLogger(CourseController.class);
     private final CourseService courseService;
 
     @Autowired
@@ -176,6 +179,24 @@ public class CourseController {
         return MyResponse.success(courseService.updateCourse(userId, courseId, updateDTO));
     }
 
+    /**
+     * 只有课程老师或管理员可以修改当前课程的信息
+     * @param userPid
+     * @param courseId
+     * @param updateDTO
+     * @return
+     */
+    @PreAuthorize("hasRole('TEACHER') or hasRole('ADMIN')")
+    @PutMapping("/portal")
+    public MyResponse updateCourseByPortal(
+            @AuthenticationPrincipal(expression = "pid") Long userPid,
+            @AuthenticationPrincipal(expression = "id") Long userId,
+            @RequestParam Long courseId,
+            @RequestBody CourseUpdateDTO updateDTO
+    ) {
+        return MyResponse.success(courseService.updateCourseByPortal(userId, userPid, courseId, updateDTO));
+    }
+
 
     @PreAuthorize("hasRole('TEACHER') or hasRole('ADMIN')")
     @PutMapping("/enrollCode")

+ 2 - 0
src/main/java/com/njuzr/eaibackend/dto/course/CourseUpdateDTO.java

@@ -32,4 +32,6 @@ public class CourseUpdateDTO {
     private String description;
 
     private String enrollCode; // 选课码
+
+    private String teacherPid;
 }

+ 2 - 0
src/main/java/com/njuzr/eaibackend/service/CourseService.java

@@ -27,6 +27,8 @@ public interface CourseService {
 
     CourseVO updateCourse(Long userId, Long courseId, CourseUpdateDTO courseUpdateDTO);
 
+    CourseVO updateCourseByPortal(Long userId, Long userPid, Long courseId, CourseUpdateDTO courseUpdateDTO);
+
     void deleteById(MyUserDetails role, Long courseId);
 
     void enroll(EnrollDTO enrollDTO);

+ 52 - 1
src/main/java/com/njuzr/eaibackend/service/impl/CourseServiceImpl.java

@@ -177,6 +177,56 @@ public class CourseServiceImpl implements CourseService {
         return convertToVO(courseMapper.selectById(courseId));
     }
 
+    /**
+     * 门户更新课程信息
+     * @param userPid
+     * @param courseId
+     * @param courseUpdateDTO
+     * @return
+     */
+    @Override
+    public CourseVO updateCourseByPortal(Long userId, Long userPid, Long courseId, CourseUpdateDTO courseUpdateDTO) {
+        User targetUser;
+        if (userPid == null){
+            targetUser = userMapper.selectById(userId);
+        } else {
+            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
+            queryWrapper.eq("pid", userPid);
+            targetUser = userMapper.selectOne(queryWrapper);
+        }
+
+        Course targetCourse = courseMapper.selectById(courseId);
+
+        if (targetCourse == null)
+            throw MyException.create(HttpStatus.BAD_REQUEST, "课程不存在");
+
+        if (targetUser.getRole() != Role.ADMIN) {
+            List<String> teacherIds = Collections.singletonList(Optional.ofNullable(targetCourse.getTeacherIds())
+                    .orElse(Collections.emptyList().toString()));
+            String userIdStr = String.valueOf(targetUser.getId());
+            boolean hasTeacherId = teacherIds.contains(userIdStr);
+            boolean pidMatches = Objects.equals(targetCourse.getTeacherPid(), targetUser.getPid());
+            if (!hasTeacherId && !pidMatches) {
+                throw MyException.create(HttpStatus.BAD_REQUEST, "无权限更新");
+            }
+        }
+
+        Course opeCourse = convertToPO(courseUpdateDTO);
+        opeCourse.setCourseId(courseId);
+
+        try {
+            int code = courseMapper.updateById(opeCourse);
+            if (code == 0) {
+                throw MyException.create(HttpStatus.INTERNAL_SERVER_ERROR, "更新课程失败");
+            }
+        } catch (Exception e) {
+            log.error(e.getMessage());
+            throw MyException.create(HttpStatus.INTERNAL_SERVER_ERROR, "更新课程失败");
+        }
+
+        return convertToVO(courseMapper.selectById(courseId));
+    }
+
 
     /**
      * 删除课程
@@ -335,7 +385,8 @@ public class CourseServiceImpl implements CourseService {
     private CourseVO convertToVO(Course course) {
         CourseVO res = new CourseVO();
         BeanUtils.copyProperties(course, res);
-        res.setCourseImages(ConvertUtil.stringToList(course.getCourseImages(), String::valueOf));
+        if(course.getCourseImages() != null)
+            res.setCourseImages(ConvertUtil.stringToList(course.getCourseImages(), String::valueOf));
         log.info(res.toString());
         // 组装教师名字
         List<String> teacherNames = new ArrayList<>();