CollectionController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Auth;
  5. use App\{
  6. Collection,
  7. CollectionItem,
  8. Profile,
  9. Status
  10. };
  11. use League\Fractal;
  12. use App\Transformer\Api\{
  13. AccountTransformer,
  14. StatusTransformer,
  15. };
  16. use League\Fractal\Serializer\ArraySerializer;
  17. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  18. class CollectionController extends Controller
  19. {
  20. public function create(Request $request)
  21. {
  22. abort_if(!Auth::check(), 403);
  23. $profile = Auth::user()->profile;
  24. $collection = Collection::firstOrCreate([
  25. 'profile_id' => $profile->id,
  26. 'published_at' => null
  27. ]);
  28. return view('collection.create', compact('collection'));
  29. }
  30. public function show(Request $request, int $collection)
  31. {
  32. $collection = Collection::whereNotNull('published_at')->findOrFail($collection);
  33. if($collection->profile->status != null) {
  34. abort(404);
  35. }
  36. return view('collection.show', compact('collection'));
  37. }
  38. public function index(Request $request)
  39. {
  40. abort_if(!Auth::check(), 403);
  41. return $request->all();
  42. }
  43. public function store(Request $request, int $id)
  44. {
  45. abort_if(!Auth::check(), 403);
  46. $this->validate($request, [
  47. 'title' => 'required',
  48. 'description' => 'required',
  49. 'visibility' => 'required|alpha|in:public,private'
  50. ]);
  51. $profile = Auth::user()->profile;
  52. $collection = Collection::whereProfileId($profile->id)->findOrFail($id);
  53. $collection->title = e($request->input('title'));
  54. $collection->description = e($request->input('description'));
  55. $collection->visibility = e($request->input('visibility'));
  56. $collection->save();
  57. return 200;
  58. }
  59. public function publish(int $id)
  60. {
  61. abort_if(!Auth::check(), 403);
  62. $profile = Auth::user()->profile;
  63. $collection = Collection::whereProfileId($profile->id)->findOrFail($id);
  64. $collection->published_at = now();
  65. $collection->save();
  66. return $collection->url();
  67. }
  68. public function delete(Request $request, int $id)
  69. {
  70. abort_if(!Auth::check(), 403);
  71. $user = Auth::user();
  72. $collection = Collection::whereProfileId($user->profile_id)->findOrFail($id);
  73. $collection->items()->delete();
  74. $collection->delete();
  75. return 200;
  76. }
  77. public function storeId(Request $request)
  78. {
  79. $this->validate($request, [
  80. 'collection_id' => 'required|int|min:1|exists:collections,id',
  81. 'post_id' => 'required|int|min:1|exists:statuses,id'
  82. ]);
  83. $profileId = Auth::user()->profile_id;
  84. $collectionId = $request->input('collection_id');
  85. $postId = $request->input('post_id');
  86. $collection = Collection::whereProfileId($profileId)->findOrFail($collectionId);
  87. $count = $collection->items()->count();
  88. if($count >= 18) {
  89. abort(400, 'You can only add 18 posts per collection');
  90. }
  91. $status = Status::whereScope('public')
  92. ->whereIn('type', ['photo'])
  93. ->findOrFail($postId);
  94. $item = CollectionItem::firstOrCreate([
  95. 'collection_id' => $collection->id,
  96. 'object_type' => 'App\Status',
  97. 'object_id' => $status->id
  98. ],[
  99. 'order' => $count,
  100. ]);
  101. return 200;
  102. }
  103. public function get(Request $request, int $id)
  104. {
  105. $profile = Auth::check() ? Auth::user()->profile : [];
  106. $collection = Collection::findOrFail($id);
  107. if($collection->published_at == null) {
  108. if(!Auth::check() || $profile->id !== $collection->profile_id) {
  109. abort(404);
  110. }
  111. }
  112. return [
  113. 'id' => $collection->id,
  114. 'title' => $collection->title,
  115. 'description' => $collection->description,
  116. 'visibility' => $collection->visibility
  117. ];
  118. }
  119. public function getItems(Request $request, int $id)
  120. {
  121. $profile = Auth::user()->profile;
  122. $collection = Collection::findOrFail($id);
  123. $posts = $collection->posts()->orderBy('order', 'asc')->paginate(18);
  124. $fractal = new Fractal\Manager();
  125. $fractal->setSerializer(new ArraySerializer());
  126. $resource = new Fractal\Resource\Collection($posts, new StatusTransformer());
  127. $res = $fractal->createData($resource)->toArray();
  128. return response()->json($res);
  129. }
  130. }