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 READ_PROCESS = 'READ_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 {
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 {
Body,
ClassSerializerInterceptor,
Delete,
Get,
Inject,
@ -8,7 +7,6 @@ import {
Patch,
Post,
Query,
UseInterceptors,
UsePipes,
ValidationPipe,
} from '@nestjs/common';
@ -27,9 +25,11 @@ 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 {
export function getBaseController(option?: ControllerOption): any {
class SkeletonCRUDController implements ISkeletonCRUDController {
constructor(
@Inject(CREATE_PROCESS)
public readonly createProcess,
@ -50,8 +50,8 @@ export class SkeletonCRUDController implements ISkeletonCRUDController {
return await CreateExcutor.bootstrap(this.createProcess, body);
}
@Delete(':id')
async delete(@Param('id') id) {
@Delete(`:${option?.uniqueIdentifier ?? 'id'}`)
async delete(@Param(`${option?.uniqueIdentifier}`) id) {
return await DeleteExecutor.bootstrap(this.deleteProcess, id);
}
@ -66,13 +66,16 @@ export class SkeletonCRUDController implements ISkeletonCRUDController {
return await PaginationExecutor.bootstrap(this.paginationProcess, params);
}
@Get(':id')
async read(@Param('id') id) {
@Get(`:${option?.uniqueIdentifier}`)
async read(@Param(`${option?.uniqueIdentifier}`) id) {
return await ReadExecutor.bootstrap(this.readProcess, id);
}
@Patch(':id')
async update(@Param('id') id, @Body() body) {
@Patch(`:${option?.uniqueIdentifier}`)
async update(@Param(`${option?.uniqueIdentifier}`) id, @Body() body) {
return await UpdateExecutor.bootstrap(this.updateProcess, id, body);
}
}
return SkeletonCRUDController;
}

View File

@ -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',
}) {}

View File

@ -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();
}
}