b67cf238c2
- 迁移:study_expectations.session_num 改为 varchar 对齐会话编码
- StudyExpectationsEntity / Mapper / Service:每会话一条预期,重复创建覆盖
- StudySessionController:PUT/GET /{sessionNum}/expectation
- 移除空的 studySessionList 占位方法
103 lines
4.3 KiB
Java
103 lines
4.3 KiB
Java
package com.guo.learningprogresstracker.controller;
|
|
|
|
import com.google.protobuf.ServiceException;
|
|
import com.guo.learningprogresstracker.dto.request.EndedStudySessionRequest;
|
|
import com.guo.learningprogresstracker.dto.request.UpsertExpectationRequest;
|
|
import com.guo.learningprogresstracker.dto.response.StudySessionResponse;
|
|
import com.guo.learningprogresstracker.entity.CommonResult;
|
|
import com.guo.learningprogresstracker.entity.StudyExpectationsEntity;
|
|
import com.guo.learningprogresstracker.exception.ErrorParameterException;
|
|
import com.guo.learningprogresstracker.service.StudyExpectationsService;
|
|
import com.guo.learningprogresstracker.service.impl.StudySessionsServiceImpl;
|
|
import jakarta.validation.Valid;
|
|
import jakarta.validation.constraints.NotEmpty;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.validation.annotation.Validated;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.ArrayList;
|
|
import java.time.LocalDateTime;
|
|
|
|
/**
|
|
* 学习会话控制层
|
|
*/
|
|
@RequestMapping("/study-sessions")
|
|
@RestController
|
|
@RequiredArgsConstructor
|
|
@Validated
|
|
public class StudySessionController {
|
|
|
|
private final StudySessionsServiceImpl studySessionsServiceImpl;
|
|
|
|
private final StudyExpectationsService studyExpectationsService;
|
|
|
|
/**
|
|
* 创建/更新学习会话的学习预期
|
|
*/
|
|
@PutMapping("/{sessionNum}/expectation")
|
|
public CommonResult<StudyExpectationsEntity> upsertExpectation(
|
|
@PathVariable("sessionNum") String sessionNum,
|
|
@Valid @RequestBody UpsertExpectationRequest request) throws ErrorParameterException {
|
|
return CommonResult.success(studyExpectationsService.upsertExpectation(sessionNum, request.getDescription()));
|
|
}
|
|
|
|
/**
|
|
* 获取学习会话的学习预期
|
|
*/
|
|
@GetMapping("/{sessionNum}/expectation")
|
|
public CommonResult<StudyExpectationsEntity> getExpectation(
|
|
@PathVariable("sessionNum") String sessionNum) {
|
|
return CommonResult.success(studyExpectationsService.getBySessionNum(sessionNum));
|
|
}
|
|
|
|
/**
|
|
* 通过sessionNum获取一个【学习会话】
|
|
*/
|
|
@GetMapping("/{sessionNum}")
|
|
public CommonResult<StudySessionResponse> getStudySessionBySessionNum(@NotEmpty(message = "sessionNum不可为空") @PathVariable String sessionNum) throws ErrorParameterException {
|
|
// 通过 service 层获取,包含归属校验
|
|
StudySessionResponse response = studySessionsServiceImpl.getStudySessionBySessionNum(sessionNum);
|
|
return CommonResult.success(response);
|
|
}
|
|
|
|
/**
|
|
* 开始一个【学习会话】
|
|
*/
|
|
@PostMapping("/{sessionNum}/study-sessions/continue")
|
|
public CommonResult<Void> startStudySession(@PathVariable("sessionNum") String sessionNum) throws ErrorParameterException, ServiceException {
|
|
studySessionsServiceImpl.continueStudySession(sessionNum);
|
|
return CommonResult.success();
|
|
}
|
|
|
|
/**
|
|
* 暂停一个【学习会话】
|
|
*/
|
|
@PostMapping("/{sessionNum}/study-sessions/pause")
|
|
public CommonResult<Void> pauseStudySession(@PathVariable("sessionNum") String sessionNum,
|
|
@RequestParam(value = "endTime", required = false) LocalDateTime endTime)
|
|
throws ErrorParameterException, ServiceException {
|
|
studySessionsServiceImpl.pauseStudySession(sessionNum, endTime);
|
|
return CommonResult.success();
|
|
}
|
|
|
|
/**
|
|
* 结束一个【学习会话】
|
|
*/
|
|
@PostMapping("/{sessionNum}/study-sessions/ended")
|
|
public CommonResult<Void> endedStudySession(@PathVariable("sessionNum") String sessionNum,
|
|
@Valid @RequestBody EndedStudySessionRequest request) throws ErrorParameterException {
|
|
String message = studySessionsServiceImpl.endedStudySession(sessionNum, request.getContent());
|
|
return message != null ? CommonResult.success(message) : CommonResult.success();
|
|
}
|
|
|
|
/**
|
|
* 获取【学习会话】所有的学习残片数据,以列表返回
|
|
*/
|
|
@GetMapping("/{sessionNum}/all-fragments")
|
|
public CommonResult<ArrayList<String>> getAllFragments(@PathVariable("sessionNum") String sessionNum) throws ErrorParameterException {
|
|
ArrayList<String> allFragments = studySessionsServiceImpl.getAllFragments(sessionNum);
|
|
return CommonResult.success(allFragments);
|
|
|
|
}
|
|
}
|