-
Notifications
You must be signed in to change notification settings - Fork 1
feat(projects): [#5] Implement project CRUD API endpoints #20
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
2 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { IsNotEmpty, IsOptional, IsString, Matches } from 'class-validator'; | ||
|
|
||
| export class CreateProjectDto { | ||
| @IsString() | ||
| @IsNotEmpty() | ||
| @Matches(/\S/, { message: 'name must contain non-whitespace characters' }) | ||
| name: string; | ||
|
|
||
| @IsString() | ||
| @IsOptional() | ||
| description?: string; | ||
| } | ||
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,4 @@ | ||
| import { PartialType } from '@nestjs/mapped-types'; | ||
| import { CreateProjectDto } from '@/projects/dto/create-project.dto'; | ||
|
|
||
| export class UpdateProjectDto extends PartialType(CreateProjectDto) {} |
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,35 @@ | ||
| import { User } from '@/users/user.entity'; | ||
| import { | ||
| Column, | ||
| CreateDateColumn, | ||
| Entity, | ||
| JoinColumn, | ||
| ManyToOne, | ||
| PrimaryGeneratedColumn, | ||
| UpdateDateColumn, | ||
| } from 'typeorm'; | ||
|
|
||
| @Entity({ name: 'projects' }) | ||
| export class Project { | ||
| @PrimaryGeneratedColumn() | ||
| id: number; | ||
|
|
||
| @Column() | ||
| name: string; | ||
|
|
||
| @Column({ nullable: true }) | ||
| description: string; | ||
|
|
||
| @Column({ name: 'user_id' }) | ||
| userId: number; | ||
|
|
||
| @ManyToOne(() => User, (user) => user.projects, { onDelete: 'CASCADE' }) | ||
| @JoinColumn({ name: 'user_id' }) | ||
| user: User; | ||
|
|
||
| @CreateDateColumn({ name: 'created_at' }) | ||
| createdAt: Date; | ||
|
|
||
| @UpdateDateColumn({ name: 'updated_at' }) | ||
| updatedAt: Date; | ||
| } |
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 { ProjectsController } from '@/projects/projects.controller'; | ||
| import { ProjectsService } from '@/projects/projects.service'; | ||
|
|
||
| describe('ProjectsController', () => { | ||
| let controller: ProjectsController; | ||
|
|
||
| beforeEach(async () => { | ||
| const module: TestingModule = await Test.createTestingModule({ | ||
| controllers: [ProjectsController], | ||
| providers: [ProjectsService], | ||
| }).compile(); | ||
Zafar7645 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| controller = module.get<ProjectsController>(ProjectsController); | ||
| }); | ||
|
|
||
| it('should be defined', () => { | ||
| expect(controller).toBeDefined(); | ||
| }); | ||
| }); | ||
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,59 @@ | ||
| import { | ||
| Controller, | ||
| Get, | ||
| Post, | ||
| Body, | ||
| Patch, | ||
| Param, | ||
| Delete, | ||
| UseGuards, | ||
| Request, | ||
| } from '@nestjs/common'; | ||
| import { ProjectsService } from '@/projects/projects.service'; | ||
| import { CreateProjectDto } from '@/projects/dto/create-project.dto'; | ||
| import { UpdateProjectDto } from '@/projects/dto/update-project.dto'; | ||
| import { JwtAuthGuard } from '@/auth/guards/jwt-auth.guard'; | ||
|
|
||
| @UseGuards(JwtAuthGuard) | ||
| @Controller('projects') | ||
| export class ProjectsController { | ||
| constructor(private readonly projectsService: ProjectsService) {} | ||
|
|
||
| @Post() | ||
| create( | ||
| @Body() createProjectDto: CreateProjectDto, | ||
| @Request() req: { user: { userId: number; email: string } }, | ||
| ) { | ||
| return this.projectsService.create(createProjectDto, req.user.userId); | ||
| } | ||
|
|
||
| @Get() | ||
| findAll(@Request() req: { user: { userId: number; email: string } }) { | ||
| return this.projectsService.findAll(req.user.userId); | ||
| } | ||
|
|
||
| @Get(':id') | ||
| findOne( | ||
| @Param('id') id: string, | ||
| @Request() req: { user: { userId: number; email: string } }, | ||
| ) { | ||
| return this.projectsService.findOne(+id, req.user.userId); | ||
| } | ||
|
|
||
| @Patch(':id') | ||
| update( | ||
| @Param('id') id: string, | ||
| @Body() updateProjectDto: UpdateProjectDto, | ||
| @Request() req: { user: { userId: number; email: string } }, | ||
| ) { | ||
| return this.projectsService.update(+id, updateProjectDto, req.user.userId); | ||
| } | ||
|
|
||
| @Delete(':id') | ||
| remove( | ||
| @Param('id') id: string, | ||
| @Request() req: { user: { userId: number; email: string } }, | ||
| ) { | ||
| return this.projectsService.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,12 @@ | ||
| import { Module } from '@nestjs/common'; | ||
| import { ProjectsService } from '@/projects/projects.service'; | ||
| import { ProjectsController } from '@/projects/projects.controller'; | ||
| import { Project } from '@/projects/entities/project.entity'; | ||
| import { TypeOrmModule } from '@nestjs/typeorm'; | ||
|
|
||
| @Module({ | ||
| imports: [TypeOrmModule.forFeature([Project])], | ||
| controllers: [ProjectsController], | ||
| providers: [ProjectsService], | ||
| }) | ||
| export class ProjectsModule {} |
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 { ProjectsService } from '@/projects/projects.service'; | ||
|
|
||
| describe('ProjectsService', () => { | ||
| let service: ProjectsService; | ||
|
|
||
| beforeEach(async () => { | ||
| const module: TestingModule = await Test.createTestingModule({ | ||
| providers: [ProjectsService], | ||
| }).compile(); | ||
Zafar7645 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| service = module.get<ProjectsService>(ProjectsService); | ||
| }); | ||
|
|
||
| it('should be defined', () => { | ||
| expect(service).toBeDefined(); | ||
| }); | ||
| }); | ||
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,61 @@ | ||
| import { Injectable, NotFoundException } from '@nestjs/common'; | ||
| import { CreateProjectDto } from '@/projects/dto/create-project.dto'; | ||
| import { UpdateProjectDto } from '@/projects/dto/update-project.dto'; | ||
| import { Repository } from 'typeorm'; | ||
| import { Project } from '@/projects/entities/project.entity'; | ||
| import { InjectRepository } from '@nestjs/typeorm'; | ||
|
|
||
| @Injectable() | ||
| export class ProjectsService { | ||
| constructor( | ||
| @InjectRepository(Project) | ||
| private projectsRepository: Repository<Project>, | ||
| ) {} | ||
|
|
||
| async create(createProjectDto: CreateProjectDto, userId: number) { | ||
| const project = this.projectsRepository.create({ | ||
| ...createProjectDto, | ||
| userId, | ||
| }); | ||
|
|
||
| return await this.projectsRepository.save(project); | ||
| } | ||
|
|
||
| async findAll(userId: number) { | ||
| return await this.projectsRepository.find({ | ||
| where: { userId }, | ||
| order: { createdAt: 'DESC' }, | ||
| }); | ||
| } | ||
|
|
||
| async findOne(id: number, userId: number) { | ||
| const project = await this.projectsRepository.findOne({ | ||
| where: { id, userId }, | ||
| }); | ||
|
|
||
| if (!project) { | ||
| throw new NotFoundException( | ||
| `Project with ID "${id}" not found or you don't have access.`, | ||
| ); | ||
| } | ||
|
|
||
| return project; | ||
| } | ||
|
|
||
| async update(id: number, updateProjectDto: UpdateProjectDto, userId: number) { | ||
| const project = await this.findOne(id, userId); | ||
|
|
||
| const updatedProject = this.projectsRepository.merge( | ||
| project, | ||
| updateProjectDto, | ||
| ); | ||
|
|
||
| return await this.projectsRepository.save(updatedProject); | ||
| } | ||
|
|
||
| async remove(id: number, userId: number) { | ||
| const project = await this.findOne(id, userId); | ||
|
|
||
| return await this.projectsRepository.remove(project); | ||
| } | ||
| } |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.