project.controller.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { Controller, Inject, Get, Param, Post, Body, Del, Query } from '@midwayjs/core';
  2. import { ProjectService } from '../../service/platform/project.service';
  3. import { ApiResponse, ApiTags, ApiQuery } from '@midwayjs/swagger';
  4. import { Validate } from '@midwayjs/validate';
  5. import { omit, pick } from 'lodash';
  6. import { ServiceError, ErrorCode } from '../../error/service.error';
  7. import { BaseController } from '../../frame/BaseController';
  8. import { QVO_project, FVO_project, CVO_project, UVAO_project } from '../../interface/platform/project.interface';
  9. import { Context } from '@midwayjs/koa';
  10. import { ServiceUtilService } from '../../service/serviceUtil.service';
  11. const namePrefix = '项目';
  12. @ApiTags(['项目'])
  13. @Controller('/project', { tagName: namePrefix })
  14. export class ProjectController implements BaseController {
  15. @Inject()
  16. service: ProjectService;
  17. @Inject()
  18. ctx: Context;
  19. @Inject()
  20. serviceUtil: ServiceUtilService;
  21. @Get('/')
  22. @ApiTags('列表查询')
  23. @ApiQuery({ name: 'query' })
  24. @ApiResponse({ type: QVO_project })
  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_project })
  34. async fetch(@Param('id') id: number) {
  35. const data = await this.service.fetch({ id });
  36. const result = new FVO_project(data);
  37. return result;
  38. }
  39. @Post('/', { routerName: `创建${namePrefix}` })
  40. @ApiTags('创建数据')
  41. @Validate()
  42. @ApiResponse({ type: CVO_project })
  43. async create(@Body() data: object) {
  44. const dbData = await this.service.create(data);
  45. const result = new CVO_project(dbData);
  46. return result;
  47. }
  48. @Post('/:id', { routerName: `修改${namePrefix}` })
  49. @ApiTags('修改数据')
  50. @Validate()
  51. @ApiResponse({ type: UVAO_project })
  52. async update(@Param('id') id: number, @Body() data: object) {
  53. if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
  54. const result = await this.service.update({ id }, data);
  55. return result;
  56. }
  57. @Del('/:id', { routerName: `删除${namePrefix}` })
  58. @ApiTags('删除数据')
  59. @Validate()
  60. async delete(@Param('id') id: number) {
  61. if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
  62. const result = await this.service.delete({ id });
  63. return result;
  64. }
  65. @Get('/list')
  66. async list(@Query() query: object) {
  67. const qobj = omit(query, ['skip', 'limit']);
  68. const others = pick(query, ['skip', 'limit']);
  69. const { data, total } = await this.service.query(qobj, others);
  70. for (const i of data) {
  71. await this.serviceUtil.fillOnwer(i);
  72. }
  73. return { data, total };
  74. }
  75. @Get('/detail/:id')
  76. @ApiResponse({ type: FVO_project })
  77. async detail(@Param('id') id: string) {
  78. const data = await this.service.fetch({ id });
  79. await this.serviceUtil.fillOnwer(data);
  80. await this.serviceUtil.fillCollection(data);
  81. return data;
  82. }
  83. }