From 64b7119347d89cd50287e15c49222d2dea3833e6 Mon Sep 17 00:00:00 2001 From: Supan Adit Pratama Date: Wed, 30 Oct 2024 20:06:58 +0700 Subject: [PATCH] wip: full fledge of API CRUD --- .../config-module-options.interface.ts | 14 ++++- libs/prisma/src/prisma.module.ts | 8 +-- .../src/processes/batch/delete.process.ts | 3 -- libs/prisma/src/processes/batch/index.ts | 2 - .../src/processes/batch/update.process.ts | 3 -- libs/prisma/src/processes/create.process.ts | 11 ++-- libs/prisma/src/processes/delete.process.ts | 21 +++++++- libs/prisma/src/processes/index.ts | 2 - libs/prisma/src/processes/list.process.ts | 1 - .../src/processes/partial/batch/index.ts | 1 - .../processes/partial/batch/update.process.ts | 3 -- libs/prisma/src/processes/partial/index.ts | 2 - .../src/processes/partial/update.process.ts | 3 -- libs/prisma/src/processes/read.process.ts | 18 ++++++- libs/prisma/src/processes/update.process.ts | 23 +++++++- libs/skeleton/src/constants.ts | 7 ++- .../skeleton-crud.controller.interface.ts | 7 ++- .../src/processes/batch/delete.process.ts | 3 -- libs/skeleton/src/processes/batch/index.ts | 2 - .../src/processes/batch/update.process.ts | 3 -- libs/skeleton/src/processes/index.ts | 2 - .../src/processes/partial/batch/index.ts | 1 - .../processes/partial/batch/update.process.ts | 3 -- libs/skeleton/src/processes/partial/index.ts | 2 - .../src/processes/partial/update.process.ts | 3 -- libs/skeleton/src/skeleton-crud.controller.ts | 54 ++++++++++++++++--- 26 files changed, 141 insertions(+), 61 deletions(-) delete mode 100644 libs/prisma/src/processes/batch/delete.process.ts delete mode 100644 libs/prisma/src/processes/batch/index.ts delete mode 100644 libs/prisma/src/processes/batch/update.process.ts delete mode 100644 libs/prisma/src/processes/partial/batch/index.ts delete mode 100644 libs/prisma/src/processes/partial/batch/update.process.ts delete mode 100644 libs/prisma/src/processes/partial/index.ts delete mode 100644 libs/prisma/src/processes/partial/update.process.ts delete mode 100644 libs/skeleton/src/processes/batch/delete.process.ts delete mode 100644 libs/skeleton/src/processes/batch/index.ts delete mode 100644 libs/skeleton/src/processes/batch/update.process.ts delete mode 100644 libs/skeleton/src/processes/partial/batch/index.ts delete mode 100644 libs/skeleton/src/processes/partial/batch/update.process.ts delete mode 100644 libs/skeleton/src/processes/partial/index.ts delete mode 100644 libs/skeleton/src/processes/partial/update.process.ts diff --git a/libs/prisma/src/interfaces/config-module-options.interface.ts b/libs/prisma/src/interfaces/config-module-options.interface.ts index da46c26..1cd667f 100644 --- a/libs/prisma/src/interfaces/config-module-options.interface.ts +++ b/libs/prisma/src/interfaces/config-module-options.interface.ts @@ -1,7 +1,19 @@ import { PrismaService } from '../prisma.service'; +import { + PrismaCreateProcess, + PrismaDeleteProcess, + PrismaPaginationProcess, + PrismaReadProcess, + PrismaUpdateProcess, +} from '../processes'; import { PrismaListProcess } from '../processes/list.process'; export interface PrismaModuleOptions { delegate: (prisma: PrismaService) => any; - processList: typeof PrismaListProcess; + processCreate?: typeof PrismaCreateProcess; + processDelete?: typeof PrismaDeleteProcess; + processList?: typeof PrismaListProcess; + processPagination?: typeof PrismaPaginationProcess; + processRead?: typeof PrismaReadProcess; + processUpdate?: typeof PrismaUpdateProcess; } diff --git a/libs/prisma/src/prisma.module.ts b/libs/prisma/src/prisma.module.ts index 45eecfe..ac1705d 100644 --- a/libs/prisma/src/prisma.module.ts +++ b/libs/prisma/src/prisma.module.ts @@ -1,4 +1,4 @@ -import { READ_ENTIRE_PROCESS } from '@aditama-labs/nest-autocrud/skeleton'; +import { LIST_PROCESS } from '@aditama-labs/nest-autocrud/skeleton'; import { DynamicModule, Module } from '@nestjs/common'; import { ConfigurableModuleClass } from './config.module-definition'; import { PRISMA_DELEGATE } from './constants'; @@ -7,7 +7,7 @@ import { PrismaService } from './prisma.service'; import { PrismaListProcess } from './processes/list.process'; @Module({ - exports: [PrismaService, PRISMA_DELEGATE, READ_ENTIRE_PROCESS], + exports: [PrismaService, PRISMA_DELEGATE, LIST_PROCESS], }) export class PrismaModule extends ConfigurableModuleClass { static forRoot(options: PrismaModuleOptions): DynamicModule { @@ -25,7 +25,7 @@ export class PrismaModule extends ConfigurableModuleClass { providers = [ ...providers, { - provide: READ_ENTIRE_PROCESS, + provide: LIST_PROCESS, useClass: options.processList, }, ]; @@ -33,7 +33,7 @@ export class PrismaModule extends ConfigurableModuleClass { providers = [ ...providers, { - provide: READ_ENTIRE_PROCESS, + provide: LIST_PROCESS, useClass: PrismaListProcess, }, ]; diff --git a/libs/prisma/src/processes/batch/delete.process.ts b/libs/prisma/src/processes/batch/delete.process.ts deleted file mode 100644 index aa161a9..0000000 --- a/libs/prisma/src/processes/batch/delete.process.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { BatchDeleteProcess } from '@aditama-labs/nest-autocrud/skeleton'; - -export class PrismaBatchDeleteProcess extends BatchDeleteProcess {} diff --git a/libs/prisma/src/processes/batch/index.ts b/libs/prisma/src/processes/batch/index.ts deleted file mode 100644 index 8521a85..0000000 --- a/libs/prisma/src/processes/batch/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './delete.process'; -export * from './update.process'; diff --git a/libs/prisma/src/processes/batch/update.process.ts b/libs/prisma/src/processes/batch/update.process.ts deleted file mode 100644 index 55c6290..0000000 --- a/libs/prisma/src/processes/batch/update.process.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { BatchUpdateProcess } from '@aditama-labs/nest-autocrud/skeleton'; - -export class PrismaBatchUpdateProcess extends BatchUpdateProcess {} diff --git a/libs/prisma/src/processes/create.process.ts b/libs/prisma/src/processes/create.process.ts index 6e74fd4..d00a241 100644 --- a/libs/prisma/src/processes/create.process.ts +++ b/libs/prisma/src/processes/create.process.ts @@ -5,13 +5,18 @@ export class PrismaCreateProcess extends PrismaProcess implements CreateProcess { - private data; + private dataInsert; + private dataResult; setData(data) { - this.data = data; + this.dataInsert = data; } async process(): Promise { - await this.getDelegate().create(this.data); + this.dataResult = await this.getDelegate().create(this.dataInsert); + } + + result() { + this.dataResult; } } diff --git a/libs/prisma/src/processes/delete.process.ts b/libs/prisma/src/processes/delete.process.ts index c203e69..27ca45d 100644 --- a/libs/prisma/src/processes/delete.process.ts +++ b/libs/prisma/src/processes/delete.process.ts @@ -1,3 +1,20 @@ -import { DeleteProcess } from '@aditama-labs/nest-autocrud/skeleton'; +import { CreateProcess } from '@aditama-labs/nest-autocrud/skeleton'; +import { PrismaProcess } from './prisma.process'; -export class PrismaDeleteProcess extends DeleteProcess {} +export class PrismaDeleteProcess + extends PrismaProcess + implements CreateProcess +{ + private dataResult: any; + private id: any; + + async process(): Promise { + this.dataResult = await this.getDelegate().delete({ + where: { id: this.id }, + }); + } + + result() { + return this.dataResult; + } +} diff --git a/libs/prisma/src/processes/index.ts b/libs/prisma/src/processes/index.ts index 30fbbdf..b574216 100644 --- a/libs/prisma/src/processes/index.ts +++ b/libs/prisma/src/processes/index.ts @@ -1,9 +1,7 @@ -export * from './batch'; export * from './create.process'; export * from './delete.process'; export * from './list.process'; export * from './pagination.process'; -export * from './partial'; export * from './read.process'; export * from './update.process'; diff --git a/libs/prisma/src/processes/list.process.ts b/libs/prisma/src/processes/list.process.ts index c3d3160..0c4b5be 100644 --- a/libs/prisma/src/processes/list.process.ts +++ b/libs/prisma/src/processes/list.process.ts @@ -9,7 +9,6 @@ export class PrismaListProcess extends PrismaProcess implements ListProcess { } result() { - console.log('ASDASDASDASD'); return this.data; } } diff --git a/libs/prisma/src/processes/partial/batch/index.ts b/libs/prisma/src/processes/partial/batch/index.ts deleted file mode 100644 index f1568a4..0000000 --- a/libs/prisma/src/processes/partial/batch/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './update.process'; diff --git a/libs/prisma/src/processes/partial/batch/update.process.ts b/libs/prisma/src/processes/partial/batch/update.process.ts deleted file mode 100644 index 804519a..0000000 --- a/libs/prisma/src/processes/partial/batch/update.process.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { PartialBatchUpdateProcess } from '@aditama-labs/nest-autocrud/skeleton'; - -export class PrismaPartialBatchUpdateProcess extends PartialBatchUpdateProcess {} diff --git a/libs/prisma/src/processes/partial/index.ts b/libs/prisma/src/processes/partial/index.ts deleted file mode 100644 index aeb80b4..0000000 --- a/libs/prisma/src/processes/partial/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './update.process'; -export * from './batch'; diff --git a/libs/prisma/src/processes/partial/update.process.ts b/libs/prisma/src/processes/partial/update.process.ts deleted file mode 100644 index ef5f6fc..0000000 --- a/libs/prisma/src/processes/partial/update.process.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { PartialUpdateProcess } from '@aditama-labs/nest-autocrud/skeleton'; - -export class PrismaPartialUpdateProcess extends PartialUpdateProcess {} diff --git a/libs/prisma/src/processes/read.process.ts b/libs/prisma/src/processes/read.process.ts index f2e623f..4b3e2c1 100644 --- a/libs/prisma/src/processes/read.process.ts +++ b/libs/prisma/src/processes/read.process.ts @@ -1,3 +1,17 @@ -import { ReadProcess } from '@aditama-labs/nest-autocrud/skeleton'; +import { CreateProcess } from '@aditama-labs/nest-autocrud/skeleton'; +import { PrismaProcess } from './prisma.process'; -export class PrismaReadProcess extends ReadProcess {} +export class PrismaReadProcess extends PrismaProcess implements CreateProcess { + private data: any; + private id: any; + + async process(): Promise { + this.data = await this.getDelegate().findUnique({ + where: { id: this.id }, + }); + } + + result() { + return this.data; + } +} diff --git a/libs/prisma/src/processes/update.process.ts b/libs/prisma/src/processes/update.process.ts index 698d6bf..6b251bd 100644 --- a/libs/prisma/src/processes/update.process.ts +++ b/libs/prisma/src/processes/update.process.ts @@ -1,3 +1,22 @@ -import { UpdateProcess } from '@aditama-labs/nest-autocrud/skeleton'; +import { CreateProcess } from '@aditama-labs/nest-autocrud/skeleton'; +import { PrismaProcess } from './prisma.process'; -export class PrismaUpdateProcess extends UpdateProcess {} +export class PrismaUpdateProcess + extends PrismaProcess + implements CreateProcess +{ + private dataResult: any; + private dataUpdate: any; + private id: any; + + async process(): Promise { + this.dataResult = await this.getDelegate().update({ + data: this.dataUpdate, + where: { id: this.id }, + }); + } + + result() { + return this.dataResult; + } +} diff --git a/libs/skeleton/src/constants.ts b/libs/skeleton/src/constants.ts index 3da889d..647b4ef 100644 --- a/libs/skeleton/src/constants.ts +++ b/libs/skeleton/src/constants.ts @@ -1 +1,6 @@ -export const READ_ENTIRE_PROCESS = 'READ_ENTIRE_PROCESS'; +export const CREATE_PROCESS = 'CREATE_PROCESS'; +export const DELETE_PROCESS = 'DELETE_PROCESS'; +export const LIST_PROCESS = 'LIST_PROCESS'; +export const PAGINATION_PROCESS = 'PAGINATION_PROCESS'; +export const READ_PROCESS = 'READ_PROCESS'; +export const UPDATE_PROCESS = 'UPDATE_PROCESS'; diff --git a/libs/skeleton/src/interfaces/controller/skeleton-crud.controller.interface.ts b/libs/skeleton/src/interfaces/controller/skeleton-crud.controller.interface.ts index c7a43d4..194e06b 100644 --- a/libs/skeleton/src/interfaces/controller/skeleton-crud.controller.interface.ts +++ b/libs/skeleton/src/interfaces/controller/skeleton-crud.controller.interface.ts @@ -1,3 +1,8 @@ export interface ISkeletonCRUDController { - readEntire(); + create(); + delete(); + list(); + pagination(); + read(id); + update(); } diff --git a/libs/skeleton/src/processes/batch/delete.process.ts b/libs/skeleton/src/processes/batch/delete.process.ts deleted file mode 100644 index c28cff9..0000000 --- a/libs/skeleton/src/processes/batch/delete.process.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { DefaultProcess } from '../default.process'; - -export class BatchDeleteProcess extends DefaultProcess {} diff --git a/libs/skeleton/src/processes/batch/index.ts b/libs/skeleton/src/processes/batch/index.ts deleted file mode 100644 index 8521a85..0000000 --- a/libs/skeleton/src/processes/batch/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './delete.process'; -export * from './update.process'; diff --git a/libs/skeleton/src/processes/batch/update.process.ts b/libs/skeleton/src/processes/batch/update.process.ts deleted file mode 100644 index 6c7d787..0000000 --- a/libs/skeleton/src/processes/batch/update.process.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { DefaultProcess } from '../default.process'; - -export class BatchUpdateProcess extends DefaultProcess {} diff --git a/libs/skeleton/src/processes/index.ts b/libs/skeleton/src/processes/index.ts index 39243fc..ebf6d2b 100644 --- a/libs/skeleton/src/processes/index.ts +++ b/libs/skeleton/src/processes/index.ts @@ -5,5 +5,3 @@ export * from './list.process'; export * from './pagination.process'; export * from './read.process'; export * from './update.process'; -export * from './batch'; -export * from './partial'; diff --git a/libs/skeleton/src/processes/partial/batch/index.ts b/libs/skeleton/src/processes/partial/batch/index.ts deleted file mode 100644 index f1568a4..0000000 --- a/libs/skeleton/src/processes/partial/batch/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './update.process'; diff --git a/libs/skeleton/src/processes/partial/batch/update.process.ts b/libs/skeleton/src/processes/partial/batch/update.process.ts deleted file mode 100644 index 45710f9..0000000 --- a/libs/skeleton/src/processes/partial/batch/update.process.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { DefaultProcess } from '../../default.process'; - -export class PartialBatchUpdateProcess extends DefaultProcess {} diff --git a/libs/skeleton/src/processes/partial/index.ts b/libs/skeleton/src/processes/partial/index.ts deleted file mode 100644 index aeb80b4..0000000 --- a/libs/skeleton/src/processes/partial/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './update.process'; -export * from './batch'; diff --git a/libs/skeleton/src/processes/partial/update.process.ts b/libs/skeleton/src/processes/partial/update.process.ts deleted file mode 100644 index 44a8e95..0000000 --- a/libs/skeleton/src/processes/partial/update.process.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { DefaultProcess } from '../default.process'; - -export class PartialUpdateProcess extends DefaultProcess {} diff --git a/libs/skeleton/src/skeleton-crud.controller.ts b/libs/skeleton/src/skeleton-crud.controller.ts index cf17fbb..0355164 100644 --- a/libs/skeleton/src/skeleton-crud.controller.ts +++ b/libs/skeleton/src/skeleton-crud.controller.ts @@ -1,16 +1,58 @@ -import { Get, Inject } from '@nestjs/common'; -import { READ_ENTIRE_PROCESS } from './constants'; +import { Delete, Get, Inject, Param, Patch, Post } from '@nestjs/common'; +import { + CREATE_PROCESS, + DELETE_PROCESS, + LIST_PROCESS, + PAGINATION_PROCESS, + READ_PROCESS, + UPDATE_PROCESS, +} from './constants'; import { DefaultExecutor } from './executors/default.executor'; import { ISkeletonCRUDController } from './interfaces/controller/skeleton-crud.controller.interface'; export class SkeletonCRUDController implements ISkeletonCRUDController { constructor( - @Inject(READ_ENTIRE_PROCESS) - public readonly readEntireProcess: any, + @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(): Promise { + return await DefaultExecutor.bootstrap(this.createProcess); + } + + @Delete(':id') + async delete() { + return await DefaultExecutor.bootstrap(this.deleteProcess); + } + @Get('list') - async readEntire() { - return await DefaultExecutor.bootstrap(this.readEntireProcess); + async list() { + return await DefaultExecutor.bootstrap(this.listProcess); + } + + @Get() + async pagination() { + return await DefaultExecutor.bootstrap(this.paginationProcess); + } + + @Get(':id') + async read(@Param('id') id) { + return await DefaultExecutor.bootstrap(this.readProcess); + } + + @Patch() + async update() { + return await DefaultExecutor.bootstrap(this.updateProcess); } }