diff --git a/src/main/java/com/guo/learningprogresstracker/controller/TaskController.java b/src/main/java/com/guo/learningprogresstracker/controller/TaskController.java index 7132fae..05ee47c 100644 --- a/src/main/java/com/guo/learningprogresstracker/controller/TaskController.java +++ b/src/main/java/com/guo/learningprogresstracker/controller/TaskController.java @@ -5,6 +5,7 @@ import com.guo.learningprogresstracker.common.Ops; import com.guo.learningprogresstracker.dto.response.StudySessionResponce; import com.guo.learningprogresstracker.entity.CommonResult; import com.guo.learningprogresstracker.dto.request.TaskRequest; +import com.guo.learningprogresstracker.exception.NotFindEntitiesException; import com.guo.learningprogresstracker.service.TasksService; import com.guo.learningprogresstracker.service.impl.StudySessionsServiceImpl; import io.swagger.v3.oas.annotations.Operation; @@ -71,10 +72,10 @@ public class TaskController { * 开始或继续一个【学习会话】 * */ - @GetMapping("/{taskId}/studay-sessions/start-or-continue") + @GetMapping("/{taskNum}/studay-sessions/start-or-continue") public CommonResult startOrContinueStudySession(@NotEmpty(message = "taskId不可为空") - @PathVariable String taskId) { - StudySessionResponce responce = studySessionsServiceImpl.startOrContinueStudySession(taskId); + @PathVariable String taskNum) throws NotFindEntitiesException { + StudySessionResponce responce = studySessionsServiceImpl.startOrContinueStudySession(taskNum); return CommonResult.success(responce); } diff --git a/src/main/java/com/guo/learningprogresstracker/entity/StudySessionsEntity.java b/src/main/java/com/guo/learningprogresstracker/entity/StudySessionsEntity.java index b27afc2..c95fb8d 100644 --- a/src/main/java/com/guo/learningprogresstracker/entity/StudySessionsEntity.java +++ b/src/main/java/com/guo/learningprogresstracker/entity/StudySessionsEntity.java @@ -21,14 +21,17 @@ import java.time.LocalDateTime; @Data public class StudySessionsEntity extends BaseEntity implements Serializable { - @TableId(value = "session_id", type = IdType.AUTO) - private Integer sessionId; + @TableId(type = IdType.AUTO) + private Integer id; + + @TableField(value = "session_num") + private String sessionNum; /** * */ - @TableField(value = "task_id") - private Integer taskId; + @TableField(value = "task_num") + private String taskNum; /** * 学习开始时间 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 9c66de2..f8706df 100644 --- a/src/main/java/com/guo/learningprogresstracker/service/impl/StudySessionsServiceImpl.java +++ b/src/main/java/com/guo/learningprogresstracker/service/impl/StudySessionsServiceImpl.java @@ -4,11 +4,14 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.guo.learningprogresstracker.dto.response.StudySessionResponce; import com.guo.learningprogresstracker.entity.StudySessionsEntity; +import com.guo.learningprogresstracker.entity.TaskEntity; import com.guo.learningprogresstracker.enums.StudySessionStateEnum; import com.guo.learningprogresstracker.exception.ErrorParameterException; +import com.guo.learningprogresstracker.exception.NotFindEntitiesException; import com.guo.learningprogresstracker.mapStruct.StudySessionConvert; import com.guo.learningprogresstracker.mapper.StudySessionsMapper; import com.guo.learningprogresstracker.service.StudySessionsService; +import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.util.Optional; @@ -19,18 +22,25 @@ import java.util.Optional; * @createDate 2024-06-09 15:28:48 */ @Service +@RequiredArgsConstructor public class StudySessionsServiceImpl extends ServiceImpl implements StudySessionsService{ - public StudySessionResponce startOrContinueStudySession(String taskId) { - // 任务id下是否存在非”已结束“的学习会话 + private final TasksServiceImpl tasksServiceImpl; + + public StudySessionResponce startOrContinueStudySession(String taskNum) throws NotFindEntitiesException { + // taskNum是否存在 + tasksServiceImpl.getOneOpt(Wrappers.lambdaQuery(TaskEntity.class).eq(TaskEntity::getTaskNum, taskNum)) + .orElseThrow(() -> new NotFindEntitiesException(String.format("[%s]不存在",taskNum))); + // 任务taskNum下是否存在非"已结束"的学习会话 Optional oneOpt = this.getOneOpt(Wrappers.lambdaQuery() - .eq(StudySessionsEntity::getTaskId, taskId) + .eq(StudySessionsEntity::getTaskNum, taskNum) .ne(StudySessionsEntity::getSessionState, StudySessionStateEnum.ENDED.name())); // 有则返回该未完成会话,否则创建新的会话 if (oneOpt.isEmpty()) { // 返回一个新的会话 StudySessionsEntity studySessionsEntity = StudySessionsEntity.initNewSession(); + studySessionsEntity.setTaskNum(taskNum); this.save(studySessionsEntity); return StudySessionConvert.MAPPER.toStudySessionResponce(studySessionsEntity); }else {