mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-07-30 04:23:11 +03:00
#47 - Adds functionality to display child comments. Also has some code towards the reply functionality.
This commit is contained in:
@ -34,8 +34,8 @@ class Comment extends Ownable
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function getParentCommentsByPage($pageId, $pageNum = 0, $limit = 0) {
|
||||
$data = ['pageId' => $pageId];
|
||||
public function getCommentsByPage($pageId, $commentId, $pageNum = 0, $limit = 0) {
|
||||
|
||||
$query = static::newQuery();
|
||||
$query->join('users AS u', 'comments.created_by', '=', 'u.id');
|
||||
$query->leftJoin('users AS u1', 'comments.updated_by', '=', 'u1.id');
|
||||
@ -44,7 +44,12 @@ class Comment extends Ownable
|
||||
. 'u.name AS created_by_name, u1.name AS updated_by_name, '
|
||||
. '(SELECT count(c.id) FROM bookstack.comments c WHERE c.parent_id = comments.id AND page_id = ?) AS cnt_sub_comments, i.url AS avatar ',
|
||||
[$pageId]);
|
||||
$query->whereRaw('page_id = ? AND parent_id IS NULL', [$pageId]);
|
||||
|
||||
if (empty($commentId)) {
|
||||
$query->whereRaw('page_id = ? AND parent_id IS NULL', [$pageId]);
|
||||
} else {
|
||||
$query->whereRaw('page_id = ? AND parent_id = ?', [$pageId, $commentId]);
|
||||
}
|
||||
$query->orderBy('created_at');
|
||||
return $query;
|
||||
}
|
||||
|
@ -85,7 +85,11 @@ class CommentController extends Controller
|
||||
$this->checkOwnablePermission('page-view', $page);
|
||||
|
||||
$comments = $this->commentRepo->getCommentsForPage($pageId, $commentId);
|
||||
|
||||
if (empty($commentId)) {
|
||||
// requesting for parent level comments, send the total count as well.
|
||||
$totalComments = $this->commentRepo->getCommentCount($pageId);
|
||||
return response()->json(array('success' => true, 'comments'=> $comments, 'total' => $totalComments));
|
||||
}
|
||||
return response()->json(array('success' => true, 'comments'=> $comments));
|
||||
}
|
||||
}
|
||||
|
@ -38,14 +38,13 @@ class CommentRepo {
|
||||
return $comment;
|
||||
}
|
||||
|
||||
public function getCommentsForPage($pageId, $commentId, $count = 20) {
|
||||
if (empty($commentId)) {
|
||||
// requesting parent comments
|
||||
$query = $this->comment->getParentCommentsByPage($pageId);
|
||||
return $query->paginate($count);
|
||||
} else {
|
||||
// requesting the child comments.
|
||||
return Comment::whereRaw("page_id = $pageId AND parent_id = $commentId")->get();
|
||||
}
|
||||
public function getCommentsForPage($pageId, $commentId, $count = 20) {
|
||||
// requesting parent comments
|
||||
$query = $this->comment->getCommentsByPage($pageId, $commentId);
|
||||
return $query->paginate($count);
|
||||
}
|
||||
|
||||
public function getCommentCount($pageId) {
|
||||
return $this->comment->where('page_id', '=', $pageId)->count();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user