region.controller.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { Body, Controller, Del, Get, Inject, Param, Post, Query } from '@midwayjs/core';
  2. import { BaseController } from '../../frame/BaseController';
  3. import { ApiQuery, ApiResponse, ApiTags } from '@midwayjs/swagger';
  4. import { Validate } from '@midwayjs/validate';
  5. import { omit, pick } from 'lodash';
  6. import { ErrorCode, ServiceError } from '../../error/service.error';
  7. import { CVO_region, FVO_region, QVO_region, UVAO_region } from '../../interface/system/region.interface';
  8. import { RegionService } from '../../service/system/region.service';
  9. const namePrefix = '地区';
  10. @ApiTags(['地区'])
  11. @Controller('/region', { tagName: namePrefix })
  12. export class RegionController implements BaseController {
  13. controllerCode = 'system_dict';
  14. @Inject()
  15. service: RegionService;
  16. @Get('/')
  17. @ApiTags('列表查询')
  18. @ApiQuery({ name: 'query' })
  19. @ApiResponse({ type: QVO_region })
  20. async index(@Query() query: object) {
  21. const qobj = omit(query, ['skip', 'limit']);
  22. const others = pick(query, ['skip', 'limit']);
  23. const qbr = this.service.queryBuilder(qobj);
  24. const result = await this.service.query(qbr, others);
  25. return result;
  26. }
  27. @Get('/:id')
  28. @ApiTags('单查询')
  29. @ApiResponse({ type: FVO_region })
  30. async fetch(@Param('id') id: number) {
  31. const qbr = this.service.queryBuilder({ id });
  32. const data = await this.service.fetch(qbr);
  33. const result = new FVO_region(data);
  34. return result;
  35. }
  36. @Post('/', { routerName: `创建${namePrefix}` })
  37. @ApiTags('创建数据')
  38. @Validate()
  39. @ApiResponse({ type: CVO_region })
  40. async create(@Body() data: object) {
  41. const dbData = await this.service.create(data);
  42. const result = new CVO_region(dbData);
  43. return result;
  44. }
  45. @Post('/:id', { routerName: `修改${namePrefix}` })
  46. @ApiTags('修改数据')
  47. @Validate()
  48. @ApiResponse({ type: UVAO_region })
  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. return result;
  53. }
  54. @Del('/:id', { routerName: `删除${namePrefix}` })
  55. @ApiTags('删除数据')
  56. @Validate()
  57. async delete(@Param('id') id: number) {
  58. if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
  59. const result = await this.service.delete({ id });
  60. return result;
  61. }
  62. }