demand.controller.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { DemandService } from '../../service/platform/demand.service';
  2. import { CVO_demand, FVO_demand, QVO_demand, UVAO_demand } from '../../interface/platform/demand.interface';
  3. import { ApiResponse, ApiTags, ApiQuery } from '@midwayjs/swagger';
  4. import { Validate } from '@midwayjs/validate';
  5. import { BaseController } from '../../frame/BaseController';
  6. import { Controller, Inject, Get, Param, Post, Body, Del, Query } from '@midwayjs/core';
  7. import { omit, pick } from 'lodash';
  8. import { ServiceError, ErrorCode } from '../../error/service.error';
  9. import { Context } from '@midwayjs/koa';
  10. import { ServiceUtilService } from '../../service/serviceUtil.service';
  11. const namePrefix = '需求';
  12. @ApiTags(['需求'])
  13. @Controller('/demand', { tagName: namePrefix })
  14. export class DemandController implements BaseController {
  15. @Inject()
  16. service: DemandService;
  17. @Inject()
  18. ctx: Context;
  19. @Inject()
  20. serviceUtil: ServiceUtilService;
  21. @Get('/')
  22. @ApiTags('列表查询')
  23. @ApiQuery({ name: 'query' })
  24. @ApiResponse({ type: QVO_demand })
  25. async index(@Query() query: object) {
  26. const qobj = omit(query, ['skip', 'limit']);
  27. const others = pick(query, ['skip', 'limit']);
  28. const result = await this.service.query(qobj, others);
  29. return result;
  30. }
  31. @Get('/:id')
  32. @ApiTags('单查询')
  33. @ApiResponse({ type: FVO_demand })
  34. async fetch(@Param('id') id: number) {
  35. const data = await this.service.fetch({ id });
  36. const result = new FVO_demand(data);
  37. return result;
  38. }
  39. @Post('/', { routerName: `创建${namePrefix}` })
  40. @ApiTags('创建数据')
  41. @Validate()
  42. @ApiResponse({ type: CVO_demand })
  43. async create(@Body() data: object) {
  44. const dbData = await this.service.create(data);
  45. const result = new CVO_demand(dbData);
  46. return result;
  47. }
  48. @Post('/:id', { routerName: `修改${namePrefix}` })
  49. @ApiTags('修改数据')
  50. @Validate()
  51. @ApiResponse({ type: UVAO_demand })
  52. async update(@Param('id') id: number, @Body() data: object) {
  53. if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
  54. const result = await this.service.update({ id }, data);
  55. return result;
  56. }
  57. @Del('/:id', { routerName: `删除${namePrefix}` })
  58. @ApiTags('删除数据')
  59. @Validate()
  60. async delete(@Param('id') id: number) {
  61. if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
  62. const result = await this.service.delete({ id });
  63. return result;
  64. }
  65. @Get('/list')
  66. async list(@Query() query: object) {
  67. const qobj = omit(query, ['skip', 'limit']);
  68. const others = pick(query, ['skip', 'limit']);
  69. const { data, total } = await this.service.query(qobj, others, {
  70. field: this.service.Opera.In,
  71. name: this.service.Opera.Like,
  72. company: this.service.Opera.Like,
  73. industry: this.service.Opera.In,
  74. area: this.service.Opera.Json,
  75. tags: this.service.Opera.Json,
  76. });
  77. for (let i of data) {
  78. i = await this.serviceUtil.fillOnwer(i);
  79. }
  80. return { data, total };
  81. }
  82. @Get('/detail/:id')
  83. @ApiResponse({ type: FVO_demand })
  84. async detail(@Param('id') id: string) {
  85. let data = await this.service.fetch({ id });
  86. data = await this.serviceUtil.fillOnwer(data);
  87. data = await this.serviceUtil.fillCollection(data, 'demand');
  88. return data;
  89. }
  90. }