VocabularyController.php 2.2 KB

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