TermVocabularyController.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // ✅ 使用 match 替代 switch
  29. $data = match ($view) {
  30. 'grammar' => $this->termService->getGrammarGlossary($lang),
  31. 'community' => $this->termService->getCommunityGlossary($lang),
  32. 'studio', 'user' => throw new \Exception('not implemented'),
  33. default => throw new \InvalidArgumentException('invalid view'),
  34. };
  35. return $this->ok([
  36. 'rows' => TermVocabularyResource::collection($data['items']),
  37. 'count' => $data['total'],
  38. ]);
  39. }
  40. /**
  41. * Store a newly created resource in storage.
  42. *
  43. * @param \Illuminate\Http\Request $request
  44. * @return \Illuminate\Http\Response
  45. */
  46. public function store(Request $request)
  47. {
  48. //
  49. }
  50. /**
  51. * Display the specified resource.
  52. *
  53. * @param \App\Models\DhammaTerm $dhammaTerm
  54. * @return \Illuminate\Http\Response
  55. */
  56. public function show(DhammaTerm $dhammaTerm)
  57. {
  58. //
  59. }
  60. /**
  61. * Update the specified resource in storage.
  62. *
  63. * @param \Illuminate\Http\Request $request
  64. * @param \App\Models\DhammaTerm $dhammaTerm
  65. * @return \Illuminate\Http\Response
  66. */
  67. public function update(Request $request, DhammaTerm $dhammaTerm)
  68. {
  69. //
  70. }
  71. /**
  72. * Remove the specified resource from storage.
  73. *
  74. * @param \App\Models\DhammaTerm $dhammaTerm
  75. * @return \Illuminate\Http\Response
  76. */
  77. public function destroy(DhammaTerm $dhammaTerm)
  78. {
  79. //
  80. }
  81. }