From 24b55b03a1484899510733f825b8ac7dd762c38a Mon Sep 17 00:00:00 2001 From: ali aslani Date: Fri, 30 Dec 2022 21:54:58 +0330 Subject: [PATCH] Fix: Add Excel Too Crud Generator --- README.md | 4 +-- rich/commander/commander.ts | 34 +++++++++++++------ rich/modules/common/Dtos/common.search.dto.ts | 3 ++ src/app.module.ts | 8 ++--- tsconfig.json | 1 + 5 files changed, 33 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 8b9742b..de469a4 100644 --- a/README.md +++ b/README.md @@ -13,11 +13,11 @@ 10. validate permission (no match, wrong structure) *DONE 11. model generator (crud gnerator) *DONE 12. refolder *DONE +13. add excel too Crud Generator *DONE -13. seed 14. update role's permission 15. notification 16. socket (emqx) (for backup service) 17. name the routes -18. add excel too Crud Generator +18. seed 19. add ELASTIC \ No newline at end of file diff --git a/rich/commander/commander.ts b/rich/commander/commander.ts index 90abed8..8c63232 100644 --- a/rich/commander/commander.ts +++ b/rich/commander/commander.ts @@ -1,6 +1,7 @@ import { Module } from '@nestjs/common'; import { Command, CommandFactory, CommandRunner, Option, } from 'nest-commander'; import * as fs from "fs" +import { resolve } from 'path'; interface CrudCommandOptions { permission?: boolean; @@ -38,7 +39,7 @@ export class CrudCommand extends CommandRunner { const ModuleName = this.generateModuleName(name) - const path = __dirname + "/src/" + ModuleName + const path = resolve(__dirname + "../../../src/" + ModuleName) if (fs.existsSync(path)) { throw new Error('Directory exists!') @@ -193,7 +194,8 @@ export class CrudCommand extends CommandRunner { Body, Post, Put, - Delete + Delete, + Response } from '@nestjs/common'; import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'; import { AuthGuard } from '@rich'; @@ -203,7 +205,7 @@ export class CrudCommand extends CommandRunner { import { ${ControllerName}ShowDto } from './dto/${name}.show.dto'; import { ${ControllerName}SearchDto } from './dto/${name}.search.dto'; import { ${ControllerName}Service } from './${name}.service'; - ${noPermission ? "import { HasPermission } from 'src/permission/permission.decorator';" : ''} + ${!noPermission ? "import { HasPermission } from '@rich';" : ''} @ApiBearerAuth("access-token") @UseGuards(AuthGuard) @@ -215,31 +217,43 @@ export class CrudCommand extends CommandRunner { constructor (private ${name}Service: ${ControllerName}Service) { } - ${noPermission ? '@HasPermission("public")' : ''} + ${!noPermission ? '@HasPermission("public")' : ''} @Get("search") - async search(@Query() payload: ${ControllerName}SearchDto) { - return await this.${name}Service.search(payload) + async search(@Query() payload: ${ControllerName}SearchDto, @Response() res) { + let data = await this.${name}Service.search({ ...payload, ...(payload.getExcel && { sheetName: "${name}" }) }) + + if (payload.getExcel) { + res.header( + "Content-type", + "application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + ) + .header("Content-Disposition", 'Content-Disposition: attachment; filename="${name}.xls"') + .send(data); + } + + + res.send(data) } - ${noPermission ? '@HasPermission("public")' : ''} + ${!noPermission ? '@HasPermission("public")' : ''} @Get("show/:id") async show(@Query() payload: ${ControllerName}ShowDto) { return await this.${name}Service.findOne(payload) } - ${noPermission ? '@HasPermission("public")' : ''} + ${!noPermission ? '@HasPermission("public")' : ''} @Post("create") async create(@Body() payload: ${ControllerName}CreateDto) { return await this.${name}Service.create(payload) } - ${noPermission ? '@HasPermission("public")' : ''} + ${!noPermission ? '@HasPermission("public")' : ''} @Put("update/:id") async update(@Param() param: ${ControllerName}UpdateParamsDto, @Body() payload: ${ControllerName}UpdatePayloadDto) { return await this.${name}Service.update(param, payload) } - ${noPermission ? '@HasPermission("public")' : ''} + ${!noPermission ? '@HasPermission("public")' : ''} @Delete("delete/:id") async delete(@Param() payload: ${ControllerName}DeleteDto) { return await this.${name}Service.delete(payload) diff --git a/rich/modules/common/Dtos/common.search.dto.ts b/rich/modules/common/Dtos/common.search.dto.ts index ff7b381..fd19c6c 100644 --- a/rich/modules/common/Dtos/common.search.dto.ts +++ b/rich/modules/common/Dtos/common.search.dto.ts @@ -9,4 +9,7 @@ export class CommonSearchDto { @ApiProperty() size: number + @ApiProperty({ required: false }) + getExcel: boolean + } \ No newline at end of file diff --git a/src/app.module.ts b/src/app.module.ts index ffb9b51..ff69a3d 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -14,10 +14,8 @@ import { RichImports, RichModule, RichProviders } from '@rich'; database: process.env.DB_NAME, autoLoadEntities: process.env.DB_AUTO_LOAD_ENTITIES, synchronize: true, - }) - ], - providers: [ - ...RichProviders, + }), ], + providers: [...RichProviders], }) -export class AppModule extends RichModule { } +export class AppModule extends RichModule {} diff --git a/tsconfig.json b/tsconfig.json index e037f82..56df9d7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,6 +11,7 @@ "outDir": "./dist", "baseUrl": "./", "paths": { + "src/*": ["./src/*"], "@rich": ["./rich"], }, "incremental": true,