Skip to content

Commit

Permalink
✨ Comment system
Browse files Browse the repository at this point in the history
  • Loading branch information
MarceauKa committed Nov 23, 2019
1 parent 31fe2c0 commit 532465d
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 189,977 deletions.
18 changes: 17 additions & 1 deletion app/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;

class Comment extends Model
{
Expand Down Expand Up @@ -71,4 +71,20 @@ public function scopeWithVisible(Builder $query, ?User $user): Builder

return $query;
}

public function repliesTree(Comment $comment = null, Collection $tree = null): Collection
{
$comment = $comment ?? $this;
$tree = $tree ?? collect();

$comment->replies->each(function (Comment $item) use ($tree) {
$tree->push($item);

if ($item->replies->isNotEmpty()) {
$tree->merge($item->repliesTree($item, $tree));
}
});

return $tree;
}
}
14 changes: 12 additions & 2 deletions app/Http/Controllers/Api/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,24 @@ public function moderate(Request $request, Shaark $shaark, int $id, int $comment
]);
}

/** @todo Remove comment and childrens */
public function delete(Request $request, int $id, int $comment_id)
{
$post = Post::withPrivate($request->user('api'))
->findOrFail($id);

$comment = Comment::postIs($id)
->isNotVisible()
->findOrFail($comment_id);

$repliesId = $comment->repliesTree()
->pluck('id')
->push($comment->id)
->toArray();

Comment::whereIn('id', $repliesId)->delete();

return response()->json([
'status' => 'deleted',
'message' => __('Deleted'),
]);
}
}

0 comments on commit 532465d

Please sign in to comment.