-
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.
feat(controller): add function to create crud controller
also use common exception filter as the default filter instead of index filter
- Loading branch information
1 parent
886fd97
commit ac2e073
Showing
10 changed files
with
180 additions
and
41 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,135 @@ | ||
import { | ||
ArgumentMetadata, | ||
Body, | ||
Delete, | ||
Get, | ||
HttpCode, | ||
Injectable, | ||
NotFoundException, | ||
Param, | ||
Post, | ||
Query, | ||
Type, | ||
UsePipes, | ||
ValidationPipe, | ||
ValidationPipeOptions, | ||
} from '@nestjs/common'; | ||
import { ApiBody, ApiQuery } from '@nestjs/swagger'; | ||
import { DatabaseEntityService } from '../modules/database/classes/entity-service.class'; | ||
import { DatabaseEntity } from '../modules/database/classes/entity.class'; | ||
import { ApiGetByIdParams } from './get-by-id.api'; | ||
import { ApiSearchQuery } from './search.api'; | ||
|
||
@Injectable() | ||
export class AbstractValidationPipe extends ValidationPipe { | ||
constructor( | ||
options: ValidationPipeOptions, | ||
private readonly targetTypes: { | ||
body?: Type; | ||
query?: Type; | ||
param?: Type; | ||
custom?: Type; | ||
}, | ||
) { | ||
super(options); | ||
} | ||
|
||
async transform(value: any, metadata: ArgumentMetadata) { | ||
const targetType = this.targetTypes[metadata.type]; | ||
if (!targetType) { | ||
return super.transform(value, metadata); | ||
} | ||
return super.transform(value, { ...metadata, metatype: targetType }); | ||
} | ||
} | ||
|
||
// @todo: support passing generic get by id | ||
// GetByIdParams: Type = ApiGetByIdParams, // unable to make this generic, @ApiParam({ type: GetByIdParams }) requires a name | ||
export function CreateCrudApiController( | ||
AddDto: Type, | ||
UpdateDto: Type, | ||
SearchQuery: Type = ApiSearchQuery, | ||
) { | ||
@UsePipes(new ValidationPipe({ whitelist: true })) | ||
class CrudApiController { | ||
constructor(readonly service: DatabaseEntityService<DatabaseEntity>) {} | ||
|
||
@UsePipes( | ||
new AbstractValidationPipe( | ||
{ whitelist: true, transform: true }, | ||
{ query: SearchQuery }, | ||
), | ||
) | ||
@ApiQuery({ type: SearchQuery }) | ||
@Get() | ||
async list(@Query() query) { | ||
return this.service.list({}, query as any); | ||
} | ||
|
||
@UsePipes( | ||
new AbstractValidationPipe( | ||
{ whitelist: true, transform: true }, | ||
{ param: ApiGetByIdParams }, | ||
), | ||
) | ||
@Get(':id') | ||
async get(@Param() params: ApiGetByIdParams) { | ||
const result = await this.service.get(params.id); | ||
if (!result) { | ||
throw new NotFoundException(); | ||
} | ||
return result; | ||
} | ||
|
||
@UsePipes( | ||
new AbstractValidationPipe( | ||
{ whitelist: true, transform: true }, | ||
{ body: AddDto }, | ||
), | ||
) | ||
@ApiBody({ type: AddDto }) | ||
@HttpCode(201) | ||
@Post() | ||
async add(@Body() body) { | ||
return this.service.add(body as any); | ||
} | ||
|
||
@UsePipes( | ||
new AbstractValidationPipe( | ||
{ whitelist: true, transform: true }, | ||
{ body: UpdateDto, param: ApiGetByIdParams }, | ||
), | ||
) | ||
@ApiBody({ type: UpdateDto }) | ||
@Post(':id') | ||
async update(@Body() body, @Param() params: ApiGetByIdParams) { | ||
const result = await this.service.get(params.id); | ||
|
||
if (!result) { | ||
throw new NotFoundException(); | ||
} | ||
|
||
return this.service.update(result, body as any); | ||
} | ||
|
||
@UsePipes( | ||
new AbstractValidationPipe( | ||
{ whitelist: true, transform: true }, | ||
{ param: ApiGetByIdParams }, | ||
), | ||
) | ||
@HttpCode(204) | ||
@Delete(':id') | ||
async delete(@Param() params: ApiGetByIdParams) { | ||
const result = await this.service.get(params.id); | ||
|
||
if (!result) { | ||
throw new NotFoundException(); | ||
} | ||
|
||
await this.service.delete(result); | ||
} | ||
} | ||
|
||
return CrudApiController; | ||
} |
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,9 +1,9 @@ | ||
import { APP_FILTER } from '@nestjs/core'; | ||
import { IndexExceptionFilter } from '../modules/exception/index-exception.filter'; | ||
import { CommonExceptionFilter } from '../modules/exception/common-exception.filter'; | ||
|
||
export const commonAppProviders = [ | ||
{ | ||
provide: APP_FILTER, | ||
useClass: IndexExceptionFilter, | ||
useClass: CommonExceptionFilter, | ||
}, | ||
]; |
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
26 changes: 26 additions & 0 deletions
26
src/modules/database/functions/create-provider.function.ts
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,26 @@ | ||
import { Type } from '@nestjs/common'; | ||
import { getModelForClass } from '@typegoose/typegoose'; | ||
import mongoose from 'mongoose'; | ||
import { DB_DEFAULT_CONNECTION } from '../../../constants'; | ||
import { DatabaseEntity } from '../classes/entity.class'; | ||
|
||
export function CreateDatabaseEntityProvider( | ||
token: string, | ||
entity: Type<DatabaseEntity>, | ||
collection: string, | ||
) { | ||
return { | ||
provide: token, | ||
useFactory: (connection: mongoose.Connection) => { | ||
return getModelForClass(entity, { | ||
existingConnection: connection, | ||
schemaOptions: { | ||
collection: collection, | ||
read: 'nearest', | ||
versionKey: false, | ||
}, | ||
}); | ||
}, | ||
inject: [DB_DEFAULT_CONNECTION], | ||
}; | ||
} |
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