TermVocabularyController.php 2.9 KB

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