diff --git a/.eslintrc.js b/.eslintrc.js index 81168c9..cffd566 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -21,5 +21,6 @@ module.exports = { '@typescript-eslint/explicit-function-return-type': 'off', '@typescript-eslint/explicit-module-boundary-types': 'off', '@typescript-eslint/no-explicit-any': 'off', + 'prettier/prettier': ['error', { printWidth: 120, endOfLine: 'auto' }], }, }; diff --git a/.prettierrc b/.prettierrc index 26bd50f..6c1c390 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,5 +1,7 @@ { "singleQuote": true, "trailingComma": "all", - "endOfLine": "auto" + "endOfLine": "auto", + "printWidth": 120, + "bracketSpacing": true } \ No newline at end of file diff --git a/README.md b/README.md index d2e3189..398b0ab 100644 --- a/README.md +++ b/README.md @@ -1,85 +1 @@ -
- -[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456 -[circleci-url]: https://circleci.com/gh/nestjs/nest - -A progressive Node.js framework for building efficient and scalable server-side applications.
- - - -## Description - -[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository. - -## Project setup - -```bash -$ pnpm install -``` - -## Compile and run the project - -```bash -# development -$ pnpm run start - -# watch mode -$ pnpm run start:dev - -# production mode -$ pnpm run start:prod -``` - -## Run tests - -```bash -# unit tests -$ pnpm run test - -# e2e tests -$ pnpm run test:e2e - -# test coverage -$ pnpm run test:cov -``` - -## Resources - -Check out a few resources that may come in handy when working with NestJS: - -- Visit the [NestJS Documentation](https://docs.nestjs.com) to learn more about the framework. -- For questions and support, please visit our [Discord channel](https://discord.gg/G7Qnnhy). -- To dive deeper and get more hands-on experience, check out our official video [courses](https://courses.nestjs.com/). -- Visualize your application graph and interact with the NestJS application in real-time using [NestJS Devtools](https://devtools.nestjs.com). -- Need help with your project (part-time to full-time)? Check out our official [enterprise support](https://enterprise.nestjs.com). -- To stay in the loop and get updates, follow us on [X](https://x.com/nestframework) and [LinkedIn](https://linkedin.com/company/nestjs). -- Looking for a job, or have a job to offer? Check out our official [Jobs board](https://jobs.nestjs.com). - -## Support - -Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support). - -## Stay in touch - -- Author - [Kamil Myśliwiec](https://twitter.com/kammysliwiec) -- Website - [https://nestjs.com](https://nestjs.com/) -- Twitter - [@nestframework](https://twitter.com/nestframework) - -## License - -Nest is [MIT licensed](https://github.com/nestjs/nest/blob/master/LICENSE). +pm2 start --name admin-banban-new-nest npm -- run start:prod \ No newline at end of file diff --git a/aa.conf b/aa.conf new file mode 100644 index 0000000..e69de29 diff --git a/package.json b/package.json index 65a6e0f..ca44b1e 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "@nestjs/platform-express": "^10.0.0", "@prisma/client": "^5.19.1", "class-validator": "^0.14.1", + "lodash": "^4.17.21", "prisma": "^5.19.1", "reflect-metadata": "^0.2.0", "rxjs": "^7.8.1" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fbc0424..78bb618 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -23,6 +23,9 @@ importers: class-validator: specifier: ^0.14.1 version: 0.14.1 + lodash: + specifier: ^4.17.21 + version: 4.17.21 prisma: specifier: ^5.19.1 version: 5.19.1 diff --git a/src/app.module.ts b/src/app.module.ts index f2a708e..1a85134 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -2,10 +2,12 @@ import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { SystemCharterModule } from './modules/index'; +import { DBModule } from './utils/db/DB.module'; + @Module({ - imports: [SystemCharterModule], + imports: [DBModule, SystemCharterModule], controllers: [AppController], providers: [AppService], }) -export class AppModule {} +export class AppModule { } diff --git a/src/common/pagination/PaginationDto.dto.ts b/src/common/pagination/PaginationDto.dto.ts new file mode 100644 index 0000000..5c493a0 --- /dev/null +++ b/src/common/pagination/PaginationDto.dto.ts @@ -0,0 +1,12 @@ +import { IsNumber, IsOptional } from 'class-validator'; + +export class PaginationDto { + @IsNumber() + @IsOptional() + current: number; + + + @IsNumber() + @IsOptional() + pageSize: number; +} \ No newline at end of file diff --git a/src/common/pagination/index.ts b/src/common/pagination/index.ts new file mode 100644 index 0000000..3e3b903 --- /dev/null +++ b/src/common/pagination/index.ts @@ -0,0 +1,11 @@ +export class Pagination { + static getPage(current: any, pageSize?: any) { + if (current == undefined) { + current = '1'; + } + if (pageSize == undefined) { + pageSize = '10'; + } + return { skip: (parseInt(current) - 1) * parseInt(pageSize), take: parseInt(pageSize) }; + } +} diff --git a/src/modules/SystemCharter/SystemCharter.controller.ts b/src/modules/SystemCharter/SystemCharter.controller.ts index 6d1e795..d28f526 100644 --- a/src/modules/SystemCharter/SystemCharter.controller.ts +++ b/src/modules/SystemCharter/SystemCharter.controller.ts @@ -1,17 +1,55 @@ -import { Body, Controller, Post } from '@nestjs/common'; -import { $db } from 'src/utils/db'; +import { Body, Controller, Get, Post, Query } from '@nestjs/common'; import { SystemCharterlDto } from './dto/SystemCharter.dto'; +import { Pagination } from 'src/common/pagination'; +import { DBService } from 'src/utils/db/DB.service'; +import { ApiResponse } from 'src/utils/response/response'; @Controller('/system/charter') export class SystemCharterController { + constructor(private readonly dbService: DBService) { } + + + @Get('/getList') + async getList(@Query() query: SystemCharterlDto) { + const { current, pageSize, ...other } = query; + const where = {}; + if (query.roleName) { + where['roleName'] = other.roleName; + } + + const [record, total] = await this.dbService.systemCharter.findManyAndCount({ + where, + ...Pagination.getPage(current, pageSize), + orderBy: { + createdAt: 'desc', + }, + }); + return ApiResponse.success({ + data: record, + meta: { + total: total, + }, + }); + } + @Post('/add') async addSystemCharter(@Body() object: SystemCharterlDto) { - console.log('---', object); delete object.id; - const newRecord = await $db.systemCharter.create({ + const newRecord = await this.dbService.systemCharter.create({ data: object, }); - console.log(newRecord); + return newRecord; + } + + @Post('/update') + async updateSystemCharter(@Body() object: SystemCharterlDto) { + const { id, ...other } = object; + const newRecord = await this.dbService.systemCharter.update({ + where: { + id: id, + }, + data: other, + }); return newRecord; } } diff --git a/src/modules/SystemCharter/dto/SystemCharter.dto.ts b/src/modules/SystemCharter/dto/SystemCharter.dto.ts index b8267e1..0133355 100644 --- a/src/modules/SystemCharter/dto/SystemCharter.dto.ts +++ b/src/modules/SystemCharter/dto/SystemCharter.dto.ts @@ -1,6 +1,7 @@ import { IsOptional, IsString } from 'class-validator'; +import { PaginationDto } from 'src/common/pagination/PaginationDto.dto'; -export class SystemCharterlDto { +export class SystemCharterlDto extends PaginationDto { @IsString() @IsOptional() id: string; diff --git a/src/utils/db.ts b/src/utils/db.ts index 92dfbb8..82905c4 100644 --- a/src/utils/db.ts +++ b/src/utils/db.ts @@ -1,4 +1,4 @@ -import { PrismaClient } from '@prisma/client'; +import { PrismaClient, PrismaPromise } from '@prisma/client'; import { $logger } from './logger'; let $db: PrismaClient; @@ -14,4 +14,18 @@ const connection = async () => { } }; -export { $db, connection }; +/** + * 查询并统计结果,(用于分页) + * @param queryList + * @returns + */ +const findAndCount = async