From 6d7e0729656875ab299192faf861ea3c44c601ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=95=8A=E5=B9=B3?= <951984189@qq.com> Date: Fri, 12 Mar 2021 16:46:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=95=86=E5=93=81demo?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/demo/controller/admin/goods.ts | 63 +++++++++++++++++++ src/app/modules/demo/controller/app/goods.ts | 12 +--- src/app/modules/demo/entity/goods.ts | 6 ++ 3 files changed, 71 insertions(+), 10 deletions(-) create mode 100644 src/app/modules/demo/controller/admin/goods.ts diff --git a/src/app/modules/demo/controller/admin/goods.ts b/src/app/modules/demo/controller/admin/goods.ts new file mode 100644 index 0000000..a6e4941 --- /dev/null +++ b/src/app/modules/demo/controller/admin/goods.ts @@ -0,0 +1,63 @@ +import { Get, Provide } from '@midwayjs/decorator'; +import { Context } from 'egg'; +import { CoolController, BaseController } from 'midwayjs-cool-core'; +import { BaseSysUserEntity } from '../../../base/entity/sys/user'; +import { DemoAppGoodsEntity } from '../../entity/goods'; + +/** + * 商品 + */ +@Provide() +@CoolController({ + // 添加通用CRUD接口 + api: ['add', 'delete', 'update', 'info', 'list', 'page'], + // 设置表实体 + entity: DemoAppGoodsEntity, + // 向表插入当前登录用户ID + insertParam: async (ctx: Context) => { + return { + userId: ctx.admin.userId, + }; + }, + // info接口忽略价格字段 + infoIgnoreProperty: ['price'], + // 分页查询配置 + pageQueryOp: { + // 让title字段支持模糊查询 + keyWordLikeFields: ['title'], + // 让type字段支持筛选 + fieldEq: ['type'], + // 指定返回字段 + select: ['a.*', 'b.name'], + // 关联表用户表 + leftJoin: [{ + // 管理的表 + entity: BaseSysUserEntity, + // 别名 + alias: 'b', + // 关联条件 + condition: 'a.userId = b.id' + }], + // 增加其他条件 + where: async (ctx: Context) => { + return [ + // 价格大于90 + ['a.price > :price', { price: 90.00 }] + ] + }, + // 添加排序 + addOrderBy: { + // 排序字段及排序方式 + price: 'desc' + } + } +}) +export class DemoAdminGoodsController extends BaseController { + /** + * 其他接口 + */ + @Get('/other') + async other() { + return this.ok('hello, cool-admin!!!'); + } +} diff --git a/src/app/modules/demo/controller/app/goods.ts b/src/app/modules/demo/controller/app/goods.ts index 722da7a..8290352 100644 --- a/src/app/modules/demo/controller/app/goods.ts +++ b/src/app/modules/demo/controller/app/goods.ts @@ -1,4 +1,4 @@ -import { Get, Provide } from '@midwayjs/decorator'; +import { Provide } from '@midwayjs/decorator'; import { CoolController, BaseController } from 'midwayjs-cool-core'; import { DemoAppGoodsEntity } from '../../entity/goods'; @@ -10,12 +10,4 @@ import { DemoAppGoodsEntity } from '../../entity/goods'; api: ['add', 'delete', 'update', 'info', 'list', 'page'], entity: DemoAppGoodsEntity, }) -export class DemoAppGoodsController extends BaseController { - /** - * 其他接口 - */ - @Get('/other') - async other() { - return this.ok('hello, cool-admin!!!'); - } -} +export class DemoAppGoodsController extends BaseController {} diff --git a/src/app/modules/demo/entity/goods.ts b/src/app/modules/demo/entity/goods.ts index 41198dc..48bf61d 100644 --- a/src/app/modules/demo/entity/goods.ts +++ b/src/app/modules/demo/entity/goods.ts @@ -7,6 +7,9 @@ import { Column } from 'typeorm'; */ @EntityModel('demo_app_goods') export class DemoAppGoodsEntity extends BaseEntity { + @Column({ comment: '用户ID' }) + userId: number; + @Column({ comment: '标题' }) title: string; @@ -15,4 +18,7 @@ export class DemoAppGoodsEntity extends BaseEntity { @Column({ comment: '价格', type: 'decimal', precision: 5, scale: 2 }) price: number; + + @Column({ comment: '分类', type: 'tinyint' }) + type: number }