ChannelTermController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Http\Controllers\V3;
  3. use App\Http\Controllers\Concerns\ResolvesCurrentUser;
  4. use App\Http\Controllers\Controller;
  5. use App\Http\Requests\V3\IndexTermRequest;
  6. use App\Http\Requests\V3\StoreTermRequest;
  7. use App\Http\Resources\V3\TermResource;
  8. use App\Models\Channel;
  9. use App\Services\V3\AgentGrantService;
  10. use App\Services\V3\TermExchangeService;
  11. use App\Services\V3\TermService;
  12. /**
  13. * 某个 channel 的术语:列表与新建。
  14. *
  15. * 取代 `GET /v2/terms?view=channel` 与带 channel 的 `POST /v2/terms`(`DhammaTermController`),v2 原样保留。
  16. */
  17. class ChannelTermController extends Controller
  18. {
  19. use ResolvesCurrentUser;
  20. public function __construct(
  21. private readonly TermService $terms,
  22. private readonly AgentGrantService $grants,
  23. private readonly TermExchangeService $exchange,
  24. ) {}
  25. /**
  26. * channel 的术语列表
  27. *
  28. * 能读 channel 的人都能看。请求头 `Accept` 是
  29. * `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` 时导出成 xlsx:整表(过滤同列表、不分页),
  30. * 列为 id / word / tag / meaning / other_meaning / summary / note / quality / content_type / redirect / language /
  31. * channel_id / etag,可改后用导入接口传回(取代 v2 `terms-export`)。
  32. *
  33. * @unauthenticated
  34. *
  35. * @urlParam channel string required channel uid
  36. *
  37. * @queryParam search string 按词、释义搜索
  38. * @queryParam quality string 质量;pending 含未评级的。Enum: featured,standard,draft,pending
  39. * @queryParam category string 百科分类
  40. * @queryParam order string 排序字段。Enum: updated_at,created_at,word Default: updated_at
  41. * @queryParam dir string 排序方向。Enum: asc,desc
  42. * @queryParam page integer 页码
  43. * @queryParam per_page integer 每页条数,最大 200。Default: 20
  44. *
  45. * @responseStatus 403 私有 channel,且看不见
  46. */
  47. public function index(IndexTermRequest $request, Channel $channel)
  48. {
  49. if (in_array(TermExchangeService::MIME, $request->getAcceptableContentTypes(), true)) {
  50. return response($this->exchange->export($this->terms->filtered(
  51. $this->terms->channelQuery($channel, $this->currentUserUid($request)),
  52. $request->validated(),
  53. )), 200, ['Content-Type' => TermExchangeService::MIME, 'Content-Disposition' => 'attachment; filename="terms.xlsx"']);
  54. }
  55. return TermResource::collection($this->terms->listInChannel(
  56. $channel,
  57. $request->validated(),
  58. $this->currentUserUid($request),
  59. $request->integer('per_page', 20),
  60. ));
  61. }
  62. /**
  63. * 在 channel 里新建术语
  64. *
  65. * 能写 channel 内容的人(含团队 editor)。owner 与语言跟 channel。同一 channel 里 word@tag 唯一(409)。
  66. * 分类写在 note 的 `{{category|…}}` 里。AI 模型凭 `X-Agent-Grant` 代写。
  67. *
  68. * @urlParam channel string required channel uid
  69. *
  70. * @bodyParam word string required 巴利词,建立后不能改
  71. * @bodyParam tag string|null 义项(同词多义时区分),不能含 @,建立后不能改
  72. * @bodyParam meaning string required 释义
  73. * @bodyParam other_meaning string|null 其它释义
  74. * @bodyParam note string|null 正文
  75. * @bodyParam summary string|null 摘要,格式与 note 相同
  76. * @bodyParam quality string|null 质量。Enum: featured,standard,draft,pending
  77. * @bodyParam content_type string 正文方言。Enum: markdown,markdown-e2 Default: markdown-e2
  78. * @bodyParam redirect string|null 重定向到 word 或 word@tag
  79. *
  80. * @responseStatus 201 已新建
  81. * @responseStatus 403 不能写这个 channel
  82. * @responseStatus 409 已有这个 word@tag
  83. */
  84. public function store(StoreTermRequest $request, Channel $channel)
  85. {
  86. return TermResource::make($this->terms->createInChannel(
  87. $channel,
  88. $this->grants->actor(
  89. $this->currentUser($request)['user_uid'],
  90. $request->header(AgentGrantService::HEADER),
  91. ),
  92. $request->validated(),
  93. ))->response()->setStatusCode(201);
  94. }
  95. }