supply.controller.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { Controller, Inject, Get, Param, Post, Body, Del, Query } from '@midwayjs/core';
  2. import { SupplyService } from '../../service/platform/supply.service';
  3. import { ApiResponse, ApiTags, ApiQuery } from '@midwayjs/swagger';
  4. import { Validate } from '@midwayjs/validate';
  5. import { omit, pick } from 'lodash';
  6. import { ServiceError, ErrorCode } from '../../error/service.error';
  7. import { BaseController } from '../../frame/BaseController';
  8. import { QVO_supply, FVO_supply, CVO_supply, UVAO_supply } from '../../interface/platform/supply.interface';
  9. import { Context } from '@midwayjs/koa';
  10. const namePrefix = '供给';
  11. @ApiTags(['供给'])
  12. @Controller('/supply', { tagName: namePrefix })
  13. export class SupplyController implements BaseController {
  14. @Inject()
  15. service: SupplyService;
  16. @Inject()
  17. ctx: Context;
  18. @Get('/')
  19. @ApiTags('列表查询')
  20. @ApiQuery({ name: 'query' })
  21. @ApiResponse({ type: QVO_supply })
  22. async index(@Query() query: object) {
  23. const qobj = omit(query, ['skip', 'limit']);
  24. const others = pick(query, ['skip', 'limit']);
  25. const qbr = this.service.queryBuilder(qobj);
  26. const result = await this.service.query(qbr, others);
  27. return result;
  28. }
  29. @Get('/:id')
  30. @ApiTags('单查询')
  31. @ApiResponse({ type: FVO_supply })
  32. async fetch(@Param('id') id: number) {
  33. const qbr = this.service.queryBuilder({ id });
  34. const data = await this.service.fetch(qbr);
  35. const result = new FVO_supply(data);
  36. return result;
  37. }
  38. @Post('/', { routerName: `创建${namePrefix}` })
  39. @ApiTags('创建数据')
  40. @Validate()
  41. @ApiResponse({ type: CVO_supply })
  42. async create(@Body() data: object) {
  43. const dbData = await this.service.create(data);
  44. const result = new CVO_supply(dbData);
  45. return result;
  46. }
  47. @Post('/:id', { routerName: `修改${namePrefix}` })
  48. @ApiTags('修改数据')
  49. @Validate()
  50. @ApiResponse({ type: UVAO_supply })
  51. async update(@Param('id') id: number, @Body() data: object) {
  52. if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
  53. const result = await this.service.update({ id }, data);
  54. return result;
  55. }
  56. @Del('/:id', { routerName: `删除${namePrefix}` })
  57. @ApiTags('删除数据')
  58. @Validate()
  59. async delete(@Param('id') id: number) {
  60. if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
  61. const result = await this.service.delete({ id });
  62. return result;
  63. }
  64. @Get('/list')
  65. async list() {
  66. const list = await this.service.list(this.ctx.filter);
  67. const data = list.data;
  68. const total = list.total;
  69. return { data, total };
  70. }
  71. @Get('/detail/:id')
  72. @ApiResponse({ type: FVO_supply })
  73. async detail(@Param('id') id: string) {
  74. const data = await this.service.detail(id);
  75. return data;
  76. }
  77. }