NavArticleController.php 2.6 KB

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