StudioTermController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\Services\V3\AgentGrantService;
  9. use App\Services\V3\TermExchangeService;
  10. use App\Services\V3\TermService;
  11. /**
  12. * 某个 studio(本人或团队)的术语:列表与新建 studio 级术语。
  13. *
  14. * 取代 `GET /v2/terms?view=studio` 与不带 channel 的 `POST /v2/terms`(`DhammaTermController`),v2 原样保留。
  15. */
  16. class StudioTermController extends Controller
  17. {
  18. use ResolvesCurrentUser;
  19. public function __construct(
  20. private readonly TermService $terms,
  21. private readonly AgentGrantService $grants,
  22. private readonly TermExchangeService $exchange,
  23. ) {}
  24. /**
  25. * studio 的术语列表
  26. *
  27. * studio 名下的全部术语(它的 channel 里的与 studio 级的)。本人或团队成员能看。请求头 `Accept` 是
  28. * `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` 时导出成 xlsx(列与说明同 channel 的术语列表),
  29. * 取代 v2 `terms-export?view=studio`。
  30. *
  31. * @urlParam studio string required 用户或团队的 uuid
  32. *
  33. * @queryParam search string 按词、释义搜索
  34. * @queryParam quality string 质量;pending 含未评级的。Enum: featured,standard,draft,pending
  35. * @queryParam category string 百科分类
  36. * @queryParam general boolean 只要 studio 级术语(不属于任何 channel)
  37. * @queryParam order string 排序字段。Enum: updated_at,created_at,word Default: updated_at
  38. * @queryParam dir string 排序方向。Enum: asc,desc
  39. * @queryParam page integer 页码
  40. * @queryParam per_page integer 每页条数,最大 200。Default: 20
  41. *
  42. * @responseStatus 403 不是本人,也不是团队成员
  43. */
  44. public function index(IndexTermRequest $request, string $studio)
  45. {
  46. if (in_array(TermExchangeService::MIME, $request->getAcceptableContentTypes(), true)) {
  47. return response($this->exchange->export($this->terms->filtered(
  48. $this->terms->studioQuery($studio, ['general' => $request->boolean('general')] + $request->validated(), $this->currentUser($request)['user_uid']),
  49. $request->validated(),
  50. )), 200, ['Content-Type' => TermExchangeService::MIME, 'Content-Disposition' => 'attachment; filename="terms.xlsx"']);
  51. }
  52. return TermResource::collection($this->terms->listInStudio(
  53. $studio,
  54. ['general' => $request->boolean('general')] + $request->validated(),
  55. $this->currentUser($request)['user_uid'],
  56. $request->integer('per_page', 20),
  57. ));
  58. }
  59. /**
  60. * 新建 studio 级术语
  61. *
  62. * 不属于任何 channel,owner 名下所有 channel 共用。本人或团队 owner / manager 能建;AI 模型不能建。
  63. * 同一 studio、同一语言里 word@tag 唯一(409)。
  64. *
  65. * @urlParam studio string required 用户或团队的 uuid
  66. *
  67. * @bodyParam word string required 巴利词,建立后不能改
  68. * @bodyParam tag string|null 义项(同词多义时区分),不能含 @,建立后不能改
  69. * @bodyParam meaning string required 释义
  70. * @bodyParam language string required 语言
  71. * @bodyParam other_meaning string|null 其它释义
  72. * @bodyParam note string|null 正文
  73. * @bodyParam summary string|null 摘要,格式与 note 相同
  74. * @bodyParam quality string|null 质量。Enum: featured,standard,draft,pending
  75. * @bodyParam content_type string 正文方言。Enum: markdown,markdown-e2 Default: markdown-e2
  76. * @bodyParam redirect string|null 重定向到 word 或 word@tag
  77. *
  78. * @responseStatus 201 已新建
  79. * @responseStatus 403 不能在这个 studio 新建
  80. * @responseStatus 409 已有这个 word@tag
  81. */
  82. public function store(StoreTermRequest $request, string $studio)
  83. {
  84. return TermResource::make($this->terms->createInStudio(
  85. $studio,
  86. $this->grants->actor(
  87. $this->currentUser($request)['user_uid'],
  88. $request->header(AgentGrantService::HEADER),
  89. ),
  90. $request->validated(),
  91. ))->response()->setStatusCode(201);
  92. }
  93. }