ChapterController.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Resources\ChapterResource;
  4. use App\Models\PaliText;
  5. use App\Services\PaliTextService;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Http\Response;
  8. class ChapterController 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 'toc':
  20. $chapter = PaliText::where('book', $request->input('book'))
  21. ->where('paragraph', $request->input('para'))
  22. ->first();
  23. $start = $request->input('para');
  24. $end = $request->input('para') + $chapter->chapter_len - 1;
  25. $table = PaliText::where('book', $request->input('book'))
  26. ->whereBetween('paragraph', [$start, $end])
  27. ->where('level', '<', 100)
  28. ->select(['book', 'paragraph', 'level', 'text', 'chapter_len', 'chapter_strlen', 'parent']);
  29. break;
  30. }
  31. // 获取记录总条数
  32. $count = $table->count();
  33. // 处理排序
  34. $table = $table->orderBy(
  35. $request->input('order', 'paragraph'),
  36. $request->input('dir', 'asc')
  37. );
  38. // 处理分页
  39. $table = $table->skip($request->input('offset', 0))
  40. ->take($request->input('limit', 1000));
  41. $result = $table->get();
  42. return $this->ok([
  43. 'rows' => ChapterResource::collection($result),
  44. 'count' => $count,
  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(string $id)
  62. {
  63. $para = explode('-', $id);
  64. if (count($para) < 2) {
  65. return $this->error('参数错误', 400, 400);
  66. }
  67. $paliTextService = app(PaliTextService::class);
  68. $paragraph = $paliTextService->getCurrChapter($para[0], $para[1]);
  69. return $this->ok(new ChapterResource($paragraph));
  70. }
  71. /**
  72. * Update the specified resource in storage.
  73. *
  74. * @return Response
  75. */
  76. public function update(Request $request, PaliText $paliText)
  77. {
  78. //
  79. }
  80. /**
  81. * Remove the specified resource from storage.
  82. *
  83. * @return Response
  84. */
  85. public function destroy(PaliText $paliText)
  86. {
  87. //
  88. }
  89. }