diff --git a/src/main/java/com/guo/learningprogresstracker/controller/StudySessionController.java b/src/main/java/com/guo/learningprogresstracker/controller/StudySessionController.java index 0d73ac0..dcaf722 100644 --- a/src/main/java/com/guo/learningprogresstracker/controller/StudySessionController.java +++ b/src/main/java/com/guo/learningprogresstracker/controller/StudySessionController.java @@ -1,13 +1,10 @@ package com.guo.learningprogresstracker.controller; -import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.google.protobuf.ServiceException; import com.guo.learningprogresstracker.dto.request.EndedStudySessionRequest; import com.guo.learningprogresstracker.dto.response.StudySessionResponse; import com.guo.learningprogresstracker.entity.CommonResult; -import com.guo.learningprogresstracker.entity.StudySessionsEntity; import com.guo.learningprogresstracker.exception.ErrorParameterException; -import com.guo.learningprogresstracker.mapStruct.StudySessionConvert; import com.guo.learningprogresstracker.service.impl.StudySessionsServiceImpl; import jakarta.validation.Valid; import jakarta.validation.constraints.NotEmpty; @@ -15,8 +12,8 @@ import lombok.RequiredArgsConstructor; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; -import java.time.LocalDateTime; import java.util.ArrayList; +import java.time.LocalDateTime; /** * 学习会话控制层 @@ -39,11 +36,9 @@ public class StudySessionController { */ @GetMapping("/{sessionNum}") public CommonResult getStudySessionBySessionNum(@NotEmpty(message = "sessionNum不可为空") @PathVariable String sessionNum) throws ErrorParameterException { - StudySessionsEntity studySessionsEntity = studySessionsServiceImpl.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class) - .eq(StudySessionsEntity::getSessionNum, sessionNum)) - .orElseThrow(() -> new ErrorParameterException("会话[" + sessionNum + "]不存在")); - StudySessionResponse studySessionResponse = StudySessionConvert.MAPPER.toStudySessionResponse(studySessionsEntity); - return CommonResult.success(studySessionResponse); + // 通过 service 层获取,包含归属校验 + StudySessionResponse response = studySessionsServiceImpl.getStudySessionBySessionNum(sessionNum); + return CommonResult.success(response); } /** diff --git a/src/main/java/com/guo/learningprogresstracker/service/impl/StudyReportFragmentsServiceImpl.java b/src/main/java/com/guo/learningprogresstracker/service/impl/StudyReportFragmentsServiceImpl.java index 04fc023..1dcaae8 100644 --- a/src/main/java/com/guo/learningprogresstracker/service/impl/StudyReportFragmentsServiceImpl.java +++ b/src/main/java/com/guo/learningprogresstracker/service/impl/StudyReportFragmentsServiceImpl.java @@ -21,22 +21,21 @@ import org.springframework.stereotype.Service; @Service @RequiredArgsConstructor public class StudyReportFragmentsServiceImpl extends ServiceImpl - implements StudyReportFragmentsService{ + implements StudyReportFragmentsService { private final StudySessionsMapper studySessionsMapper; @Override public void createFragments(CreateFragmentsRequest request) throws NotFindEntitiesException { StudyReportFragmentsEntity entity = FragmentsConvert.MAPPER.toFragmentsEntity(request); - if (studySessionsMapper.exists(Wrappers.lambdaQuery(StudySessionsEntity.class) - .eq(StudySessionsEntity::getSessionNum,entity.getSessionNum()))) { - this.save(entity); - }else { - throw new NotFindEntitiesException(String.format("未能找到学习会话sessionNum[%s]",entity.getSessionNum())); + // 拦截器自动注入 created_by 条件,会话归属校验已内置 + StudySessionsEntity session = studySessionsMapper.selectOne( + Wrappers.lambdaQuery(StudySessionsEntity.class) + .eq(StudySessionsEntity::getSessionNum, entity.getSessionNum())); + if (session == null) { + throw new NotFindEntitiesException(String.format("未能找到学习会话sessionNum[%s]", entity.getSessionNum())); } + // created_by 由 MetaObjectHandler 在 insert 时自动填充 + this.save(entity); } } - - - - diff --git a/src/main/java/com/guo/learningprogresstracker/service/impl/StudySessionsServiceImpl.java b/src/main/java/com/guo/learningprogresstracker/service/impl/StudySessionsServiceImpl.java index 912b3b7..0788ff5 100644 --- a/src/main/java/com/guo/learningprogresstracker/service/impl/StudySessionsServiceImpl.java +++ b/src/main/java/com/guo/learningprogresstracker/service/impl/StudySessionsServiceImpl.java @@ -38,12 +38,14 @@ public class StudySessionsServiceImpl extends ServiceImpl new NotFindEntitiesException(String.format("[%s]不存在", taskNum))); @@ -80,6 +82,7 @@ public class StudySessionsServiceImpl extends ServiceImpl new ErrorParameterException("会话[" + sessionNum + "]不存在")); @@ -95,12 +98,13 @@ public class StudySessionsServiceImpl extends ServiceImpl new ErrorParameterException("会话[" + sessionNum + "]不存在")); studySessionsEntity.endedStudySession(); this.updateById(studySessionsEntity); - // 创建学习报告 + // 创建学习报告(created_by 由 MetaObjectHandler 自动填充) StudyReportsEntity studyReportsEntity = new StudyReportsEntity(); studyReportsEntity.setSessionNum(sessionNum); studyReportsEntity.setContent(content); @@ -109,6 +113,10 @@ public class StudySessionsServiceImpl extends ServiceImpl new ErrorParameterException("任务[" + taskNum + "]不存在")); StudySessionsDto dto = Optional.ofNullable(studyReportsMapper.getNotEndedStudySessionDtoByTaskNum(taskNum)) .orElseThrow(() -> new ErrorParameterException("任务[" + taskNum + "]不存在进行中或暂停中的会话")); return StudySessionConvert.MAPPER.toStudySessionResponse(dto); @@ -116,9 +124,9 @@ public class StudySessionsServiceImpl extends ServiceImpl getAllFragments(String sessionNum) throws ErrorParameterException { + // 拦截器自动校验归属 if (this.exists(Wrappers.lambdaQuery(StudySessionsEntity.class) .eq(StudySessionsEntity::getSessionNum, sessionNum))) { - // todo guo 后续可在此补充更高级的整理方式 return studyReportFragmentsMapper.selectList(Wrappers.lambdaQuery(StudyReportFragmentsEntity.class) .eq(StudyReportFragmentsEntity::getSessionNum, sessionNum)) .stream().map(StudyReportFragmentsEntity::getContent).collect(Collectors.toCollection(ArrayList::new)); @@ -129,6 +137,7 @@ public class StudySessionsServiceImpl extends ServiceImpl new ErrorParameterException("会话[" + sessionNum + "]不存在")); @@ -142,8 +151,15 @@ public class StudySessionsServiceImpl extends ServiceImpl new ErrorParameterException("会话[" + sessionNum + "]不存在")); + return StudySessionConvert.MAPPER.toStudySessionResponse(session); + } + } - - - - diff --git a/src/main/java/com/guo/learningprogresstracker/service/impl/TasksServiceImpl.java b/src/main/java/com/guo/learningprogresstracker/service/impl/TasksServiceImpl.java index c44b4a1..547d112 100644 --- a/src/main/java/com/guo/learningprogresstracker/service/impl/TasksServiceImpl.java +++ b/src/main/java/com/guo/learningprogresstracker/service/impl/TasksServiceImpl.java @@ -19,8 +19,6 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; -import java.rmi.ServerException; - /** * @author guo * @description 针对表【tasks(存储学习任务的基本信息,包括优先级的多维度计算)】的数据库操作Service实现 @@ -30,24 +28,25 @@ import java.rmi.ServerException; @RequiredArgsConstructor @Slf4j public class TasksServiceImpl extends ServiceImpl - implements TasksService{ - + implements TasksService { private final TasksMapper tasksMapper; @Override public Page taskList(Integer pageNum, Integer pageSize) { + // 拦截器自动注入 created_by 条件,只返回当前用户的任务 Page page = tasksMapper.selectPage(new Page(pageNum, pageSize), - Wrappers.lambdaQuery(TaskEntity.class).orderByDesc(TaskEntity::getCalculatedPriority)); + Wrappers.lambdaQuery(TaskEntity.class) + .orderByDesc(TaskEntity::getCalculatedPriority)); Page response = TaskConvert.MAPPER.toTaskInfoPage(page); - // todo 需要补充TaskInfo中的lastLearningStatus字段信息 return response; } @Override public String addTask(TaskRequest taskRequest) throws ErrorParameterException { - TaskEntity task= TaskConvert.MAPPER.taskRequestToTaskEntity(taskRequest); + TaskEntity task = TaskConvert.MAPPER.taskRequestToTaskEntity(taskRequest); + // 拦截器自动注入 created_by,重复校验仅限当前用户 if (this.exists(Wrappers.lambdaQuery(TaskEntity.class) .eq(TaskEntity::getTaskName, task.getTaskName()))) { throw new ErrorParameterException(String.format("任务名[%s]重复", task.getTaskName())); @@ -62,14 +61,18 @@ public class TasksServiceImpl extends ServiceImpl @Override public TaskInfoResponse getTask(String taskId) { + // 拦截器自动校验归属,查到的一定是当前用户的任务 TaskEntity taskEntity = this.getById(taskId); - TaskInfoResponse taskInfoResponse = TaskConvert.MAPPER.taskEntityToTaskInfoResponse(taskEntity); - - return taskInfoResponse; + return TaskConvert.MAPPER.taskEntityToTaskInfoResponse(taskEntity); } @Override public void updateTask(String taskId, TaskRequest updatedTask) { + // getById 已被拦截器保护,其他用户的任务查不到 + TaskEntity existingTask = this.getById(taskId); + if (existingTask == null) { + throw new IllegalArgumentException("任务不存在: " + taskId); + } TaskEntity taskEntity = TaskConvert.MAPPER.taskRequestToTaskEntity(updatedTask); int id; try { @@ -78,21 +81,16 @@ public class TasksServiceImpl extends ServiceImpl throw new IllegalArgumentException("任务ID格式错误: " + taskId); } taskEntity.setId(id); - boolean updated = this.updateById(taskEntity); - if (!updated) { - throw new IllegalArgumentException("任务不存在或更新失败: " + taskId); - } - + this.updateById(taskEntity); } @Override - public void deleteTask(String taskId) throws ServerException { - boolean b = this.removeById(taskId); - if(!b){ - throw new ServerException("未能正常删除任务"); + public void deleteTask(String taskId) { + // getById 已被拦截器保护,其他用户的任务查不到 + TaskEntity existingTask = this.getById(taskId); + if (existingTask == null) { + throw new IllegalArgumentException("任务不存在: " + taskId); } + this.removeById(taskId); } } - - -