CollectionController.php 6.3 KB

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