mirror of
https://github.com/aditama-labs/nest-autocrud.git
synced 2024-11-21 10:46:22 +00:00
wip: new way to extend a controller
This commit is contained in:
parent
1456f3e2bb
commit
65b4915831
@ -4,3 +4,5 @@ export const LIST_PROCESS = 'LIST_PROCESS';
|
||||
export const PAGINATION_PROCESS = 'PAGINATION_PROCESS';
|
||||
export const READ_PROCESS = 'READ_PROCESS';
|
||||
export const UPDATE_PROCESS = 'UPDATE_PROCESS';
|
||||
|
||||
export const UNIQUE_IDENTIFIER = 'PARAM:UNIQUE_IDENTIFIER';
|
||||
|
@ -1,5 +1,7 @@
|
||||
import { UNIQUE_IDENTIFIER } from '../constants';
|
||||
|
||||
export function SkeletonController(): ClassDecorator {
|
||||
return (target: object) => {
|
||||
console.log('STARTED', target);
|
||||
Reflect.defineMetadata(UNIQUE_IDENTIFIER, 'haha', target);
|
||||
};
|
||||
}
|
||||
|
19
libs/skeleton/src/decoratos/unique-override.decorator.ts
Normal file
19
libs/skeleton/src/decoratos/unique-override.decorator.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { UNIQUE_IDENTIFIER } from '../constants';
|
||||
|
||||
export const UniqueOverride = (): MethodDecorator => {
|
||||
return (
|
||||
target: object,
|
||||
key: string | symbol,
|
||||
descriptor: TypedPropertyDescriptor<any>,
|
||||
) => {
|
||||
// const result = Reflect.getMetadata(UNIQUE_IDENTIFIER, descriptor.value);
|
||||
// console.log(result);
|
||||
// console.log(Object.getOwnPropertyNames(props));
|
||||
|
||||
let result = Reflect.getOwnMetadata(UNIQUE_IDENTIFIER, target);
|
||||
console.log(target.constructor);
|
||||
|
||||
console.log(result);
|
||||
return descriptor;
|
||||
};
|
||||
};
|
@ -0,0 +1,3 @@
|
||||
export interface ControllerOption {
|
||||
uniqueIdentifier?: string;
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
import {
|
||||
Body,
|
||||
ClassSerializerInterceptor,
|
||||
Delete,
|
||||
Get,
|
||||
Inject,
|
||||
@ -8,7 +7,6 @@ import {
|
||||
Patch,
|
||||
Post,
|
||||
Query,
|
||||
UseInterceptors,
|
||||
UsePipes,
|
||||
ValidationPipe,
|
||||
} from '@nestjs/common';
|
||||
@ -27,52 +25,57 @@ import { DeleteExecutor } from './executors/delete.executor';
|
||||
import { ListExecutor } from './executors/list.executor';
|
||||
import { ReadExecutor } from './executors/read.executor';
|
||||
import { UpdateExecutor } from './executors/update.executor';
|
||||
import { ControllerOption } from './interfaces/controller/controller.option';
|
||||
import { ISkeletonCRUDController } from './interfaces/controller/skeleton-crud.controller.interface';
|
||||
|
||||
export class SkeletonCRUDController implements ISkeletonCRUDController {
|
||||
constructor(
|
||||
@Inject(CREATE_PROCESS)
|
||||
public readonly createProcess,
|
||||
@Inject(DELETE_PROCESS)
|
||||
public readonly deleteProcess,
|
||||
@Inject(LIST_PROCESS)
|
||||
public readonly listProcess,
|
||||
@Inject(PAGINATION_PROCESS)
|
||||
public readonly paginationProcess,
|
||||
@Inject(READ_PROCESS)
|
||||
public readonly readProcess,
|
||||
@Inject(UPDATE_PROCESS)
|
||||
public readonly updateProcess,
|
||||
) {}
|
||||
export function getBaseController(option?: ControllerOption): any {
|
||||
class SkeletonCRUDController implements ISkeletonCRUDController {
|
||||
constructor(
|
||||
@Inject(CREATE_PROCESS)
|
||||
public readonly createProcess,
|
||||
@Inject(DELETE_PROCESS)
|
||||
public readonly deleteProcess,
|
||||
@Inject(LIST_PROCESS)
|
||||
public readonly listProcess,
|
||||
@Inject(PAGINATION_PROCESS)
|
||||
public readonly paginationProcess,
|
||||
@Inject(READ_PROCESS)
|
||||
public readonly readProcess,
|
||||
@Inject(UPDATE_PROCESS)
|
||||
public readonly updateProcess,
|
||||
) {}
|
||||
|
||||
@Post()
|
||||
async create(@Body() body): Promise<any> {
|
||||
return await CreateExcutor.bootstrap(this.createProcess, body);
|
||||
@Post()
|
||||
async create(@Body() body): Promise<any> {
|
||||
return await CreateExcutor.bootstrap(this.createProcess, body);
|
||||
}
|
||||
|
||||
@Delete(`:${option?.uniqueIdentifier ?? 'id'}`)
|
||||
async delete(@Param(`${option?.uniqueIdentifier}`) id) {
|
||||
return await DeleteExecutor.bootstrap(this.deleteProcess, id);
|
||||
}
|
||||
|
||||
@Get('list')
|
||||
async list() {
|
||||
return await ListExecutor.bootstrap(this.listProcess);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@UsePipes(new ValidationPipe({ transform: true }))
|
||||
async pagination(@Query() params: PaginationParamDTO) {
|
||||
return await PaginationExecutor.bootstrap(this.paginationProcess, params);
|
||||
}
|
||||
|
||||
@Get(`:${option?.uniqueIdentifier}`)
|
||||
async read(@Param(`${option?.uniqueIdentifier}`) id) {
|
||||
return await ReadExecutor.bootstrap(this.readProcess, id);
|
||||
}
|
||||
|
||||
@Patch(`:${option?.uniqueIdentifier}`)
|
||||
async update(@Param(`${option?.uniqueIdentifier}`) id, @Body() body) {
|
||||
return await UpdateExecutor.bootstrap(this.updateProcess, id, body);
|
||||
}
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
async delete(@Param('id') id) {
|
||||
return await DeleteExecutor.bootstrap(this.deleteProcess, id);
|
||||
}
|
||||
|
||||
@Get('list')
|
||||
async list() {
|
||||
return await ListExecutor.bootstrap(this.listProcess);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@UsePipes(new ValidationPipe({ transform: true }))
|
||||
async pagination(@Query() params: PaginationParamDTO) {
|
||||
return await PaginationExecutor.bootstrap(this.paginationProcess, params);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async read(@Param('id') id) {
|
||||
return await ReadExecutor.bootstrap(this.readProcess, id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
async update(@Param('id') id, @Body() body) {
|
||||
return await UpdateExecutor.bootstrap(this.updateProcess, id, body);
|
||||
}
|
||||
return SkeletonCRUDController;
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
import { getBaseController } from '@aditama-labs/nest-autocrud/skeleton';
|
||||
import { Controller } from '@nestjs/common';
|
||||
import { SkeletonCRUDController } from 'libs';
|
||||
|
||||
@Controller('example/custom')
|
||||
export class CustomController extends SkeletonCRUDController {}
|
||||
export class CustomController extends getBaseController({
|
||||
uniqueIdentifier: 'key',
|
||||
}) {}
|
||||
|
@ -1,6 +1,14 @@
|
||||
import { Controller } from '@nestjs/common';
|
||||
import { SkeletonController, SkeletonCRUDController } from 'libs';
|
||||
import { UniqueOverride } from '@aditama-labs/nest-autocrud/skeleton/src/decoratos/unique-override.decorator';
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { getBaseController, SkeletonController, UNIQUE_IDENTIFIER } from 'libs';
|
||||
|
||||
@Controller('example/simple')
|
||||
@SkeletonController()
|
||||
export class SimpleController extends SkeletonCRUDController {}
|
||||
@Controller('example/simple')
|
||||
export class SimpleController extends getBaseController() {
|
||||
@UniqueOverride()
|
||||
@Get('list')
|
||||
async list(): Promise<any> {
|
||||
console.log(Reflect.getOwnMetadata(UNIQUE_IDENTIFIER, this.constructor));
|
||||
return super.list();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user