refactor: 优化其他 Service 实现和 Controller 代码
This commit is contained in:
@@ -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