news.controller.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { NewsService } from '../../service/platform/news.service';
  2. import { CVO_news, FVO_news, QVO_news, UVAO_news } from '../../interface/platform/news.interface';
  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 { Controller, Inject, Get, Param, Post, Body, Del, Query } from '@midwayjs/core';
  8. import { BaseController } from '../../frame/BaseController';
  9. import { ServiceUtilService } from '../../service/serviceUtil.service';
  10. const namePrefix = '收藏';
  11. @ApiTags(['收藏'])
  12. @Controller('/news', { tagName: namePrefix })
  13. export class NewsController implements BaseController {
  14. @Inject()
  15. service: NewsService;
  16. @Inject()
  17. serviceUtil: ServiceUtilService;
  18. @Get('/')
  19. @ApiTags('列表查询')
  20. @ApiQuery({ name: 'query' })
  21. @ApiResponse({ type: QVO_news })
  22. async index(@Query() query: object) {
  23. const qobj = omit(query, ['skip', 'limit']);
  24. const others: any = pick(query, ['skip', 'limit']);
  25. others.order = { order_num: 'ASC' };
  26. const result = await this.service.query(qobj, others);
  27. return result;
  28. }
  29. @Get('/:id')
  30. @ApiTags('单查询')
  31. @ApiResponse({ type: FVO_news })
  32. async fetch(@Param('id') id: number) {
  33. const data = await this.service.fetch({ id });
  34. await this.service.fetchBrowse(data);
  35. const result = new FVO_news(data);
  36. return result;
  37. }
  38. @Post('/', { routerName: `创建${namePrefix}` })
  39. @ApiTags('创建数据')
  40. @Validate()
  41. @ApiResponse({ type: CVO_news })
  42. async create(@Body() data: object) {
  43. const dbData = await this.service.create(data);
  44. const result = new CVO_news(dbData);
  45. return result;
  46. }
  47. @Post('/:id', { routerName: `修改${namePrefix}` })
  48. @ApiTags('修改数据')
  49. @Validate()
  50. @ApiResponse({ type: UVAO_news })
  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_news })
  66. async detail(@Param('id') id: string) {
  67. let data = await this.service.fetch({ id });
  68. data = await this.serviceUtil.fillCollection(data, 'news');
  69. return data;
  70. }
  71. }