-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
184fdf6
commit fc60d4e
Showing
16 changed files
with
363 additions
and
20 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
|
||
import { Roles } from '~/app/auth/auth.decorator'; | ||
import { Session } from '~/schemas/session.schema'; | ||
|
||
@Injectable() | ||
export class RoleGuard implements CanActivate { | ||
constructor(private readonly reflector: Reflector) {} | ||
|
||
canActivate(context: ExecutionContext): boolean { | ||
const roles = this.reflector.get(Roles, context.getHandler()); | ||
|
||
if (roles) { | ||
const req = context.switchToHttp().getRequest(); | ||
const session = req.user as Session; | ||
|
||
return roles.includes(session.user.role); | ||
} | ||
|
||
return true; | ||
} | ||
} |
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,96 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
HttpStatus, | ||
Param, | ||
Post, | ||
Put, | ||
Query, | ||
Res, | ||
UseGuards, | ||
} from '@nestjs/common'; | ||
import { ApiResponse, ApiTags } from '@nestjs/swagger'; | ||
import { Response } from 'express'; | ||
|
||
import { | ||
CreateAuthorDto, | ||
SearchAuthorDto, | ||
UpdateAuthorDto, | ||
} from '~/app/author/author.dto'; | ||
import { AuthorService } from '~/app/author/author.service'; | ||
import { JwtAuthGuard } from '~/app/auth/auth.guard'; | ||
import { RoleGuard } from '~/app/auth/role.guard'; | ||
import { Roles } from '~/app/auth/auth.decorator'; | ||
import { Role } from '~/types/role.enum'; | ||
|
||
@ApiTags('Author') | ||
@Controller('author') | ||
export class AuthorController { | ||
constructor(private readonly authorService: AuthorService) {} | ||
|
||
@Post('create') | ||
@ApiResponse({ | ||
status: 201, | ||
description: 'Create new author', | ||
}) | ||
@UseGuards(JwtAuthGuard, RoleGuard) | ||
@Roles([Role.ADMIN]) | ||
async create(@Body() body: CreateAuthorDto, @Res() res: Response) { | ||
const data = await this.authorService.create(body); | ||
|
||
return res.status(HttpStatus.CREATED).json(data); | ||
} | ||
|
||
@Get(':id') | ||
@ApiResponse({ | ||
status: 200, | ||
description: 'Get a author', | ||
}) | ||
async getById(@Param('id') id: string, @Res() res: Response) { | ||
const data = await this.authorService.getById(id); | ||
|
||
return res.status(HttpStatus.CREATED).json(data); | ||
} | ||
|
||
@Delete('delete/:id') | ||
@ApiResponse({ | ||
status: 201, | ||
description: 'Delete a author', | ||
}) | ||
@UseGuards(JwtAuthGuard, RoleGuard) | ||
@Roles([Role.ADMIN]) | ||
async delete(@Param('id') id: string, @Res() res: Response) { | ||
await this.authorService.delete(id); | ||
|
||
return res.status(HttpStatus.OK).send(); | ||
} | ||
|
||
@Put('update/:id') | ||
@ApiResponse({ | ||
status: 201, | ||
description: 'Update a author', | ||
}) | ||
@UseGuards(JwtAuthGuard, RoleGuard) | ||
@Roles([Role.ADMIN]) | ||
async update( | ||
@Body() body: UpdateAuthorDto, | ||
@Param('id') id: string, | ||
@Res() res: Response, | ||
) { | ||
const data = await this.authorService.update(id, body); | ||
|
||
return res.status(HttpStatus.OK).json(data); | ||
} | ||
|
||
@Get('search') | ||
@ApiResponse({ | ||
status: 200, | ||
description: 'Author search', | ||
}) | ||
async search(@Query() query: SearchAuthorDto, @Res() res: Response) { | ||
const data = await this.authorService.search(query); | ||
return res.status(HttpStatus.OK).json(data); | ||
} | ||
} |
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,67 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { Transform } from 'class-transformer'; | ||
import { IsDateString, IsNotEmpty, IsOptional, Min } from 'class-validator'; | ||
|
||
import { SortOrder } from '~/types/filter.enum'; | ||
|
||
export class UpdateAuthorDto { | ||
@ApiProperty({ required: false }) | ||
@IsOptional() | ||
@IsNotEmpty() | ||
name: string; | ||
|
||
@ApiProperty({ required: false }) | ||
@IsOptional() | ||
@IsDateString() | ||
birthDate: Date; | ||
|
||
@ApiProperty({ required: false }) | ||
@IsOptional() | ||
@IsNotEmpty() | ||
biography: string; | ||
} | ||
|
||
export class CreateAuthorDto { | ||
@ApiProperty() | ||
@IsNotEmpty() | ||
name: string; | ||
|
||
@ApiProperty() | ||
@IsDateString() | ||
birthDate: Date; | ||
|
||
@ApiProperty() | ||
@IsNotEmpty() | ||
biography: string; | ||
} | ||
|
||
export class SearchAuthorDto { | ||
@ApiProperty({ required: false }) | ||
@Transform(params => { | ||
if (params.value) { | ||
if (params.value.trim().length == 0) return undefined; | ||
} | ||
|
||
return params.value; | ||
}) | ||
query: string; | ||
|
||
@ApiProperty({ default: 1, required: false }) | ||
@Transform(params => parseInt(params.value)) | ||
@IsOptional() | ||
@Min(1) | ||
page: number = 1; | ||
|
||
@ApiProperty({ enum: SortOrder, default: SortOrder.DESC, required: false }) | ||
sortOrder: SortOrder; | ||
|
||
@ApiProperty({ required: false }) | ||
@IsOptional() | ||
sortField: string; | ||
|
||
@ApiProperty({ default: 12, required: false }) | ||
@Transform(params => parseInt(params.value)) | ||
@IsOptional() | ||
@Min(1) | ||
perPage: number = 12; | ||
} |
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,15 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { MongooseModule } from '@nestjs/mongoose'; | ||
|
||
import { AuthorController } from '~/app/author/author.controller'; | ||
import { AuthorService } from '~/app/author/author.service'; | ||
import { Author, AuthorSchema } from '~/schemas/author.schema'; | ||
|
||
@Module({ | ||
imports: [ | ||
MongooseModule.forFeature([{ name: Author.name, schema: AuthorSchema }]), | ||
], | ||
controllers: [AuthorController], | ||
providers: [AuthorService], | ||
}) | ||
export class AuthorModule {} |
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,44 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import { FilterQuery, Model } from 'mongoose'; | ||
|
||
import { | ||
CreateAuthorDto, | ||
SearchAuthorDto, | ||
UpdateAuthorDto, | ||
} from '~/app/author/author.dto'; | ||
import { Author } from '~/schemas/author.schema'; | ||
import { paginate } from '~/utils/funcs/pagination'; | ||
|
||
@Injectable() | ||
export class AuthorService { | ||
constructor( | ||
@InjectModel(Author.name) private readonly authorModel: Model<Author>, | ||
) {} | ||
|
||
async create(data: CreateAuthorDto) { | ||
return this.authorModel.create(data); | ||
} | ||
|
||
async update(id: string, data: UpdateAuthorDto) { | ||
return this.authorModel.findByIdAndUpdate(id, data, { new: true }); | ||
} | ||
|
||
async delete(id: string) { | ||
return this.authorModel.findByIdAndDelete(id); | ||
} | ||
|
||
async getById(id: string) { | ||
return this.authorModel.findById(id); | ||
} | ||
|
||
async search(data: SearchAuthorDto) { | ||
const query: FilterQuery<Author> = {}; | ||
|
||
if (data.query) { | ||
query.$or = [{ name: { $regex: data.query, $options: 'i' } }]; | ||
} | ||
|
||
return paginate<Author>(this.authorModel, query, data); | ||
} | ||
} |
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,49 @@ | ||
import { Model } from 'mongoose'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { getModelToken } from '@nestjs/mongoose'; | ||
|
||
import { Author } from '~/schemas/author.schema'; | ||
import { AuthorService } from '~/app/author/author.service'; | ||
import { CreateAuthorDto } from '~/app/author/author.dto'; | ||
import { Entity } from '~/types'; | ||
|
||
describe('AuthorService', () => { | ||
let authorModel: Model<Author>; | ||
let authorService: AuthorService; | ||
|
||
afterEach(() => jest.clearAllMocks()); | ||
|
||
beforeEach(async () => { | ||
const app: TestingModule = await Test.createTestingModule({ | ||
controllers: [], | ||
providers: [ | ||
AuthorService, | ||
{ | ||
provide: getModelToken(Author.name), | ||
useValue: Model, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
authorService = app.get<AuthorService>(AuthorService); | ||
authorModel = app.get<Model<Author>>(getModelToken(Author.name)); | ||
}); | ||
|
||
describe('create', () => { | ||
const payload: CreateAuthorDto = { | ||
biography: expect.anything(), | ||
name: expect.anything(), | ||
birthDate: expect.anything(), | ||
}; | ||
|
||
const author = {} as Entity<Author>[]; | ||
it('should return author created', async () => { | ||
jest.spyOn(authorModel, 'create').mockResolvedValue(author); | ||
|
||
const data = await authorService.create(payload); | ||
|
||
expect(data).toBe(author); | ||
expect(authorModel.create).toHaveBeenCalledWith(payload); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.