NavArticleController.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Resources\ArticleMapResource;
  4. use App\Models\ArticleCollection;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Http\Response;
  7. class NavArticleController extends Controller
  8. {
  9. /**
  10. * Display a listing of the resource.
  11. *
  12. * @return Response
  13. */
  14. public function index()
  15. {
  16. //
  17. }
  18. /**
  19. * Store a newly created resource in storage.
  20. *
  21. * @return Response
  22. */
  23. public function store(Request $request)
  24. {
  25. //
  26. }
  27. /**
  28. * Display the specified resource.
  29. * 文章导航
  30. * 文集中 某个文章的 前一个,后一个
  31. *
  32. * @param ArticleCollection $articleCollection
  33. * @return Response
  34. */
  35. public function show(string $id)
  36. {
  37. // article_anthology
  38. $id = explode('_', $id);
  39. if (count($id) !== 2) {
  40. return $this->error('参数错误。参数应为 2 实际得到'.count($id), 400, 400);
  41. }
  42. $curr = ArticleCollection::where('collect_id', $id[1])
  43. ->where('article_id', $id[0])
  44. ->first();
  45. if (! $curr) {
  46. return $this->error('article not found');
  47. }
  48. $data = [];
  49. $data['curr'] = new ArticleMapResource($curr);
  50. $prev = ArticleCollection::where('collect_id', $id[1])
  51. ->where('id', '<', $curr->id)
  52. ->orderBy('id', 'desc')
  53. ->first();
  54. if ($prev) {
  55. $data['prev'] = new ArticleMapResource($prev);
  56. }
  57. $next = ArticleCollection::where('collect_id', $id[1])
  58. ->where('id', '>', $curr->id)
  59. ->orderBy('id')
  60. ->first();
  61. if ($next) {
  62. $data['next'] = new ArticleMapResource($next);
  63. }
  64. return $this->ok($data);
  65. }
  66. /**
  67. * Update the specified resource in storage.
  68. *
  69. * @return Response
  70. */
  71. public function update(Request $request, ArticleCollection $articleCollection)
  72. {
  73. //
  74. }
  75. /**
  76. * Remove the specified resource from storage.
  77. *
  78. * @return Response
  79. */
  80. public function destroy(ArticleCollection $articleCollection)
  81. {
  82. //
  83. }
  84. }