-
Notifications
You must be signed in to change notification settings - Fork 1
feat(tasks): [#8] Implement task CRUD API endpoints #22
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
be03de1
feat(tasks): implement task and board column CRUD APIs
Zafar7645 6f2131b
chore(backend): refactor type of query param id with ParseIntPipe
Zafar7645 727f900
feat(tasks): apply all suggestions from automated code review
Zafar7645 ef5c294
feat(tasks): apply all suggestions from second round of automated review
Zafar7645 e588e9d
feat(tasks): apply all suggestions from third round of automated review
Zafar7645 964dd7d
feat(tasks): apply all suggestions from fourth round of automated review
Zafar7645 bd941e8
feat(tasks): apply all suggestions from fifth round of automated review
Zafar7645 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 hidden or 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
20 changes: 20 additions & 0 deletions
20
apps/backend/src/board-columns/board-columns.controller.spec.ts
This file contains hidden or 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,20 @@ | ||
| import { Test, TestingModule } from '@nestjs/testing'; | ||
| import { BoardColumnsController } from '@/board-columns/board-columns.controller'; | ||
| import { BoardColumnsService } from '@/board-columns/board-columns.service'; | ||
|
|
||
| describe('BoardColumnsController', () => { | ||
| let controller: BoardColumnsController; | ||
|
|
||
| beforeEach(async () => { | ||
| const module: TestingModule = await Test.createTestingModule({ | ||
| controllers: [BoardColumnsController], | ||
| providers: [BoardColumnsService], | ||
| }).compile(); | ||
|
|
||
| controller = module.get<BoardColumnsController>(BoardColumnsController); | ||
| }); | ||
|
|
||
| it('should be defined', () => { | ||
| expect(controller).toBeDefined(); | ||
| }); | ||
| }); | ||
71 changes: 71 additions & 0 deletions
71
apps/backend/src/board-columns/board-columns.controller.ts
This file contains hidden or 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,71 @@ | ||
| import { | ||
| Controller, | ||
| Get, | ||
| Post, | ||
| Body, | ||
| Patch, | ||
| Param, | ||
| Delete, | ||
| UseGuards, | ||
| Request, | ||
| Query, | ||
| ParseIntPipe, | ||
| } from '@nestjs/common'; | ||
| import { BoardColumnsService } from '@/board-columns/board-columns.service'; | ||
| import { CreateBoardColumnDto } from '@/board-columns/dto/create-board-column.dto'; | ||
| import { UpdateBoardColumnDto } from '@/board-columns/dto/update-board-column.dto'; | ||
| import { JwtAuthGuard } from '@/auth/guards/jwt-auth.guard'; | ||
|
|
||
| @UseGuards(JwtAuthGuard) | ||
| @Controller('board-columns') | ||
| export class BoardColumnsController { | ||
| constructor(private readonly boardColumnsService: BoardColumnsService) {} | ||
|
|
||
| @Post() | ||
| create( | ||
| @Body() createBoardColumnDto: CreateBoardColumnDto, | ||
| @Request() req: { user: { userId: number; email: string } }, | ||
| ) { | ||
| return this.boardColumnsService.create( | ||
| createBoardColumnDto, | ||
| req.user.userId, | ||
| ); | ||
| } | ||
|
|
||
| @Get() | ||
| findAll( | ||
| @Query('projectId', ParseIntPipe) projectId: number, | ||
| @Request() req: { user: { userId: number; email: string } }, | ||
| ) { | ||
| return this.boardColumnsService.findAll(projectId, req.user.userId); | ||
| } | ||
|
|
||
| @Get(':id') | ||
| findOne( | ||
| @Param('id', ParseIntPipe) id: number, | ||
| @Request() req: { user: { userId: number; email: string } }, | ||
| ) { | ||
| return this.boardColumnsService.findOne(id, req.user.userId); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| @Patch(':id') | ||
| update( | ||
| @Param('id', ParseIntPipe) id: number, | ||
| @Body() updateBoardColumnDto: UpdateBoardColumnDto, | ||
| @Request() req: { user: { userId: number; email: string } }, | ||
| ) { | ||
| return this.boardColumnsService.update( | ||
| id, | ||
| updateBoardColumnDto, | ||
| req.user.userId, | ||
| ); | ||
| } | ||
|
|
||
| @Delete(':id') | ||
| remove( | ||
| @Param('id', ParseIntPipe) id: number, | ||
| @Request() req: { user: { userId: number; email: string } }, | ||
| ) { | ||
| return this.boardColumnsService.remove(id, req.user.userId); | ||
| } | ||
| } | ||
This file contains hidden or 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,13 @@ | ||
| import { Module } from '@nestjs/common'; | ||
| import { BoardColumnsService } from '@/board-columns/board-columns.service'; | ||
| import { BoardColumnsController } from '@/board-columns/board-columns.controller'; | ||
| import { TypeOrmModule } from '@nestjs/typeorm'; | ||
| import { BoardColumn } from '@/board-columns/entities/board-column.entity'; | ||
| import { Project } from '@/projects/entities/project.entity'; | ||
|
|
||
| @Module({ | ||
| imports: [TypeOrmModule.forFeature([BoardColumn, Project])], | ||
| controllers: [BoardColumnsController], | ||
| providers: [BoardColumnsService], | ||
| }) | ||
| export class BoardColumnsModule {} |
18 changes: 18 additions & 0 deletions
18
apps/backend/src/board-columns/board-columns.service.spec.ts
This file contains hidden or 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,18 @@ | ||
| import { Test, TestingModule } from '@nestjs/testing'; | ||
| import { BoardColumnsService } from '@/board-columns/board-columns.service'; | ||
|
|
||
| describe('BoardColumnsService', () => { | ||
| let service: BoardColumnsService; | ||
|
|
||
| beforeEach(async () => { | ||
| const module: TestingModule = await Test.createTestingModule({ | ||
| providers: [BoardColumnsService], | ||
| }).compile(); | ||
|
|
||
| service = module.get<BoardColumnsService>(BoardColumnsService); | ||
|
Zafar7645 marked this conversation as resolved.
|
||
| }); | ||
|
|
||
| it('should be defined', () => { | ||
| expect(service).toBeDefined(); | ||
| }); | ||
| }); | ||
114 changes: 114 additions & 0 deletions
114
apps/backend/src/board-columns/board-columns.service.ts
This file contains hidden or 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,114 @@ | ||
| import { | ||
| ForbiddenException, | ||
| Injectable, | ||
| NotFoundException, | ||
| } from '@nestjs/common'; | ||
| import { CreateBoardColumnDto } from '@/board-columns/dto/create-board-column.dto'; | ||
| import { UpdateBoardColumnDto } from '@/board-columns/dto/update-board-column.dto'; | ||
| import { Project } from '@/projects/entities/project.entity'; | ||
| import { InjectRepository } from '@nestjs/typeorm'; | ||
| import { Repository } from 'typeorm'; | ||
| import { BoardColumn } from '@/board-columns/entities/board-column.entity'; | ||
|
|
||
| @Injectable() | ||
| export class BoardColumnsService { | ||
| constructor( | ||
| @InjectRepository(BoardColumn) | ||
| private columnsRepository: Repository<BoardColumn>, | ||
| @InjectRepository(Project) private projectsRepository: Repository<Project>, | ||
| ) {} | ||
|
|
||
| private async verifyProjectAccess( | ||
| projectId: number, | ||
| userId: number, | ||
| ): Promise<void> { | ||
| const project = await this.projectsRepository.findOne({ | ||
| where: { id: projectId }, | ||
| }); | ||
| if (!project) throw new NotFoundException('Project not found'); | ||
| if (project.userId !== userId) { | ||
| throw new ForbiddenException( | ||
| 'You do not have permission to modify this project board', | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private async verifyColumnAccess( | ||
| columnId: number, | ||
| userId: number, | ||
| ): Promise<BoardColumn> { | ||
| const column = await this.columnsRepository.findOne({ | ||
| where: { id: columnId }, | ||
| relations: ['project'], | ||
| }); | ||
| if (!column) throw new NotFoundException('Column not found'); | ||
| if (column.project.userId !== userId) { | ||
| throw new ForbiddenException( | ||
| 'You do not have permission to modify this column', | ||
| ); | ||
| } | ||
| return column; | ||
| } | ||
|
|
||
| async create(createDto: CreateBoardColumnDto, userId: number) { | ||
| return await this.columnsRepository.manager.transaction( | ||
| async (transactionalManager) => { | ||
| const project = await transactionalManager | ||
| .createQueryBuilder(Project, 'project') | ||
| .where('project.id = :id', { id: createDto.projectId }) | ||
| .setLock('pessimistic_write') | ||
| .getOne(); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| if (!project) { | ||
| throw new NotFoundException('Project not found'); | ||
| } | ||
| if (project.userId !== userId) { | ||
| throw new ForbiddenException( | ||
| 'You do not have permission to modify this project board', | ||
| ); | ||
| } | ||
|
|
||
| let order = createDto.order; | ||
| if (order === undefined) { | ||
| const lastColumn = await transactionalManager.findOne(BoardColumn, { | ||
| where: { projectId: createDto.projectId }, | ||
| order: { order: 'DESC' }, | ||
| }); | ||
| order = lastColumn ? lastColumn.order + 1 : 0; | ||
| } | ||
|
|
||
| const column = transactionalManager.create(BoardColumn, { | ||
| ...createDto, | ||
| order, | ||
| }); | ||
| return await transactionalManager.save(column); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| async findAll(projectId: number, userId: number) { | ||
| await this.verifyProjectAccess(projectId, userId); | ||
| return await this.columnsRepository.find({ | ||
| where: { projectId }, | ||
| order: { order: 'ASC' }, | ||
| }); | ||
| } | ||
|
|
||
| async findOne(id: number, userId: number) { | ||
| return await this.verifyColumnAccess(id, userId); | ||
| } | ||
|
|
||
| async update(id: number, updateDto: UpdateBoardColumnDto, userId: number) { | ||
| const column = await this.verifyColumnAccess(id, userId); | ||
| if ('projectId' in updateDto) { | ||
| delete updateDto.projectId; | ||
| } | ||
| const updatedColumn = this.columnsRepository.merge(column, updateDto); | ||
| return await this.columnsRepository.save(updatedColumn); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| async remove(id: number, userId: number) { | ||
| const column = await this.verifyColumnAccess(id, userId); | ||
| return await this.columnsRepository.remove(column); | ||
| } | ||
| } | ||
15 changes: 15 additions & 0 deletions
15
apps/backend/src/board-columns/dto/create-board-column.dto.ts
This file contains hidden or 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,15 @@ | ||
| import { IsNotEmpty, IsString, IsOptional, IsInt } from 'class-validator'; | ||
|
|
||
| export class CreateBoardColumnDto { | ||
| @IsString() | ||
| @IsNotEmpty() | ||
| name: string; | ||
|
|
||
| @IsInt() | ||
| @IsOptional() | ||
| order?: number; | ||
|
|
||
| @IsInt() | ||
| @IsNotEmpty() | ||
| projectId: number; | ||
| } |
6 changes: 6 additions & 0 deletions
6
apps/backend/src/board-columns/dto/update-board-column.dto.ts
This file contains hidden or 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,6 @@ | ||
| import { OmitType, PartialType } from '@nestjs/mapped-types'; | ||
| import { CreateBoardColumnDto } from '@/board-columns/dto/create-board-column.dto'; | ||
|
|
||
| export class UpdateBoardColumnDto extends PartialType( | ||
| OmitType(CreateBoardColumnDto, ['projectId'] as const), | ||
| ) {} |
42 changes: 42 additions & 0 deletions
42
apps/backend/src/board-columns/entities/board-column.entity.ts
This file contains hidden or 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,42 @@ | ||
| import { Project } from '@/projects/entities/project.entity'; | ||
| import { Task } from '@/tasks/entities/task.entity'; | ||
| import { | ||
| Column, | ||
| CreateDateColumn, | ||
| UpdateDateColumn, | ||
| Entity, | ||
| ManyToOne, | ||
| JoinColumn, | ||
| OneToMany, | ||
| PrimaryGeneratedColumn, | ||
| } from 'typeorm'; | ||
|
|
||
| @Entity({ name: 'board_columns' }) | ||
| export class BoardColumn { | ||
| @PrimaryGeneratedColumn() | ||
| id: number; | ||
|
|
||
| @Column() | ||
| name: string; | ||
|
|
||
| @Column() | ||
| order: number; | ||
|
|
||
| @Column({ name: 'project_id' }) | ||
| projectId: number; | ||
|
|
||
| @CreateDateColumn({ name: 'created_at' }) | ||
| createdAt: Date; | ||
|
|
||
| @UpdateDateColumn({ name: 'updated_at' }) | ||
| updatedAt: Date; | ||
|
|
||
| @ManyToOne(() => Project, (project) => project.boardColumns, { | ||
| onDelete: 'CASCADE', | ||
| }) | ||
| @JoinColumn({ name: 'project_id' }) | ||
| project: Project; | ||
|
|
||
| @OneToMany(() => Task, (task) => task.column) | ||
| tasks: Task[]; | ||
| } |
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.