wip: new way to extend a controller

This commit is contained in:
Supan Adit Pratama 2024-11-02 00:08:02 +07:00
parent 1456f3e2bb
commit 65b4915831
7 changed files with 91 additions and 52 deletions

View File

@ -4,3 +4,5 @@ export const LIST_PROCESS = 'LIST_PROCESS';
export const PAGINATION_PROCESS = 'PAGINATION_PROCESS'; export const PAGINATION_PROCESS = 'PAGINATION_PROCESS';
export const READ_PROCESS = 'READ_PROCESS'; export const READ_PROCESS = 'READ_PROCESS';
export const UPDATE_PROCESS = 'UPDATE_PROCESS'; export const UPDATE_PROCESS = 'UPDATE_PROCESS';
export const UNIQUE_IDENTIFIER = 'PARAM:UNIQUE_IDENTIFIER';

View File

@ -1,5 +1,7 @@
import { UNIQUE_IDENTIFIER } from '../constants';
export function SkeletonController(): ClassDecorator { export function SkeletonController(): ClassDecorator {
return (target: object) => { return (target: object) => {
console.log('STARTED', target); Reflect.defineMetadata(UNIQUE_IDENTIFIER, 'haha', target);
}; };
} }

View 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;
};
};

View File

@ -0,0 +1,3 @@
export interface ControllerOption {
uniqueIdentifier?: string;
}

View File

@ -1,6 +1,5 @@
import { import {
Body, Body,
ClassSerializerInterceptor,
Delete, Delete,
Get, Get,
Inject, Inject,
@ -8,7 +7,6 @@ import {
Patch, Patch,
Post, Post,
Query, Query,
UseInterceptors,
UsePipes, UsePipes,
ValidationPipe, ValidationPipe,
} from '@nestjs/common'; } from '@nestjs/common';
@ -27,52 +25,57 @@ import { DeleteExecutor } from './executors/delete.executor';
import { ListExecutor } from './executors/list.executor'; import { ListExecutor } from './executors/list.executor';
import { ReadExecutor } from './executors/read.executor'; import { ReadExecutor } from './executors/read.executor';
import { UpdateExecutor } from './executors/update.executor'; import { UpdateExecutor } from './executors/update.executor';
import { ControllerOption } from './interfaces/controller/controller.option';
import { ISkeletonCRUDController } from './interfaces/controller/skeleton-crud.controller.interface'; import { ISkeletonCRUDController } from './interfaces/controller/skeleton-crud.controller.interface';
export class SkeletonCRUDController implements ISkeletonCRUDController { export function getBaseController(option?: ControllerOption): any {
constructor( class SkeletonCRUDController implements ISkeletonCRUDController {
@Inject(CREATE_PROCESS) constructor(
public readonly createProcess, @Inject(CREATE_PROCESS)
@Inject(DELETE_PROCESS) public readonly createProcess,
public readonly deleteProcess, @Inject(DELETE_PROCESS)
@Inject(LIST_PROCESS) public readonly deleteProcess,
public readonly listProcess, @Inject(LIST_PROCESS)
@Inject(PAGINATION_PROCESS) public readonly listProcess,
public readonly paginationProcess, @Inject(PAGINATION_PROCESS)
@Inject(READ_PROCESS) public readonly paginationProcess,
public readonly readProcess, @Inject(READ_PROCESS)
@Inject(UPDATE_PROCESS) public readonly readProcess,
public readonly updateProcess, @Inject(UPDATE_PROCESS)
) {} public readonly updateProcess,
) {}
@Post() @Post()
async create(@Body() body): Promise<any> { async create(@Body() body): Promise<any> {
return await CreateExcutor.bootstrap(this.createProcess, body); 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') return SkeletonCRUDController;
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);
}
} }

View File

@ -1,5 +1,7 @@
import { getBaseController } from '@aditama-labs/nest-autocrud/skeleton';
import { Controller } from '@nestjs/common'; import { Controller } from '@nestjs/common';
import { SkeletonCRUDController } from 'libs';
@Controller('example/custom') @Controller('example/custom')
export class CustomController extends SkeletonCRUDController {} export class CustomController extends getBaseController({
uniqueIdentifier: 'key',
}) {}

View File

@ -1,6 +1,14 @@
import { Controller } from '@nestjs/common'; import { UniqueOverride } from '@aditama-labs/nest-autocrud/skeleton/src/decoratos/unique-override.decorator';
import { SkeletonController, SkeletonCRUDController } from 'libs'; import { Controller, Get } from '@nestjs/common';
import { getBaseController, SkeletonController, UNIQUE_IDENTIFIER } from 'libs';
@Controller('example/simple')
@SkeletonController() @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();
}
}