company.controller.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 { CompanyService } from '../../service/users/company.service';
  8. import { CVO_company, FVO_company, QVO_company, UVAO_company } from '../../interface/users/company.interface';
  9. import { Context } from '@midwayjs/koa';
  10. import { ServiceUtilService } from '../../service/serviceUtil.service';
  11. const namePrefix = '企业';
  12. @ApiTags(['企业'])
  13. @Controller('/company', { tagName: namePrefix })
  14. export class CompanyController implements BaseController {
  15. @Inject()
  16. service: CompanyService;
  17. @Inject()
  18. ctx: Context;
  19. @Inject()
  20. serviceUtil: ServiceUtilService;
  21. @Get('/')
  22. @ApiTags('列表查询')
  23. @ApiQuery({ name: 'query' })
  24. @ApiResponse({ type: QVO_company })
  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_company })
  34. async fetch(@Param('id') id: number) {
  35. const data = await this.service.fetch({ id });
  36. const result = new FVO_company(data);
  37. return result;
  38. }
  39. @Post('/', { routerName: `创建${namePrefix}` })
  40. @ApiTags('创建数据')
  41. @Validate()
  42. @ApiResponse({ type: CVO_company })
  43. async create(@Body() data: object) {
  44. const dbData = await this.service.create(data);
  45. await this.serviceUtil.fillIdentity(data, 'Company');
  46. const result = new CVO_company(dbData);
  47. return result;
  48. }
  49. @Post('/:id', { routerName: `修改${namePrefix}` })
  50. @ApiTags('修改数据')
  51. @Validate()
  52. @ApiResponse({ type: UVAO_company })
  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. await this.serviceUtil.fillIdentity(data, 'Company');
  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('/list')
  68. async list(@Query() query: object) {
  69. const qobj = omit(query, ['skip', 'limit']);
  70. const others = pick(query, ['skip', 'limit']);
  71. const { data, total } = await this.service.query(qobj, others);
  72. for (let i of data) {
  73. i = await this.serviceUtil.fillOnwer(i);
  74. }
  75. return { data, total };
  76. }
  77. @Get('/contact')
  78. @ApiTags('列表查询')
  79. @ApiQuery({ name: 'query' })
  80. @ApiResponse({ type: QVO_company })
  81. async contact(@Query() query: object) {
  82. const qobj = omit(query, ['skip', 'limit']);
  83. const others = pick(query, ['skip', 'limit']);
  84. const { data, total } = await this.service.query(qobj, others);
  85. const list = await this.service.fillMatch(data);
  86. return { data: list, total };
  87. }
  88. @Get('/detail/:id')
  89. @ApiResponse({ type: FVO_company })
  90. async detail(@Param('id') id: string) {
  91. let data = await this.service.fetch({ id });
  92. data = await this.serviceUtil.fillOnwer(data);
  93. data = await this.serviceUtil.fillCollection(data, 'company');
  94. return data;
  95. }
  96. }