VocabularyController.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Cache;
  5. use App\Models\Vocabulary;
  6. use App\Http\Resources\VocabularyResource;
  7. class VocabularyController extends Controller
  8. {
  9. /**
  10. * Display a listing of the resource.
  11. *
  12. * @return \Illuminate\Http\Response
  13. */
  14. public function index(Request $request)
  15. {
  16. //
  17. switch ($request->input("view")) {
  18. case 'key':
  19. $key = (string) $request->input('key', '');
  20. if ($key === '') {
  21. return $this->ok(['rows' => [], 'count' => 0]);
  22. }
  23. $payload = Cache::remember(
  24. 'v13:dict_vocabulary:' . md5($key),
  25. config('mint.cache.expire'),
  26. function () use ($key) {
  27. $rows = Vocabulary::where('word', 'like', $key . '%')
  28. ->orWhere('word_en', 'like', $key . '%')
  29. ->orderBy('strlen')
  30. ->orderBy('word')
  31. ->take(50)
  32. ->get();
  33. return [
  34. 'rows' => VocabularyResource::collection($rows)->resolve(),
  35. 'count' => $rows->count(),
  36. ];
  37. }
  38. );
  39. return $this->ok($payload);
  40. }
  41. return $this->ok(['rows'=>[],'count'=>0]);
  42. }
  43. /**
  44. * Store a newly created resource in storage.
  45. *
  46. * @param \Illuminate\Http\Request $request
  47. * @return \Illuminate\Http\Response
  48. */
  49. public function store(Request $request)
  50. {
  51. //
  52. }
  53. /**
  54. * Display the specified resource.
  55. *
  56. * @param \App\Models\Vocabulary $vocabulary
  57. * @return \Illuminate\Http\Response
  58. */
  59. public function show(Vocabulary $vocabulary)
  60. {
  61. //
  62. }
  63. /**
  64. * Update the specified resource in storage.
  65. *
  66. * @param \Illuminate\Http\Request $request
  67. * @param \App\Models\Vocabulary $vocabulary
  68. * @return \Illuminate\Http\Response
  69. */
  70. public function update(Request $request, Vocabulary $vocabulary)
  71. {
  72. //
  73. }
  74. /**
  75. * Remove the specified resource from storage.
  76. *
  77. * @param \App\Models\Vocabulary $vocabulary
  78. * @return \Illuminate\Http\Response
  79. */
  80. public function destroy(Vocabulary $vocabulary)
  81. {
  82. //
  83. }
  84. }