CollectionController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. if($collection->visibility !== 'public') {
  37. abort_if(!Auth::check() || Auth::user()->profile_id != $collection->profile_id, 404);
  38. }
  39. return view('collection.show', compact('collection'));
  40. }
  41. public function index(Request $request)
  42. {
  43. abort_if(!Auth::check(), 403);
  44. return $request->all();
  45. }
  46. public function store(Request $request, int $id)
  47. {
  48. abort_if(!Auth::check(), 403);
  49. $this->validate($request, [
  50. 'title' => 'nullable',
  51. 'description' => 'nullable',
  52. 'visibility' => 'required|alpha|in:public,private'
  53. ]);
  54. $profile = Auth::user()->profile;
  55. $collection = Collection::whereProfileId($profile->id)->findOrFail($id);
  56. $collection->title = e($request->input('title'));
  57. $collection->description = e($request->input('description'));
  58. $collection->visibility = e($request->input('visibility'));
  59. $collection->save();
  60. return 200;
  61. }
  62. public function publish(Request $request, int $id)
  63. {
  64. abort_if(!Auth::check(), 403);
  65. $this->validate($request, [
  66. 'title' => 'nullable',
  67. 'description' => 'nullable',
  68. 'visibility' => 'required|alpha|in:public,private'
  69. ]);
  70. $profile = Auth::user()->profile;
  71. $collection = Collection::whereProfileId($profile->id)->findOrFail($id);
  72. if($collection->items()->count() == 0) {
  73. abort(404);
  74. }
  75. $collection->title = e($request->input('title'));
  76. $collection->description = e($request->input('description'));
  77. $collection->visibility = e($request->input('visibility'));
  78. $collection->published_at = now();
  79. $collection->save();
  80. return $collection->url();
  81. }
  82. public function delete(Request $request, int $id)
  83. {
  84. abort_if(!Auth::check(), 403);
  85. $user = Auth::user();
  86. $collection = Collection::whereProfileId($user->profile_id)->findOrFail($id);
  87. $collection->items()->delete();
  88. $collection->delete();
  89. return 200;
  90. }
  91. public function storeId(Request $request)
  92. {
  93. $this->validate($request, [
  94. 'collection_id' => 'required|int|min:1|exists:collections,id',
  95. 'post_id' => 'required|int|min:1|exists:statuses,id'
  96. ]);
  97. $profileId = Auth::user()->profile_id;
  98. $collectionId = $request->input('collection_id');
  99. $postId = $request->input('post_id');
  100. $collection = Collection::whereProfileId($profileId)->findOrFail($collectionId);
  101. $count = $collection->items()->count();
  102. if($count >= 18) {
  103. abort(400, 'You can only add 18 posts per collection');
  104. }
  105. $status = Status::whereScope('public')
  106. ->whereIn('type', ['photo', 'photo:album', 'video'])
  107. ->findOrFail($postId);
  108. $item = CollectionItem::firstOrCreate([
  109. 'collection_id' => $collection->id,
  110. 'object_type' => 'App\Status',
  111. 'object_id' => $status->id
  112. ],[
  113. 'order' => $count,
  114. ]);
  115. return 200;
  116. }
  117. public function get(Request $request, int $id)
  118. {
  119. $profile = Auth::check() ? Auth::user()->profile : [];
  120. $collection = Collection::whereVisibility('public')->findOrFail($id);
  121. if($collection->published_at == null) {
  122. if(!Auth::check() || $profile->id !== $collection->profile_id) {
  123. abort(404);
  124. }
  125. }
  126. return [
  127. 'id' => $collection->id,
  128. 'title' => $collection->title,
  129. 'description' => $collection->description,
  130. 'visibility' => $collection->visibility
  131. ];
  132. }
  133. public function getItems(Request $request, int $id)
  134. {
  135. $collection = Collection::findOrFail($id);
  136. if($collection->visibility !== 'public') {
  137. abort_if(!Auth::check() || Auth::user()->profile_id != $collection->profile_id, 404);
  138. }
  139. $posts = $collection->posts()->orderBy('order', 'asc')->paginate(18);
  140. $fractal = new Fractal\Manager();
  141. $fractal->setSerializer(new ArraySerializer());
  142. $resource = new Fractal\Resource\Collection($posts, new StatusTransformer());
  143. $res = $fractal->createData($resource)->toArray();
  144. return response()->json($res);
  145. }
  146. public function getUserCollections(Request $request, int $id)
  147. {
  148. $profile = Profile::whereNull('status')
  149. ->whereNull('domain')
  150. ->findOrFail($id);
  151. if($profile->is_private) {
  152. abort_if(!Auth::check(), 404);
  153. abort_if(!$profile->followedBy(Auth::user()->profile) && $profile->id != Auth::user()->profile_id, 404);
  154. }
  155. return $profile
  156. ->collections()
  157. ->has('posts')
  158. ->with('posts')
  159. ->whereVisibility('public')
  160. ->whereNotNull('published_at')
  161. ->orderByDesc('published_at')
  162. ->paginate(9)
  163. ->map(function($collection) {
  164. return [
  165. 'id' => $collection->id,
  166. 'title' => $collection->title,
  167. 'description' => $collection->description,
  168. 'thumb' => $collection->posts()->first()->thumb(),
  169. 'url' => $collection->url(),
  170. 'published_at' => $collection->published_at
  171. ];
  172. });
  173. }
  174. }