refactor: 优化其他 Service 实现和 Controller 代码
This commit is contained in:
+4
-9
@@ -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<StudySessionResponse> 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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+9
-10
@@ -21,22 +21,21 @@ import org.springframework.stereotype.Service;
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class StudyReportFragmentsServiceImpl extends ServiceImpl<StudyReportFragmentsMapper, StudyReportFragmentsEntity>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
+22
-6
@@ -38,12 +38,14 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
|
||||
implements StudySessionsService {
|
||||
|
||||
public static final int WORK_DURATION = 25;
|
||||
|
||||
private final TasksServiceImpl tasksServiceImpl;
|
||||
private final StudyReportFragmentsServiceImpl studyReportFragmentsServiceImpl;
|
||||
private final StudyReportFragmentsMapper studyReportFragmentsMapper;
|
||||
private final StudyReportsMapper studyReportsMapper;
|
||||
|
||||
public StudySessionResponse startOrContinueStudySession(String taskNum) throws NotFindEntitiesException, ServiceException {
|
||||
// 拦截器已自动注入 created_by 条件,只有当前用户的 task 才能查到
|
||||
TaskEntity taskEntity = tasksServiceImpl.getOneOpt(
|
||||
Wrappers.lambdaQuery(TaskEntity.class).eq(TaskEntity::getTaskNum, taskNum))
|
||||
.orElseThrow(() -> new NotFindEntitiesException(String.format("[%s]不存在", taskNum)));
|
||||
@@ -80,6 +82,7 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
|
||||
|
||||
@Override
|
||||
public void pauseStudySession(String sessionNum, LocalDateTime endTime) throws ErrorParameterException, ServiceException {
|
||||
// 拦截器自动校验归属,查不到即说明无权访问
|
||||
StudySessionsEntity studySessionsEntity = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
|
||||
.eq(StudySessionsEntity::getSessionNum, sessionNum))
|
||||
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionNum + "]不存在"));
|
||||
@@ -95,12 +98,13 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
|
||||
|
||||
@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);
|
||||
// 创建学习报告
|
||||
// 创建学习报告(created_by 由 MetaObjectHandler 自动填充)
|
||||
StudyReportsEntity studyReportsEntity = new StudyReportsEntity();
|
||||
studyReportsEntity.setSessionNum(sessionNum);
|
||||
studyReportsEntity.setContent(content);
|
||||
@@ -109,6 +113,10 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
|
||||
|
||||
@Override
|
||||
public StudySessionResponse getNotEndedStudySessionByTaskNum(String taskNum) throws ErrorParameterException {
|
||||
// 拦截器自动注入 created_by,task 归属校验已内置
|
||||
TaskEntity task = tasksServiceImpl.getOneOpt(Wrappers.lambdaQuery(TaskEntity.class)
|
||||
.eq(TaskEntity::getTaskNum, taskNum))
|
||||
.orElseThrow(() -> 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<StudySessionsMapper, S
|
||||
|
||||
@Override
|
||||
public ArrayList<String> 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<StudySessionsMapper, S
|
||||
|
||||
@Override
|
||||
public void continueStudySession(String sessionNum) throws ErrorParameterException, ServiceException {
|
||||
// 拦截器自动校验归属
|
||||
StudySessionsEntity studySessionsEntity = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
|
||||
.eq(StudySessionsEntity::getSessionNum, sessionNum))
|
||||
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionNum + "]不存在"));
|
||||
@@ -142,8 +151,15 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
|
||||
studySessionsEntity.continueStudySession();
|
||||
this.updateById(studySessionsEntity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过 sessionNum 获取学习会话(归属由拦截器自动校验)
|
||||
*/
|
||||
public StudySessionResponse getStudySessionBySessionNum(String sessionNum) throws ErrorParameterException {
|
||||
StudySessionsEntity session = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
|
||||
.eq(StudySessionsEntity::getSessionNum, sessionNum))
|
||||
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionNum + "]不存在"));
|
||||
return StudySessionConvert.MAPPER.toStudySessionResponse(session);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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<TasksMapper, TaskEntity>
|
||||
implements TasksService{
|
||||
|
||||
implements TasksService {
|
||||
|
||||
private final TasksMapper tasksMapper;
|
||||
|
||||
@Override
|
||||
public Page<TaskInfo> taskList(Integer pageNum, Integer pageSize) {
|
||||
// 拦截器自动注入 created_by 条件,只返回当前用户的任务
|
||||
Page<TaskEntity> page = tasksMapper.selectPage(new Page<TaskEntity>(pageNum, pageSize),
|
||||
Wrappers.lambdaQuery(TaskEntity.class).orderByDesc(TaskEntity::getCalculatedPriority));
|
||||
Wrappers.lambdaQuery(TaskEntity.class)
|
||||
.orderByDesc(TaskEntity::getCalculatedPriority));
|
||||
Page<TaskInfo> 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<TasksMapper, TaskEntity>
|
||||
|
||||
@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<TasksMapper, TaskEntity>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user