Skip to content

Commit

Permalink
Fix: Add Excel Too Crud Generator
Browse files Browse the repository at this point in the history
  • Loading branch information
ialiaslani committed Dec 30, 2022
1 parent 5fb0a77 commit 24b55b0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 24 additions & 10 deletions rich/commander/commander.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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!')
Expand Down Expand Up @@ -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';
Expand All @@ -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)
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions rich/modules/common/Dtos/common.search.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ export class CommonSearchDto {
@ApiProperty()
size: number

@ApiProperty({ required: false })
getExcel: boolean

}
8 changes: 3 additions & 5 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"outDir": "./dist",
"baseUrl": "./",
"paths": {
"src/*": ["./src/*"],
"@rich": ["./rich"],
},
"incremental": true,
Expand Down

0 comments on commit 24b55b0

Please sign in to comment.