# 快速CURD

点击查看更多视频教程 (opens new window)

大部分的后台管理系统,或者API服务都是对数据进行管理,所以可以看到大量的CRUD场景(增删改查),cool-admin对此进行了大量地封装,让这块的编码量变得极其地少。

# 新建一个数据表

src/modules/demo/entity/goods.ts,项目启动数据库会自动创建该表,无需手动创建

import { EntityModel } from '@midwayjs/orm';
import { BaseEntity } from 'midwayjs-cool-core';
import { Column } from 'typeorm';

/**
 * 商品
 */
@EntityModel('demo_app_goods')
export class DemoAppGoodsEntity extends BaseEntity {

    @Column({ comment: '标题' })
    title: string;

    @Column({ comment: '图片' })
    pic: string;

    @Column({ comment: '价格', type: 'decimal', precision: 5, scale: 2 })
    price: number;

}

# 编写api接口

src/modules/demo/controller/app/goods.ts,快速编写6个api接口

import { Provide } from '@midwayjs/decorator';
import { CoolController, BaseController } from 'midwayjs-cool-core';
import { DemoAppGoodsEntity } from '../../entity/goods';

/**
 * 商品
 */
@Provide()
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: DemoAppGoodsEntity
})
export class DemoAppGoodsController extends BaseController {
  /**
   * 其他接口
   */
  @Get('/other')
  async other() {
    return this.ok('hello, cool-admin!!!');
  }
}

这样我们就完成了6个接口的编写,对应的接口如下:

  • POST /app/demo/goods/add 新增
  • POST /app/demo/goods/delete 删除
  • POST /app/demo/goods/update 更新
  • GET /app/demo/goods/info 单个信息
  • POST /app/demo/goods/list 列表信息
  • POST /app/demo/goods/page 分页查询(包含模糊查询、字段全匹配等)

# 接口调用

# 新增

POST /app/demo/goods/add 新增

# 请求

Url: http://localhost:8001/app/demo/goods/add

Method: POST

# Body

{
    "title":"商品标题",
    "pic":"https://show.cool-admin.com/uploads/20210311/2e393000-8226-11eb-abcf-fd7ae6caeb70.png",
    "price": 99.00
}

# 返回

{
    "code": 1000,
    "message": "success",
    "data": {
        "id": 2
    }
}

# 修改

POST /app/demo/goods/update 修改

# 请求

Url: http://localhost:8001/app/demo/goods/update

Method: POST

# Body

与add接口的区别是多传了id参数,后端根据id更新数据

{
    "id": 2,
    "title":"修改商品标题",
    "pic":"https://show.cool-admin.com/uploads/20210311/2e393000-8226-11eb-abcf-fd7ae6caeb70.png",
    "price": 99.00
}

# 返回

{
    "code": 1000,
    "message": "success",
    "data": {
        "id": 2
    }
}

# 删除

POST /app/demo/goods/delete 删除

# 请求

Url: http://localhost:8001/app/demo/goods/delete

Method: POST

# Body

{
    "ids":[1]
}

# 返回

{
    "code": 1000,
    "message": "success"
}

# 详情

GET /app/demo/goods/info 单个信息

# 请求

Url: http://localhost:8001/app/demo/goods/info

Method: GET

# Query Params

Param value
id 2

# 返回

{
    "code": 1000,
    "message": "success",
    "data": {
        "id": 2,
        "createTime": "2021-03-12 10:24:50",
        "updateTime": "2021-03-12 10:31:21",
        "title": "修改商品标题",
        "pic": "https://show.cool-admin.com/uploads/20210311/2e393000-8226-11eb-abcf-fd7ae6caeb70.png",
        "price": "99.00"
    }
}

# 列表

POST /app/demo/goods/list 所有信息

# 请求

Url: http://localhost:8001/app/demo/goods/list

Method: POST

# 返回

{
    "code": 1000,
    "message": "success",
    "data": [
        {
            "id": 2,
            "createTime": "2021-03-12 10:24:50",
            "updateTime": "2021-03-12 10:31:21",
            "title": "修改商品标题",
            "pic": "https://show.cool-admin.com/uploads/20210311/2e393000-8226-11eb-abcf-fd7ae6caeb70.png",
            "price": "99.00"
        }
    ]
}

# 分页

POST /app/demo/goods/page 分页数据

# 请求

Url: http://localhost:8001/app/demo/goods/page

Method: POST

# 返回

{
    "code": 1000,
    "message": "success",
    "data": {
        "list": [
            {
                "id": 2,
                "createTime": "2021-03-12 10:24:50",
                "updateTime": "2021-03-12 10:31:21",
                "title": "修改商品标题",
                "pic": "https://show.cool-admin.com/uploads/20210311/2e393000-8226-11eb-abcf-fd7ae6caeb70.png",
                "price": "99.00"
            }
        ],
        "pagination": {
            "page": 1,
            "size": 15,
            "total": 1
        }
    }
}