TermVocabularyController.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Resources\TermVocabularyResource;
  4. use App\Models\DhammaTerm;
  5. use App\Services\TermService;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Http\Response;
  8. class TermVocabularyController extends Controller
  9. {
  10. protected TermService $termService;
  11. public function __construct(TermService $termService)
  12. {
  13. $this->termService = $termService;
  14. }
  15. /**
  16. * Display a listing of the resource.
  17. *
  18. * @return Response
  19. */
  20. public function index(Request $request)
  21. {
  22. // ✅ 数据验证
  23. $validated = $request->validate([
  24. 'view' => ['required', 'string'],
  25. 'lang' => ['nullable', 'string'],
  26. ]);
  27. $view = $validated['view'];
  28. $lang = $validated['lang'] ?? null;
  29. // TODO: 下面两条 throw 都会变成 500,客户端因此分不清「我传错了参数」和
  30. // 「服务端挂了」。2026-08-11 在 next 上实测:view=community / grammar 返回
  31. // 200,view=my / all / public / user / studio 一律 500。
  32. // - 无效取值应是 422:把合法值写进上面的 validate('view' => [..., 'in:grammar,community']),
  33. // 由框架拦下,与本控制器已有的 required 校验一致
  34. // - studio / user 尚未实现,应返回 501,而不是与「参数写错」同一个状态码
  35. // ✅ 使用 match 替代 switch
  36. $data = match ($view) {
  37. 'grammar' => $this->termService->getGrammarGlossary($lang),
  38. 'community' => $this->termService->getCommunityGlossary($lang),
  39. 'studio', 'user' => throw new \Exception('not implemented'),
  40. default => throw new \InvalidArgumentException('invalid view'),
  41. };
  42. return $this->ok([
  43. 'rows' => TermVocabularyResource::collection($data['items']),
  44. 'count' => $data['total'],
  45. ]);
  46. }
  47. /**
  48. * Store a newly created resource in storage.
  49. *
  50. * @return Response
  51. */
  52. public function store(Request $request)
  53. {
  54. //
  55. }
  56. /**
  57. * Display the specified resource.
  58. *
  59. * @return Response
  60. */
  61. public function show(DhammaTerm $dhammaTerm)
  62. {
  63. //
  64. }
  65. /**
  66. * Update the specified resource in storage.
  67. *
  68. * @return Response
  69. */
  70. public function update(Request $request, DhammaTerm $dhammaTerm)
  71. {
  72. //
  73. }
  74. /**
  75. * Remove the specified resource from storage.
  76. *
  77. * @return Response
  78. */
  79. public function destroy(DhammaTerm $dhammaTerm)
  80. {
  81. //
  82. }
  83. }