dept.controller.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { Body, Controller, Del, Get, Inject, Param, Post, Query } from '@midwayjs/core';
  2. import { DeptService } from '../../service/system/dept.service';
  3. import { BaseController } from '../../frame/BaseController';
  4. import { ApiQuery, ApiResponse, ApiTags } from '@midwayjs/swagger';
  5. import { Validate } from '@midwayjs/validate';
  6. import { omit, pick } from 'lodash';
  7. import { ErrorCode, ServiceError } from '../../error/service.error';
  8. import { CVO_dept, FVO_dept, QVO_dept, UVAO_dept } from '../../interface/system/dept.interface';
  9. @ApiTags(['部门表'])
  10. @Controller('/dept')
  11. export class DeptController implements BaseController {
  12. controllerCode = 'system_dept';
  13. @Inject()
  14. service: DeptService;
  15. @ApiTags('列表查询')
  16. @ApiQuery({ name: 'query' })
  17. @ApiResponse({ type: QVO_dept })
  18. async index(@Query() query: object) {
  19. const qobj = omit(query, ['skip', 'limit']);
  20. const others = pick(query, ['skip', 'limit']);
  21. const qbr = this.service.queryBuilder(qobj);
  22. const result = await this.service.query(qbr, others);
  23. return result;
  24. }
  25. @Get('/')
  26. @ApiTags('树型列表查询')
  27. @ApiResponse({ type: FVO_dept })
  28. async query() {
  29. const data = await this.service.queryAll();
  30. return data;
  31. }
  32. @Get('/nextLevel/:id')
  33. @ApiTags('下一级部门列表查询')
  34. @ApiQuery({ name: 'nextLevel' })
  35. @ApiResponse({ type: QVO_dept })
  36. async nextLevel(@Param('id') id: number, @Query('skip') skip: number, @Query('limit') limit: number) {
  37. const result = await this.service.getNextLevel(id, { skip, limit });
  38. return result;
  39. }
  40. @Get('/:id')
  41. @ApiTags('单查询')
  42. @ApiResponse({ type: FVO_dept })
  43. async fetch(@Param('id') id: number) {
  44. const qbr = this.service.queryBuilder({ id });
  45. const data = await this.service.fetch(qbr);
  46. const result = new FVO_dept(data);
  47. return result;
  48. }
  49. @Post('/')
  50. @ApiTags('创建数据')
  51. @Validate()
  52. @ApiResponse({ type: CVO_dept })
  53. async create(@Body() data: object) {
  54. const dbData = await this.service.create(data);
  55. const result = new CVO_dept(dbData);
  56. return result;
  57. }
  58. @Post('/:id')
  59. @ApiTags('修改数据')
  60. @Validate()
  61. @ApiResponse({ type: UVAO_dept })
  62. async update(@Param('id') id: number, @Body() data: object) {
  63. if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
  64. const result = await this.service.update({ id }, data);
  65. return result;
  66. }
  67. @Del('/:id')
  68. @ApiTags('删除数据')
  69. @Validate()
  70. async delete(@Param('id') id: number) {
  71. if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
  72. const result = await this.service.delete({ id });
  73. return result;
  74. }
  75. }