1. 开始或继续一个【学习会话】API 2. 暂停一个【学习会话】API 3.通过sessionId获取一个【学习会话】API
This commit is contained in:
+34
-1
@@ -1,11 +1,18 @@
|
||||
package com.guo.learningprogresstracker.service.impl;
|
||||
|
||||
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.service.StudySessionsService;
|
||||
import com.guo.learningprogresstracker.enums.StudySessionStateEnum;
|
||||
import com.guo.learningprogresstracker.exception.ErrorParameterException;
|
||||
import com.guo.learningprogresstracker.mapStruct.StudySessionConvert;
|
||||
import com.guo.learningprogresstracker.mapper.StudySessionsMapper;
|
||||
import com.guo.learningprogresstracker.service.StudySessionsService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* @author guo
|
||||
* @description 针对表【study_sessions(记录每次学习会话的具体数据)】的数据库操作Service实现
|
||||
@@ -15,6 +22,32 @@ import org.springframework.stereotype.Service;
|
||||
public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, StudySessionsEntity>
|
||||
implements StudySessionsService{
|
||||
|
||||
public StudySessionResponce startOrContinueStudySession(String taskId) {
|
||||
// 任务id下是否存在非”已结束“的学习会话
|
||||
Optional<StudySessionsEntity> oneOpt = this.getOneOpt(Wrappers.<StudySessionsEntity>lambdaQuery()
|
||||
.eq(StudySessionsEntity::getTaskId, taskId)
|
||||
.ne(StudySessionsEntity::getSessionState, StudySessionStateEnum.ENDED.name()));
|
||||
// 有则返回该未完成会话,否则创建新的会话
|
||||
if (oneOpt.isEmpty()) {
|
||||
// 返回一个新的会话
|
||||
StudySessionsEntity studySessionsEntity = StudySessionsEntity.initNewSession();
|
||||
this.save(studySessionsEntity);
|
||||
return StudySessionConvert.MAPPER.toStudySessionResponce(studySessionsEntity);
|
||||
}else {
|
||||
// 返回已经存在的会话
|
||||
StudySessionsEntity studySessionsEntity = oneOpt.get();
|
||||
studySessionsEntity.continueStudySession();
|
||||
this.updateById(studySessionsEntity);
|
||||
return StudySessionConvert.MAPPER.toStudySessionResponce(studySessionsEntity);
|
||||
}
|
||||
}
|
||||
|
||||
public void pauseStudySession(String sessionId) throws ErrorParameterException {
|
||||
StudySessionsEntity studySessionsEntity = this.getOptById(sessionId)
|
||||
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionId + "]不存在"));
|
||||
studySessionsEntity.pausedStudySession();
|
||||
this.updateById(studySessionsEntity);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user