mirror of
https://github.com/aditama-labs/nest-autocrud.git
synced 2024-11-21 19:06:21 +00:00
feat: full working simple crud with id as unique identifier
This commit is contained in:
parent
87f951fe08
commit
d4ce99975d
@ -5,18 +5,16 @@ export class PrismaCreateProcess
|
||||
extends PrismaProcess
|
||||
implements CreateProcess
|
||||
{
|
||||
private dataInsert;
|
||||
private dataResult;
|
||||
|
||||
setData(data) {
|
||||
this.dataInsert = data;
|
||||
}
|
||||
public data;
|
||||
public result;
|
||||
|
||||
async process(): Promise<any> {
|
||||
this.dataResult = await this.getDelegate().create(this.dataInsert);
|
||||
this.result = await this.getDelegate().create({
|
||||
data: this.data,
|
||||
});
|
||||
}
|
||||
|
||||
output() {
|
||||
this.dataResult;
|
||||
this.result;
|
||||
}
|
||||
}
|
||||
|
@ -1,20 +1,20 @@
|
||||
import { CreateProcess } from '@aditama-labs/nest-autocrud/skeleton';
|
||||
import { DeleteProcess } from '@aditama-labs/nest-autocrud/skeleton';
|
||||
import { PrismaProcess } from './prisma.process';
|
||||
|
||||
export class PrismaDeleteProcess
|
||||
extends PrismaProcess
|
||||
implements CreateProcess
|
||||
implements DeleteProcess
|
||||
{
|
||||
private dataResult: any;
|
||||
private id: any;
|
||||
public id: any;
|
||||
private result: any;
|
||||
|
||||
async process(): Promise<any> {
|
||||
this.dataResult = await this.getDelegate().delete({
|
||||
this.result = await this.getDelegate().delete({
|
||||
where: { id: this.id },
|
||||
});
|
||||
}
|
||||
|
||||
output() {
|
||||
return this.dataResult;
|
||||
return this.result;
|
||||
}
|
||||
}
|
||||
|
@ -2,13 +2,13 @@ import { ListProcess } from '@aditama-labs/nest-autocrud/skeleton';
|
||||
import { PrismaProcess } from './prisma.process';
|
||||
|
||||
export class PrismaListProcess extends PrismaProcess implements ListProcess {
|
||||
private data: any;
|
||||
private result;
|
||||
|
||||
async process(): Promise<any> {
|
||||
this.data = await this.getDelegate().findMany();
|
||||
this.result = await this.getDelegate().findMany();
|
||||
}
|
||||
|
||||
output() {
|
||||
return this.data;
|
||||
return this.result;
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,25 @@
|
||||
import { PaginationProcess } from '@aditama-labs/nest-autocrud/skeleton';
|
||||
import { PrismaProcess } from './prisma.process';
|
||||
|
||||
export class PrismaPaginationProcess extends PaginationProcess {}
|
||||
export class PrismaPaginationProcess
|
||||
extends PrismaProcess
|
||||
implements PaginationProcess
|
||||
{
|
||||
public params: { page: number; limit: number };
|
||||
private result;
|
||||
|
||||
async process(): Promise<any> {
|
||||
const { page, limit } = this.params;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
this.result = await this.getDelegate().findMany({
|
||||
skip,
|
||||
// @TODO: I don't know why this actually string even the type is number, without parseInt it will throw error
|
||||
take: parseInt(limit.toString(), 10),
|
||||
});
|
||||
}
|
||||
|
||||
output() {
|
||||
return this.result;
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,17 @@
|
||||
import { CreateProcess } from '@aditama-labs/nest-autocrud/skeleton';
|
||||
import { ReadProcess } from '@aditama-labs/nest-autocrud/skeleton';
|
||||
import { PrismaProcess } from './prisma.process';
|
||||
|
||||
export class PrismaReadProcess extends PrismaProcess implements CreateProcess {
|
||||
private data: any;
|
||||
private id: any;
|
||||
export class PrismaReadProcess extends PrismaProcess implements ReadProcess {
|
||||
public id;
|
||||
private result;
|
||||
|
||||
async process(): Promise<any> {
|
||||
this.data = await this.getDelegate().findUnique({
|
||||
this.result = await this.getDelegate().findUnique({
|
||||
where: { id: this.id },
|
||||
});
|
||||
}
|
||||
|
||||
output() {
|
||||
return this.data;
|
||||
return this.result;
|
||||
}
|
||||
}
|
||||
|
@ -1,22 +1,22 @@
|
||||
import { CreateProcess } from '@aditama-labs/nest-autocrud/skeleton';
|
||||
import { UpdateProcess } from '@aditama-labs/nest-autocrud/skeleton';
|
||||
import { PrismaProcess } from './prisma.process';
|
||||
|
||||
export class PrismaUpdateProcess
|
||||
extends PrismaProcess
|
||||
implements CreateProcess
|
||||
implements UpdateProcess
|
||||
{
|
||||
private dataResult: any;
|
||||
private dataUpdate: any;
|
||||
private id: any;
|
||||
public id: any;
|
||||
public data: any;
|
||||
private result: any;
|
||||
|
||||
async process(): Promise<any> {
|
||||
this.dataResult = await this.getDelegate().update({
|
||||
data: this.dataUpdate,
|
||||
this.result = await this.getDelegate().update({
|
||||
data: this.data,
|
||||
where: { id: this.id },
|
||||
});
|
||||
}
|
||||
|
||||
output() {
|
||||
return this.dataResult;
|
||||
return this.result;
|
||||
}
|
||||
}
|
||||
|
16
libs/skeleton/src/executors/create.executor.ts
Normal file
16
libs/skeleton/src/executors/create.executor.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { CreateProcess } from '../processes';
|
||||
import { DefaultExecutor } from './default.executor';
|
||||
|
||||
export class CreateExcutor extends DefaultExecutor {
|
||||
constructor(process: CreateProcess, data) {
|
||||
super(process);
|
||||
// Set data to process
|
||||
process.data = data;
|
||||
}
|
||||
|
||||
static async bootstrap(process: CreateProcess, data): Promise<any> {
|
||||
const executor = new CreateExcutor(process, data);
|
||||
await executor.execute();
|
||||
return executor.getOutput();
|
||||
}
|
||||
}
|
@ -15,10 +15,4 @@ export class DefaultExecutor {
|
||||
getOutput(): any {
|
||||
return this.process.output();
|
||||
}
|
||||
|
||||
static async bootstrap(process: ISkeletonProcess): Promise<any> {
|
||||
const executor = new DefaultExecutor(process);
|
||||
await executor.execute();
|
||||
return executor.getOutput();
|
||||
}
|
||||
}
|
||||
|
17
libs/skeleton/src/executors/delete.executor.ts
Normal file
17
libs/skeleton/src/executors/delete.executor.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { DeleteProcess } from '../processes';
|
||||
import { DefaultExecutor } from './default.executor';
|
||||
|
||||
// @TODO: ReadExecutor and DeleteExecutor can be merged into one executor
|
||||
export class DeleteExecutor extends DefaultExecutor {
|
||||
constructor(process: DeleteProcess, id) {
|
||||
super(process);
|
||||
// Set the id of the data
|
||||
process.id = id;
|
||||
}
|
||||
|
||||
static async bootstrap(process: DeleteProcess, id): Promise<any> {
|
||||
const executor = new DeleteExecutor(process, id);
|
||||
await executor.execute();
|
||||
return executor.getOutput();
|
||||
}
|
||||
}
|
@ -1 +1,7 @@
|
||||
export * from './create.executor';
|
||||
export * from './default.executor';
|
||||
export * from './delete.executor';
|
||||
export * from './list.executor';
|
||||
export * from './pagination.executor';
|
||||
export * from './read.executor';
|
||||
export * from './update.executor';
|
||||
|
14
libs/skeleton/src/executors/list.executor.ts
Normal file
14
libs/skeleton/src/executors/list.executor.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { DefaultExecutor } from '.';
|
||||
import { ListProcess } from '../processes';
|
||||
|
||||
export class ListExecutor extends DefaultExecutor {
|
||||
constructor(process: ListProcess) {
|
||||
super(process);
|
||||
}
|
||||
|
||||
static async bootstrap(process: ListProcess): Promise<any> {
|
||||
const executor = new ListExecutor(process);
|
||||
await executor.execute();
|
||||
return executor.getOutput();
|
||||
}
|
||||
}
|
22
libs/skeleton/src/executors/pagination.executor.ts
Normal file
22
libs/skeleton/src/executors/pagination.executor.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { DefaultExecutor } from '.';
|
||||
import { PaginationProcess } from '../processes';
|
||||
|
||||
export class PaginationExecutor extends DefaultExecutor {
|
||||
constructor(
|
||||
process: PaginationProcess,
|
||||
params: { page: number; limit: number },
|
||||
) {
|
||||
super(process);
|
||||
// Set params to process
|
||||
process.params = params;
|
||||
}
|
||||
|
||||
static async bootstrap(
|
||||
process: PaginationProcess,
|
||||
params: { page: number; limit: number },
|
||||
): Promise<any> {
|
||||
const executor = new PaginationExecutor(process, params);
|
||||
await executor.execute();
|
||||
return executor.getOutput();
|
||||
}
|
||||
}
|
16
libs/skeleton/src/executors/read.executor.ts
Normal file
16
libs/skeleton/src/executors/read.executor.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { ReadProcess } from '../processes';
|
||||
import { DefaultExecutor } from './default.executor';
|
||||
|
||||
export class ReadExecutor extends DefaultExecutor {
|
||||
constructor(process: ReadProcess, id) {
|
||||
super(process);
|
||||
// Set the id of the data
|
||||
process.id = id;
|
||||
}
|
||||
|
||||
static async bootstrap(process: ReadProcess, id): Promise<any> {
|
||||
const executor = new ReadExecutor(process, id);
|
||||
await executor.execute();
|
||||
return executor.getOutput();
|
||||
}
|
||||
}
|
18
libs/skeleton/src/executors/update.executor.ts
Normal file
18
libs/skeleton/src/executors/update.executor.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { UpdateProcess } from '../processes';
|
||||
import { DefaultExecutor } from './default.executor';
|
||||
|
||||
// @TODO: This executor should be able to extend from ReadExecutor and CreateExecutor
|
||||
export class UpdateExecutor extends DefaultExecutor {
|
||||
constructor(process: UpdateProcess, id, data) {
|
||||
super(process);
|
||||
// Set the id and data to process
|
||||
process.id = id;
|
||||
process.data = data;
|
||||
}
|
||||
|
||||
static async bootstrap(process: UpdateProcess, id, data): Promise<any> {
|
||||
const executor = new UpdateExecutor(process, id, data);
|
||||
await executor.execute();
|
||||
return executor.getOutput();
|
||||
}
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
import { DefaultProcess } from './default.process';
|
||||
|
||||
export class CreateProcess extends DefaultProcess {}
|
||||
export class CreateProcess extends DefaultProcess {
|
||||
public data;
|
||||
}
|
||||
|
@ -1,3 +1,6 @@
|
||||
import { DefaultProcess } from './default.process';
|
||||
|
||||
export class DeleteProcess extends DefaultProcess {}
|
||||
// @TODO: DeleteProcess and ReadProcess are the same, should be refactored next
|
||||
export class DeleteProcess extends DefaultProcess {
|
||||
public id;
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
import { DefaultProcess } from './default.process';
|
||||
|
||||
export class PaginationProcess extends DefaultProcess {}
|
||||
export class PaginationProcess extends DefaultProcess {
|
||||
params: { page: number; limit: number };
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
import { DefaultProcess } from './default.process';
|
||||
|
||||
export class ReadProcess extends DefaultProcess {}
|
||||
export class ReadProcess extends DefaultProcess {
|
||||
public id;
|
||||
}
|
||||
|
@ -1,3 +1,8 @@
|
||||
import { DefaultProcess } from './default.process';
|
||||
|
||||
export class UpdateProcess extends DefaultProcess {}
|
||||
export class UpdateProcess extends DefaultProcess {
|
||||
// @TODO: The property of id can be take from ReadProcess which is extended
|
||||
public id;
|
||||
// @TODO: The property of data can be take from CreateProcess which is extended
|
||||
public data;
|
||||
}
|
||||
|
@ -16,8 +16,14 @@ import {
|
||||
READ_PROCESS,
|
||||
UPDATE_PROCESS,
|
||||
} from './constants';
|
||||
import { CreateExcutor } from './executors/create.executor';
|
||||
import { DefaultExecutor } from './executors/default.executor';
|
||||
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 { ISkeletonCRUDController } from './interfaces/controller/skeleton-crud.controller.interface';
|
||||
import { PaginationExecutor } from './executors';
|
||||
|
||||
export class SkeletonCRUDController implements ISkeletonCRUDController {
|
||||
constructor(
|
||||
@ -37,33 +43,33 @@ export class SkeletonCRUDController implements ISkeletonCRUDController {
|
||||
|
||||
@Post()
|
||||
async create(@Body() body): Promise<any> {
|
||||
return await DefaultExecutor.bootstrap(this.createProcess);
|
||||
return await CreateExcutor.bootstrap(this.createProcess, body);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
async delete(@Param('id') id) {
|
||||
return await DefaultExecutor.bootstrap(this.deleteProcess);
|
||||
return await DeleteExecutor.bootstrap(this.deleteProcess, id);
|
||||
}
|
||||
|
||||
@Get('list')
|
||||
async list() {
|
||||
return await DefaultExecutor.bootstrap(this.listProcess);
|
||||
return await ListExecutor.bootstrap(this.listProcess);
|
||||
}
|
||||
|
||||
@Get()
|
||||
async pagination(
|
||||
@Query() params: { page?: number; limit?: number } = { page: 1, limit: 10 },
|
||||
@Query() params: { page: number; limit: number } = { page: 1, limit: 10 },
|
||||
) {
|
||||
return await DefaultExecutor.bootstrap(this.paginationProcess);
|
||||
return await PaginationExecutor.bootstrap(this.paginationProcess, params);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async read(@Param('id') id) {
|
||||
return await DefaultExecutor.bootstrap(this.readProcess);
|
||||
return await ReadExecutor.bootstrap(this.readProcess, id);
|
||||
}
|
||||
|
||||
@Patch()
|
||||
@Patch(':id')
|
||||
async update(@Param('id') id, @Body() body) {
|
||||
return await DefaultExecutor.bootstrap(this.updateProcess);
|
||||
return await UpdateExecutor.bootstrap(this.updateProcess, id, body);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user