[xingyu.guo] feat: 【结束一个【学习会话】】API

This commit is contained in:
guo
2024-09-22 15:52:08 +08:00
parent 54af814b5e
commit cecfa7a800
6 changed files with 71 additions and 8 deletions
@@ -1,18 +1,21 @@
package com.guo.learningprogresstracker.controller; package com.guo.learningprogresstracker.controller;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.guo.learningprogresstracker.dto.request.EndedStudySessionRequest;
import com.guo.learningprogresstracker.dto.response.StudySessionResponce; import com.guo.learningprogresstracker.dto.response.StudySessionResponce;
import com.guo.learningprogresstracker.entity.CommonResult; import com.guo.learningprogresstracker.entity.CommonResult;
import com.guo.learningprogresstracker.entity.StudySessionsEntity; import com.guo.learningprogresstracker.entity.StudySessionsEntity;
import com.guo.learningprogresstracker.exception.ErrorParameterException; import com.guo.learningprogresstracker.exception.ErrorParameterException;
import com.guo.learningprogresstracker.mapStruct.StudySessionConvert; import com.guo.learningprogresstracker.mapStruct.StudySessionConvert;
import com.guo.learningprogresstracker.service.impl.StudySessionsServiceImpl; import com.guo.learningprogresstracker.service.impl.StudySessionsServiceImpl;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@@ -49,12 +52,21 @@ public class StudySessionController {
/** /**
* 暂停一个【学习会话】 * 暂停一个【学习会话】
*/ */
@PostMapping("/{sessionNum}/studay-sessions/pause") @PostMapping("/{sessionNum}/study-sessions/pause")
public CommonResult pauseStudySession(@PathVariable("sessionNum") String sessionNum) throws ErrorParameterException { public CommonResult pauseStudySession(@PathVariable("sessionNum") String sessionNum) throws ErrorParameterException {
studySessionsServiceImpl.pauseStudySession(sessionNum); studySessionsServiceImpl.pauseStudySession(sessionNum);
return CommonResult.success(); return CommonResult.success();
} }
/**
* 结束一个【学习会话】
*/
@PostMapping("/{sessionNum}/study-sessions/ended")
public CommonResult endedStudySession(@PathVariable("sessionNum") String sessionNum,
@Valid @RequestBody EndedStudySessionRequest request) throws ErrorParameterException {
studySessionsServiceImpl.endedStudySession(sessionNum,request.getContent());
return CommonResult.success();
}
/** /**
* 获取【学习会话】所有的学习残片数据,以列表返回 * 获取【学习会话】所有的学习残片数据,以列表返回
*/ */
@@ -0,0 +1,13 @@
package com.guo.learningprogresstracker.dto.request;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
/**
* @author guo
*/
@Data
public class EndedStudySessionRequest {
@NotBlank(message = "啊?搞无字天书是吧?报告内容不可为空")
private String content;
}
@@ -5,7 +5,7 @@ import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable; import java.io.Serializable;
import java.time.LocalDateTime;
import lombok.Data; import lombok.Data;
/** /**
@@ -18,14 +18,14 @@ public class StudyReportsEntity extends BaseEntity implements Serializable {
/** /**
* *
*/ */
@TableId(value = "report_id", type = IdType.AUTO) @TableId(value = "id", type = IdType.AUTO)
private Integer reportId; private Integer id;
/** /**
* * 会话编码
*/ */
@TableField(value = "session_id") @TableField(value = "session_num")
private Integer sessionId; private String sessionNum;
/** /**
* 完整的学习报告内容 * 完整的学习报告内容
@@ -117,4 +117,18 @@ public class StudySessionsEntity extends BaseEntity implements Serializable {
this.setSessionState(StudySessionStateEnum.PAUSED.name()); this.setSessionState(StudySessionStateEnum.PAUSED.name());
} }
} }
/**
* 结束会话
*/
public void endedStudySession() {
if (this.getSessionState().equals(StudySessionStateEnum.ENDED.name())){
log.warn("不应出现的情况:结束了一个状态为【{}】的学习会话",this.getSessionState());
}else {
if (this.getSessionState().equals(StudySessionStateEnum.ONGOING.name())) {
this.pausedStudySession();
}
this.setSessionState(StudySessionStateEnum.ENDED.name());
}
}
} }
@@ -17,4 +17,8 @@ public interface StudySessionsService extends IService<StudySessionsEntity> {
StudySessionResponce getNotEndedStudySessionByTaskNum(String taskNum) throws ErrorParameterException; StudySessionResponce getNotEndedStudySessionByTaskNum(String taskNum) throws ErrorParameterException;
ArrayList<String> getAllFragments(String sessionNum) throws ErrorParameterException; ArrayList<String> getAllFragments(String sessionNum) throws ErrorParameterException;
void endedStudySession(String sessionNum, String content) throws ErrorParameterException;
void pauseStudySession(String sessionNum) throws ErrorParameterException;
} }
@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guo.learningprogresstracker.dto.response.StudySessionResponce; import com.guo.learningprogresstracker.dto.response.StudySessionResponce;
import com.guo.learningprogresstracker.entity.StudyReportFragmentsEntity; import com.guo.learningprogresstracker.entity.StudyReportFragmentsEntity;
import com.guo.learningprogresstracker.entity.StudyReportsEntity;
import com.guo.learningprogresstracker.entity.StudySessionsEntity; import com.guo.learningprogresstracker.entity.StudySessionsEntity;
import com.guo.learningprogresstracker.entity.TaskEntity; import com.guo.learningprogresstracker.entity.TaskEntity;
import com.guo.learningprogresstracker.enums.StudySessionStateEnum; import com.guo.learningprogresstracker.enums.StudySessionStateEnum;
@@ -11,6 +12,7 @@ import com.guo.learningprogresstracker.exception.ErrorParameterException;
import com.guo.learningprogresstracker.exception.NotFindEntitiesException; import com.guo.learningprogresstracker.exception.NotFindEntitiesException;
import com.guo.learningprogresstracker.mapStruct.StudySessionConvert; import com.guo.learningprogresstracker.mapStruct.StudySessionConvert;
import com.guo.learningprogresstracker.mapper.StudyReportFragmentsMapper; import com.guo.learningprogresstracker.mapper.StudyReportFragmentsMapper;
import com.guo.learningprogresstracker.mapper.StudyReportsMapper;
import com.guo.learningprogresstracker.mapper.StudySessionsMapper; import com.guo.learningprogresstracker.mapper.StudySessionsMapper;
import com.guo.learningprogresstracker.service.StudySessionsService; import com.guo.learningprogresstracker.service.StudySessionsService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@@ -29,11 +31,12 @@ import java.util.stream.Collectors;
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, StudySessionsEntity> public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, StudySessionsEntity>
implements StudySessionsService{ implements StudySessionsService {
private final TasksServiceImpl tasksServiceImpl; private final TasksServiceImpl tasksServiceImpl;
private final StudyReportFragmentsServiceImpl studyReportFragmentsServiceImpl; private final StudyReportFragmentsServiceImpl studyReportFragmentsServiceImpl;
private final StudyReportFragmentsMapper studyReportFragmentsMapper; private final StudyReportFragmentsMapper studyReportFragmentsMapper;
private final StudyReportsMapper studyReportsMapper;
public StudySessionResponce startOrContinueStudySession(String taskNum) throws NotFindEntitiesException { public StudySessionResponce startOrContinueStudySession(String taskNum) throws NotFindEntitiesException {
// taskNum是否存在 // taskNum是否存在
@@ -60,6 +63,7 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
} }
} }
@Override
public void pauseStudySession(String sessionNum) throws ErrorParameterException { public void pauseStudySession(String sessionNum) throws ErrorParameterException {
StudySessionsEntity studySessionsEntity = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class) StudySessionsEntity studySessionsEntity = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
.eq(StudySessionsEntity::getSessionNum, sessionNum)) .eq(StudySessionsEntity::getSessionNum, sessionNum))
@@ -68,6 +72,21 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
this.updateById(studySessionsEntity); this.updateById(studySessionsEntity);
} }
@Override
public void endedStudySession(String sessionNum, String content) throws ErrorParameterException {
StudySessionsEntity studySessionsEntity = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
.eq(StudySessionsEntity::getSessionNum, sessionNum))
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionNum + "]不存在"));
studySessionsEntity.endedStudySession();
this.updateById(studySessionsEntity);
// 创建学习报告
StudyReportsEntity studyReportsEntity = new StudyReportsEntity();
studyReportsEntity.setSessionNum(sessionNum);
studyReportsEntity.setContent(content);
studyReportsMapper.insert(studyReportsEntity);
}
@Override @Override
public StudySessionResponce getNotEndedStudySessionByTaskNum(String taskNum) throws ErrorParameterException { public StudySessionResponce getNotEndedStudySessionByTaskNum(String taskNum) throws ErrorParameterException {
StudySessionsEntity studySessionsEntity = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class) StudySessionsEntity studySessionsEntity = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
@@ -89,6 +108,7 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
throw new ErrorParameterException("会话[" + sessionNum + "]不存在"); throw new ErrorParameterException("会话[" + sessionNum + "]不存在");
} }
} }
} }