From f6b4c99e75fe073605581ad080760f6de7f0905a Mon Sep 17 00:00:00 2001 From: richarjiang Date: Fri, 22 Aug 2025 17:23:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=A7=BB=E9=99=A4=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E7=9B=AE=E6=A0=87=E5=92=8C=E4=BB=BB=E5=8A=A1=E8=AF=A6=E6=83=85?= =?UTF-8?q?=E7=9A=84API=E5=8F=8A=E7=9B=B8=E5=85=B3=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 删除GoalsController和GoalsService中获取单个目标和任务详情的API实现,简化代码结构。 - 更新GoalTaskService中的分页参数,将每页数量调整为200,提升数据处理能力。 - 优化GoalTaskQueryDto,移除页码和每页数量的验证装饰器,简化DTO结构。 --- src/goals/dto/goal-task.dto.ts | 6 ++-- src/goals/goals.controller.ts | 31 ------------------- src/goals/goals.service.ts | 40 ------------------------- src/goals/services/goal-task.service.ts | 36 +--------------------- 4 files changed, 3 insertions(+), 110 deletions(-) diff --git a/src/goals/dto/goal-task.dto.ts b/src/goals/dto/goal-task.dto.ts index 3a6bc47..abb8139 100644 --- a/src/goals/dto/goal-task.dto.ts +++ b/src/goals/dto/goal-task.dto.ts @@ -83,14 +83,12 @@ export class GoalTaskQueryDto { @IsOptional() @IsInt() - @Min(1, { message: '页码必须大于0' }) + page?: number = 1; @IsOptional() @IsInt() - @Min(1, { message: '每页数量必须大于0' }) - @Max(100, { message: '每页数量不能超过100' }) - pageSize?: number = 20; + pageSize?: number = 200; } export class CompleteGoalTaskDto { diff --git a/src/goals/goals.controller.ts b/src/goals/goals.controller.ts index 6d85b1e..a208b4c 100644 --- a/src/goals/goals.controller.ts +++ b/src/goals/goals.controller.ts @@ -64,21 +64,6 @@ export class GoalsController { }; } - /** - * 获取单个目标详情 - */ - @Get(':id') - async getGoal( - @Param('id') id: string, - @CurrentUser() user: AccessTokenPayload, - ): Promise> { - const goal = await this.goalsService.getGoal(user.sub, id); - return { - code: ResponseCode.SUCCESS, - message: '获取目标详情成功', - data: goal, - }; - } /** * 更新目标 @@ -227,22 +212,6 @@ export class GoalsController { }; } - /** - * 获取单个任务详情 - */ - @Get('tasks/:taskId') - async getTask( - @Param('taskId') taskId: string, - @CurrentUser() user: AccessTokenPayload, - ): Promise> { - const task = await this.goalTaskService.getTask(user.sub, taskId); - return { - code: ResponseCode.SUCCESS, - message: '获取任务详情成功', - data: task, - }; - } - /** * 完成任务 */ diff --git a/src/goals/goals.service.ts b/src/goals/goals.service.ts index e848fea..291d981 100644 --- a/src/goals/goals.service.ts +++ b/src/goals/goals.service.ts @@ -136,46 +136,6 @@ export class GoalsService { } } - /** - * 获取单个目标详情 - */ - async getGoal(userId: string, goalId: string): Promise { - try { - // 惰性生成任务 - await this.goalTaskService.generateTasksLazily(userId, goalId); - - const goal = await this.goalModel.findOne({ - where: { id: goalId, userId, deleted: false }, - include: [ - { - model: GoalCompletion, - as: 'completions', - where: { deleted: false }, - required: false, - order: [['completedAt', 'DESC']], - limit: 10, // 只显示最近10次完成记录 - }, - { - model: GoalTask, - as: 'tasks', - where: { deleted: false }, - required: false, - order: [['startDate', 'ASC']], - limit: 20, // 显示最近20个任务 - }, - ], - }); - - if (!goal) { - throw new NotFoundException('目标不存在'); - } - - return this.formatGoalResponse(goal); - } catch (error) { - this.logger.error(`获取目标详情失败: ${error.message}`); - throw error; - } - } /** * 更新目标 diff --git a/src/goals/services/goal-task.service.ts b/src/goals/services/goal-task.service.ts index a1c20fc..e14ecdf 100644 --- a/src/goals/services/goal-task.service.ts +++ b/src/goals/services/goal-task.service.ts @@ -314,7 +314,7 @@ export class GoalTaskService { // 先进行惰性生成 await this.generateTasksLazily(userId, query.goalId); - const { page = 1, pageSize = 20, goalId, status, startDate, endDate } = query; + const { page = 1, pageSize = 200, goalId, status, startDate, endDate } = query; const offset = (page - 1) * pageSize; const where: WhereOptions = { @@ -345,13 +345,6 @@ export class GoalTaskService { order: [['startDate', 'ASC'], ['createdAt', 'DESC']], offset, limit: pageSize, - include: [ - { - model: Goal, - as: 'goal', - attributes: ['id', 'title', 'repeatType', 'frequency', 'category'], - }, - ], }); return { @@ -366,33 +359,6 @@ export class GoalTaskService { } } - /** - * 获取单个任务详情 - */ - async getTask(userId: string, taskId: string): Promise { - try { - const task = await this.goalTaskModel.findOne({ - where: { id: taskId, userId, deleted: false }, - include: [ - { - model: Goal, - as: 'goal', - attributes: ['id', 'title', 'repeatType', 'frequency', 'category'], - }, - ], - }); - - if (!task) { - throw new NotFoundException('任务不存在'); - } - - return this.formatTaskResponse(task); - } catch (error) { - this.logger.error(`获取任务详情失败: ${error.message}`); - throw error; - } - } - /** * 完成任务 */