Skip to content

Commit

Permalink
feat: 단일 태스크 조회 API 구현
Browse files Browse the repository at this point in the history
* refactor: Task, Section 조회 및 예외처리 코드 함수 분리

* feat: 태스크 조회 API 구현

* fix: 오타 수정
  • Loading branch information
yangchef1 authored Nov 12, 2024
1 parent ea4f1e0 commit ae0b12e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
7 changes: 6 additions & 1 deletion apps/server/src/task/controller/task.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, Controller, Param, Patch, Post } from '@nestjs/common';
import { Body, Controller, Get, Param, Patch, Post } from '@nestjs/common';
import { TaskService } from '../service/task.service';
import { CreateTaskRequest } from '../dto/create-task-request.dto';
import { UpdateTaskRequest } from '../dto/update-task-request.dto';
Expand All @@ -22,4 +22,9 @@ export class TaskController {
move(@Param('id') id: number, @Body() moveTaskRequest: MoveTaskRequest) {
return this.taskService.move(id, moveTaskRequest);
}

@Get(':id')
get(@Param('id') id: number) {
return this.taskService.get(id);
}
}
21 changes: 21 additions & 0 deletions apps/server/src/task/dto/task-response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Task } from '../domain/task.entity';

export class TaskResponse {
constructor(task: Task) {
this.id = task.id;
this.title = task.title;
this.description = task.description;
this.sectionName = task.section.name;
this.position = task.position;
}

id: number;

title: string;

description: string;

sectionName: string;

position: string;
}
44 changes: 27 additions & 17 deletions apps/server/src/task/service/task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { UpdateTaskRequest } from '../dto/update-task-request.dto';
import { UpdateTaskResponse } from '../dto/update-task-response.dto';
import { MoveTaskRequest } from '../dto/move-task-request.dto';
import { MoveTaskResponse } from '../dto/move-task-response.dto';
import { TaskResponse } from '../dto/task-response.dto';

@Injectable()
export class TaskService {
Expand All @@ -32,41 +33,50 @@ export class TaskService {
}

async update(id: number, updateTaskRequest: UpdateTaskRequest) {
const task = await this.taskRepository.findOneBy({ id });
if (!task) {
throw new NotFoundException('Task not found');
}
const task = await this.findTaskOrThrow(id);

task.title = updateTaskRequest.title ?? task.title;
task.description = updateTaskRequest.description ?? task.description;

const section = await this.sectionRepository.findOneBy({ id: updateTaskRequest.sectionId });
if (!section) {
throw new NotFoundException('Section not found');
}
const section = await this.findSectionOrThrow(updateTaskRequest.sectionId);
task.section = section;

await this.taskRepository.save(task);
return new UpdateTaskResponse(task);
}

async move(id: number, moveTaskRequest: MoveTaskRequest) {
const task = await this.findTaskOrThrow(id);

const section = await this.findSectionOrThrow(moveTaskRequest.sectionId);
task.section = section;

const beforePosition = LexoRank.parse(moveTaskRequest.beforePosition);
const afterPosition = LexoRank.parse(moveTaskRequest.afterPosition);
task.position = beforePosition.between(afterPosition).toString();

await this.taskRepository.save(task);
return new MoveTaskResponse(task);
}

async get(id: number) {
const task = await this.findTaskOrThrow(id);
return new TaskResponse(task);
}

private async findTaskOrThrow(id: number) {
const task = await this.taskRepository.findOneBy({ id });
if (!task) {
throw new NotFoundException('Task not found');
}
return task;
}

const section = await this.sectionRepository.findOneBy({ id: moveTaskRequest.sectionId });
private async findSectionOrThrow(id: number) {
const section = await this.sectionRepository.findOneBy({ id });
if (!section) {
throw new NotFoundException('Section not found');
}
task.section = section;

const beforePostion = LexoRank.parse(moveTaskRequest.beforePosition);
const afterPosition = LexoRank.parse(moveTaskRequest.afterPosition);
task.position = beforePostion.between(afterPosition).toString();

await this.taskRepository.save(task);
return new MoveTaskResponse(task);
return section;
}
}

0 comments on commit ae0b12e

Please sign in to comment.