Skip to content

Commit

Permalink
feat: 태스크 위치 이동 API 구현
Browse files Browse the repository at this point in the history
* refactor: LexoRank 알고리즘에 맞춰 생성 API 수정

* feat: 태스크 이동 API 구현

* feat: 태스크의 섹션 간 이동 기능 추가 구현

* fix: 중복 import 코드 삭제
  • Loading branch information
yangchef1 authored Nov 11, 2024
1 parent 8fa6560 commit ea4f1e0
Show file tree
Hide file tree
Showing 10 changed files with 1,742 additions and 4,798 deletions.
1 change: 1 addition & 0 deletions apps/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"dotenv": "^16.4.5",
"lexorank": "^1.0.5",
"mysql2": "^3.11.4",
"passport": "^0.7.0",
"passport-jwt": "^4.0.1",
Expand Down
8 changes: 7 additions & 1 deletion apps/server/src/task/controller/task.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Body, Controller, 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';
import { MoveTaskRequest } from '../dto/move-task-request.dto';

@Controller('task')
export class TaskController {
Expand All @@ -12,8 +13,13 @@ export class TaskController {
return this.taskService.create(createTaskRequest);
}

@Patch(':id')
@Patch(':id/status')
update(@Param('id') id: number, @Body() updateTaskRequest: UpdateTaskRequest) {
return this.taskService.update(id, updateTaskRequest);
}

@Patch(':id/position')
move(@Param('id') id: number, @Body() moveTaskRequest: MoveTaskRequest) {
return this.taskService.move(id, moveTaskRequest);
}
}
2 changes: 1 addition & 1 deletion apps/server/src/task/domain/task.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class Task {
description: string;

@Column()
position: number;
position: string;

@ManyToOne(() => Section)
@JoinColumn({ name: 'section_id' })
Expand Down
4 changes: 1 addition & 3 deletions apps/server/src/task/dto/create-task-request.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export class CreateTaskRequest {
position: number;

sectionId: number;
lastTaskPosition: string;
}
3 changes: 3 additions & 0 deletions apps/server/src/task/dto/create-task-response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { Task } from '../domain/task.entity';
export class CreateTaskResponse {
constructor(task: Task) {
this.id = task.id;
this.position = task.position;
}

id: number;

position: string;
}
7 changes: 7 additions & 0 deletions apps/server/src/task/dto/move-task-request.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class MoveTaskRequest {
sectionId: number;

beforePosition: string;

afterPosition: string;
}
21 changes: 21 additions & 0 deletions apps/server/src/task/dto/move-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 MoveTaskResponse {
constructor(task: Task) {
this.id = task.id;
this.title = task.title;
this.description = task.description;
this.sectionId = task.section.id;
this.position = task.position;
}

id: number;

title: string;

description: string;

sectionId: number;

position: string;
}
2 changes: 1 addition & 1 deletion apps/server/src/task/dto/update-task-response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ export class UpdateTaskResponse {

sectionId: number;

position: number;
position: string;
}
33 changes: 27 additions & 6 deletions apps/server/src/task/service/task.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { LexoRank } from 'lexorank';
import { Task } from '../domain/task.entity';
import { CreateTaskRequest } from '../dto/create-task-request.dto';
import { CreateTaskResponse } from '../dto/create-task-response.dto';
import { Section } from '../domain/section.entity';
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';

@Injectable()
export class TaskService {
Expand All @@ -18,14 +21,12 @@ export class TaskService {
) {}

async create(createTaskRequest: CreateTaskRequest) {
const section = await this.sectionRepository.findOneBy({ id: createTaskRequest.sectionId });
if (!section) {
throw new NotFoundException('Section not found');
}
const position: string = createTaskRequest.lastTaskPosition
? LexoRank.parse(createTaskRequest.lastTaskPosition).genNext().toString()
: LexoRank.min().toString();

const task = await this.taskRepository.save({
position: createTaskRequest.position,
section,
position,
});
return new CreateTaskResponse(task);
}
Expand All @@ -48,4 +49,24 @@ export class TaskService {
await this.taskRepository.save(task);
return new UpdateTaskResponse(task);
}

async move(id: number, moveTaskRequest: MoveTaskRequest) {
const task = await this.taskRepository.findOneBy({ id });
if (!task) {
throw new NotFoundException('Task not found');
}

const section = await this.sectionRepository.findOneBy({ id: moveTaskRequest.sectionId });
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);
}
}
Loading

0 comments on commit ea4f1e0

Please sign in to comment.