incubator.controller.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { Body, Controller, Del, Get, Inject, Param, Post, Query } from '@midwayjs/core';
  2. import { ApiResponse, ApiTags, ApiQuery } from '@midwayjs/swagger';
  3. import { Validate } from '@midwayjs/validate';
  4. import { BaseController } from '../../frame/BaseController';
  5. import { omit, pick } from 'lodash';
  6. import { ErrorCode, ServiceError } from '../../error/service.error';
  7. import { Context } from '@midwayjs/koa';
  8. import { IncubatorService } from '../../service/users/incubator.service';
  9. import { QVO_incubator, FVO_incubator, CVO_incubator, UVAO_incubator } from '../../interface/users/incubator.interface';
  10. import { ServiceUtilService } from '../../service/serviceUtil.service';
  11. const namePrefix = '孵化基地';
  12. @ApiTags(['孵化基地'])
  13. @Controller('/incubator', { tagName: namePrefix })
  14. export class IncubatorController implements BaseController {
  15. @Inject()
  16. service: IncubatorService;
  17. @Inject()
  18. ctx: Context;
  19. @Inject()
  20. serviceUtil: ServiceUtilService;
  21. @Get('/')
  22. @ApiTags('列表查询')
  23. @ApiQuery({ name: 'query' })
  24. @ApiResponse({ type: QVO_incubator })
  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_incubator })
  34. async fetch(@Param('id') id: number) {
  35. const data = await this.service.fetch({ id });
  36. const result = new FVO_incubator(data);
  37. return result;
  38. }
  39. @Post('/', { routerName: `创建${namePrefix}` })
  40. @ApiTags('创建数据')
  41. @Validate()
  42. @ApiResponse({ type: CVO_incubator })
  43. async create(@Body() data: object) {
  44. const dbData = await this.service.create(data);
  45. this.serviceUtil.fillIdentity(data, 'Incubator');
  46. const result = new CVO_incubator(dbData);
  47. return result;
  48. }
  49. @Post('/:id', { routerName: `修改${namePrefix}` })
  50. @ApiTags('修改数据')
  51. @Validate()
  52. @ApiResponse({ type: UVAO_incubator })
  53. async update(@Param('id') id: number, @Body() data: object) {
  54. if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
  55. const result = await this.service.update({ id }, data);
  56. this.serviceUtil.fillIdentity(data, 'Incubator');
  57. return result;
  58. }
  59. @Del('/:id', { routerName: `删除${namePrefix}` })
  60. @ApiTags('删除数据')
  61. @Validate()
  62. async delete(@Param('id') id: number) {
  63. if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
  64. const result = await this.service.delete({ id });
  65. return result;
  66. }
  67. @Get('/detail/:id')
  68. @ApiResponse({ type: FVO_incubator })
  69. async detail(@Param('id') id: string) {
  70. let data = await this.service.fetch({ id });
  71. data = await this.serviceUtil.fillOnwer(data);
  72. data = await this.serviceUtil.fillCollection(data, 'incubator');
  73. return data;
  74. }
  75. }