news.controller.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. const namePrefix = '收藏';
  10. @ApiTags(['收藏'])
  11. @Controller('/news', { tagName: namePrefix })
  12. export class NewsController implements BaseController {
  13. @Inject()
  14. service: NewsService;
  15. @Get('/')
  16. @ApiTags('列表查询')
  17. @ApiQuery({ name: 'query' })
  18. @ApiResponse({ type: QVO_news })
  19. async index(@Query() query: object) {
  20. const qobj = omit(query, ['skip', 'limit']);
  21. const others = pick(query, ['skip', 'limit']);
  22. const qbr = this.service.queryBuilder(qobj);
  23. const result = await this.service.query(qbr, others);
  24. return result;
  25. }
  26. @Get('/:id')
  27. @ApiTags('单查询')
  28. @ApiResponse({ type: FVO_news })
  29. async fetch(@Param('id') id: number) {
  30. const qbr = this.service.queryBuilder({ id });
  31. const data = await this.service.fetch(qbr);
  32. await this.service.fetchBrowse(data);
  33. const result = new FVO_news(data);
  34. return result;
  35. }
  36. @Post('/', { routerName: `创建${namePrefix}` })
  37. @ApiTags('创建数据')
  38. @Validate()
  39. @ApiResponse({ type: CVO_news })
  40. async create(@Body() data: object) {
  41. const dbData = await this.service.create(data);
  42. const result = new CVO_news(dbData);
  43. return result;
  44. }
  45. @Post('/:id', { routerName: `修改${namePrefix}` })
  46. @ApiTags('修改数据')
  47. @Validate()
  48. @ApiResponse({ type: UVAO_news })
  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. }