canBeViewedBy($request->user()), 404); return response()->json([ 'data' => $this->comments->mapComments($collection, $request->user()), ]); } public function store(Request $request, Collection $collection): JsonResponse { $this->authorize('comment', $collection); $data = $request->validate([ 'body' => ['required', 'string', 'min:2', 'max:4000'], 'parent_id' => ['nullable', 'integer', 'min:1'], ]); $parent = null; if (! empty($data['parent_id'])) { $parent = CollectionComment::query()->findOrFail((int) $data['parent_id']); abort_unless((int) $parent->collection_id === (int) $collection->id, 404); } $this->comments->create($collection->loadMissing('user'), $request->user(), (string) $data['body'], $parent); return response()->json([ 'ok' => true, 'comments' => $this->comments->mapComments($collection, $request->user()), 'comments_count' => (int) $collection->fresh()->comments_count, ]); } public function destroy(Request $request, Collection $collection, CollectionComment $comment): JsonResponse { abort_unless((int) $comment->collection_id === (int) $collection->id, 404); $this->comments->delete($comment->load('collection'), $request->user()); return response()->json([ 'ok' => true, 'comments' => $this->comments->mapComments($collection, $request->user()), 'comments_count' => (int) $collection->fresh()->comments_count, ]); } }