-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feat] 태스크 위치 이동 API 구현 #53
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5b4467e
chore: TypeORM, MySQL 연동
yangchef1 b8ced69
feat: 태스크 생성 API 구현
yangchef1 16b6c4d
feat: 태스크 수정 API 구현
yangchef1 93dcb8b
refactor: LexoRank 알고리즘에 맞춰 생성 API 수정
yangchef1 436f06a
feat: 태스크 이동 API 구현
yangchef1 3c72a29
feat: 태스크의 섹션 간 이동 기능 추가 구현
yangchef1 4cfb56c
fix: dev 브랜치와 충돌 해결
yangchef1 83e9a2e
fix: 중복 import 코드 삭제
yangchef1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
export class CreateTaskRequest { | ||
position: number; | ||
|
||
sectionId: number; | ||
lastTaskPosition: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export class MoveTaskRequest { | ||
sectionId: number; | ||
|
||
beforePosition: string; | ||
|
||
afterPosition: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,5 +17,5 @@ export class UpdateTaskResponse { | |
|
||
sectionId: number; | ||
|
||
position: number; | ||
position: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
@@ -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); | ||
} | ||
|
@@ -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(); | ||
Comment on lines
+65
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lexo rank 사용법이 굉장히 간단하고 좋네요. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 맞습니다 ㅋㅋㅋ |
||
|
||
await this.taskRepository.save(task); | ||
return new MoveTaskResponse(task); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡
update
와move
사이에는 많은 공통 로직이 있어보입니다.update
를 더 확장성을 고려해서 만들고 그 로직을 활용하는 방향으로move
를 구현하는 것이 좋을 것 같아요!그동안 마스터님들의 말슴은
보통 세번 반복되면 리팩토링한다는 것이긴 합니다.
한번 더 겹치면 고려해주세용~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update
랑move
는 별도의 스토리로 구성한 만큼, 서로 의존성을 갖지 않게 하고 싶어서 update를 move에서 활용하는 설계는 고려하지 않았던 것 같습니다.대신에 ,, 사실 같은 코드가 반복되다 보니, 아래 2가지 부분을 별도 메서드로 분리할까 고민했었습니다.
아래 코드를 분리하면 update, move외에 다른 메서드에서도 재사용할 수 있을 것 같아서용 ,,
작성할 때는 분리할 정도의 코드는 아니라고 생각했었는데, 말씀 듣고 다시 한번 생각해보니 다른 생성, 조회, 삭제 메서드에서도 활용될 수 있는 재사용성 높은 코드라서 분리하는게 여러모로 좋아보이네요 ...!