achievement.controller.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { AchievementService } from '../../service/platform/achievement.service';
  2. import { CVO_achievement, FVO_achievement, QVO_achievement, UVAO_achievement } from '../../interface/platform/achievement.interface';
  3. import { ApiResponse, ApiTags, ApiQuery } from '@midwayjs/swagger';
  4. import { Validate } from '@midwayjs/validate';
  5. import { BaseController } from '../../frame/BaseController';
  6. import { ErrorCode, ServiceError } from '../../error/service.error';
  7. import { omit, pick } from 'lodash';
  8. import { Controller, Inject, Get, Param, Post, Body, Del, Query } from '@midwayjs/core';
  9. import { Context } from '@midwayjs/koa';
  10. const namePrefix = '成果';
  11. @ApiTags(['成果'])
  12. @Controller('/achievement', { tagName: namePrefix })
  13. export class AchievementController implements BaseController {
  14. @Inject()
  15. service: AchievementService;
  16. @Inject()
  17. ctx: Context;
  18. @Get('/')
  19. @ApiTags('列表查询')
  20. @ApiQuery({ name: 'query' })
  21. @ApiResponse({ type: QVO_achievement })
  22. async index(@Query() query: object) {
  23. const qobj = omit(query, ['skip', 'limit']);
  24. const others = pick(query, ['skip', 'limit']);
  25. const qbr = this.service.queryBuilder(qobj);
  26. const result = await this.service.query(qbr, others);
  27. return result;
  28. }
  29. @Get('/:id')
  30. @ApiTags('单查询')
  31. @ApiResponse({ type: FVO_achievement })
  32. async fetch(@Param('id') id: number) {
  33. const qbr = this.service.queryBuilder({ id });
  34. const data = await this.service.fetch(qbr);
  35. const result = new FVO_achievement(data);
  36. return result;
  37. }
  38. @Post('/', { routerName: `创建${namePrefix}` })
  39. @ApiTags('创建数据')
  40. @Validate()
  41. @ApiResponse({ type: CVO_achievement })
  42. async create(@Body() data: object) {
  43. const dbData = await this.service.create(data);
  44. const result = new CVO_achievement(dbData);
  45. return result;
  46. }
  47. @Post('/:id', { routerName: `修改${namePrefix}` })
  48. @ApiTags('修改数据')
  49. @Validate()
  50. @ApiResponse({ type: UVAO_achievement })
  51. async update(@Param('id') id: number, @Body() data: object) {
  52. if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
  53. const result = await this.service.update({ id }, data);
  54. return result;
  55. }
  56. @Del('/:id', { routerName: `删除${namePrefix}` })
  57. @ApiTags('删除数据')
  58. @Validate()
  59. async delete(@Param('id') id: number) {
  60. if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
  61. const result = await this.service.delete({ id });
  62. return result;
  63. }
  64. @Get('/detail/:id')
  65. @ApiResponse({ type: FVO_achievement })
  66. async detail(@Param('id') id: string) {
  67. const data = await this.service.detail(id);
  68. return data;
  69. }
  70. @Get('/list')
  71. async list() {
  72. const list = await this.service.list(this.ctx.filter);
  73. const data = list.data;
  74. const total = list.total;
  75. return { data, total };
  76. }
  77. }