demand.controller.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. @ApiTags(['需求'])
  11. @Controller('/demand')
  12. export class DemandController implements BaseController {
  13. @Inject()
  14. service: DemandService;
  15. @Inject()
  16. ctx: Context;
  17. @Get('/')
  18. @ApiTags('列表查询')
  19. @ApiQuery({ name: 'query' })
  20. @ApiResponse({ type: QVO_demand })
  21. async index(@Query() query: object) {
  22. const qobj = omit(query, ['skip', 'limit']);
  23. const others = pick(query, ['skip', 'limit']);
  24. const qbr = this.service.queryBuilder(qobj);
  25. const result = await this.service.query(qbr, others);
  26. return result;
  27. }
  28. @Get('/:id')
  29. @ApiTags('单查询')
  30. @ApiResponse({ type: FVO_demand })
  31. async fetch(@Param('id') id: number) {
  32. const qbr = this.service.queryBuilder({ id });
  33. const data = await this.service.fetch(qbr);
  34. const result = new FVO_demand(data);
  35. return result;
  36. }
  37. @Post('/')
  38. @ApiTags('创建数据')
  39. @Validate()
  40. @ApiResponse({ type: CVO_demand })
  41. async create(@Body() data: object) {
  42. const dbData = await this.service.create(data);
  43. const result = new CVO_demand(dbData);
  44. return result;
  45. }
  46. @Post('/:id')
  47. @ApiTags('修改数据')
  48. @Validate()
  49. @ApiResponse({ type: UVAO_demand })
  50. async update(@Param('id') id: number, @Body() data: object) {
  51. if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
  52. const result = await this.service.update({ id }, data);
  53. return result;
  54. }
  55. @Del('/:id')
  56. @ApiTags('删除数据')
  57. @Validate()
  58. async delete(@Param('id') id: number) {
  59. if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
  60. const result = await this.service.delete({ id });
  61. return result;
  62. }
  63. @Get('/list')
  64. async list() {
  65. const list = await this.service.list(this.ctx.filter);
  66. const data = list.data;
  67. const total = list.total;
  68. return { data, total };
  69. }
  70. @Get('/detail/:id')
  71. @ApiResponse({ type: FVO_demand })
  72. async detail(@Param('id') id: string) {
  73. const data = await this.service.detail(id);
  74. return data;
  75. }
  76. }