association.controller.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { Body, Controller, Del, Get, Inject, Param, Post, Query } from '@midwayjs/core';
  2. import { AssociationService } from '../../service/users/association.service';
  3. import { CVO_association, FVO_association, QVO_association, UVAO_association } from '../../interface/users/association.interface';
  4. import { ApiResponse, ApiTags, ApiQuery } from '@midwayjs/swagger';
  5. import { Validate } from '@midwayjs/validate';
  6. import { UtilService } from '../../service/util.service';
  7. import { BaseController } from '../../frame/BaseController';
  8. import { omit, pick } from 'lodash';
  9. import { ErrorCode, ServiceError } from '../../error/service.error';
  10. const namePrefix = '商协会';
  11. @ApiTags(['商协会'])
  12. @Controller('/association', { tagName: namePrefix })
  13. export class AssociationController implements BaseController {
  14. @Inject()
  15. service: AssociationService;
  16. @Inject()
  17. utilService: UtilService;
  18. @Get('/')
  19. @ApiTags('列表查询')
  20. @ApiQuery({ name: 'query' })
  21. @ApiResponse({ type: QVO_association })
  22. async index(@Query() query: object) {
  23. const qobj = omit(query, ['skip', 'limit']);
  24. const others = pick(query, ['skip', 'limit']);
  25. const result = await this.service.query(qobj, others);
  26. return result;
  27. }
  28. @Get('/:id')
  29. @ApiTags('单查询')
  30. @ApiResponse({ type: FVO_association })
  31. async fetch(@Param('id') id: number) {
  32. const data = await this.service.fetch({ id });
  33. const result = new FVO_association(data);
  34. return result;
  35. }
  36. @Post('/', { routerName: `创建${namePrefix}` })
  37. @ApiTags('创建数据')
  38. @Validate()
  39. @ApiResponse({ type: CVO_association })
  40. async create(@Body() data: object) {
  41. const dbData = await this.service.create(data);
  42. const result = new CVO_association(dbData);
  43. return result;
  44. }
  45. @Post('/:id', { routerName: `修改${namePrefix}` })
  46. @ApiTags('修改数据')
  47. @Validate()
  48. @ApiResponse({ type: UVAO_association })
  49. async update(@Param('id') id: number, @Body() data: object) {
  50. if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
  51. const result = await this.service.update({ id }, data);
  52. await this.utilService.updateUserAfter(id, data, 'Association');
  53. return result;
  54. }
  55. @Del('/:id', { routerName: `删除${namePrefix}` })
  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. }