expert.controller.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 { UtilService } from '../../service/util.service';
  5. import { BaseController } from '../../frame/BaseController';
  6. import { get, omit, pick } from 'lodash';
  7. import { ErrorCode, ServiceError } from '../../error/service.error';
  8. import { ExpertService } from '../../service/users/expert.service';
  9. import { Context } from '@midwayjs/koa';
  10. import { QVO_expert, FVO_expert, CVO_expert, UVAO_expert } from '../../interface/users/expert.interface';
  11. import { ServiceUtilService } from '../../service/serviceUtil.service';
  12. const namePrefix = '专家';
  13. @ApiTags(['专家'])
  14. @Controller('/expert', { tagName: namePrefix })
  15. export class ExpertController implements BaseController {
  16. @Inject()
  17. service: ExpertService;
  18. @Inject()
  19. ctx: Context;
  20. @Inject()
  21. serviceUtil: ServiceUtilService;
  22. @Inject()
  23. utilService: UtilService;
  24. @Get('/')
  25. @ApiTags('列表查询')
  26. @ApiQuery({ name: 'query' })
  27. @ApiResponse({ type: QVO_expert })
  28. async index(@Query() query: object) {
  29. const qobj = omit(query, ['skip', 'limit']);
  30. const others = pick(query, ['skip', 'limit']);
  31. const result = await this.service.query(qobj, others);
  32. return result;
  33. }
  34. @Get('/:id')
  35. @ApiTags('单查询')
  36. @ApiResponse({ type: FVO_expert })
  37. async fetch(@Param('id') id: number) {
  38. const data = await this.service.fetch({ id });
  39. const result = new FVO_expert(data);
  40. return result;
  41. }
  42. @Post('/', { routerName: `创建${namePrefix}` })
  43. @ApiTags('创建数据')
  44. @Validate()
  45. @ApiResponse({ type: CVO_expert })
  46. async create(@Body() data: object) {
  47. const dbData = await this.service.create(data);
  48. const result = new CVO_expert(dbData);
  49. return result;
  50. }
  51. @Post('/:id', { routerName: `修改${namePrefix}` })
  52. @ApiTags('修改数据')
  53. @Validate()
  54. @ApiResponse({ type: UVAO_expert })
  55. async update(@Param('id') id: number, @Body() data: object) {
  56. if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
  57. const result = await this.service.update({ id }, data);
  58. await this.utilService.updateUserAfter(id, data, 'Association');
  59. return result;
  60. }
  61. @Del('/:id', { routerName: `删除${namePrefix}` })
  62. @ApiTags('删除数据')
  63. @Validate()
  64. async delete(@Param('id') id: number) {
  65. if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
  66. const result = await this.service.delete({ id });
  67. return result;
  68. }
  69. @Get('/list')
  70. async list(@Query() query: object) {
  71. const qobj = omit(query, ['skip', 'limit']);
  72. const others = pick(query, ['skip', 'limit']);
  73. const { data, total } = await this.service.query(qobj, others);
  74. for (const i of data) {
  75. await this.serviceUtil.fillOnwer(i);
  76. }
  77. return { data, total };
  78. }
  79. @Get('/detail/:id')
  80. @ApiResponse({ type: FVO_expert })
  81. async detail(@Param('id') id: string) {
  82. const data = await this.service.fetch({ id });
  83. await this.serviceUtil.fillOnwer(data);
  84. await this.serviceUtil.fillCollection(data);
  85. return data;
  86. }
  87. }