X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/03e5d61798e6b8df3b4d74b5f3bb1a96fe8199bb..refs/pull/635/head:/app/Repos/CommentRepo.php diff --git a/app/Repos/CommentRepo.php b/app/Repos/CommentRepo.php index 10b36eb16..d8c57bdb3 100644 --- a/app/Repos/CommentRepo.php +++ b/app/Repos/CommentRepo.php @@ -1,50 +1,87 @@ comment = $comment; } - public function create (Page $page, $data = []) { + /** + * Get a comment by ID. + * @param $id + * @return Comment|\Illuminate\Database\Eloquent\Model + */ + public function getById($id) + { + return $this->comment->newQuery()->findOrFail($id); + } + + /** + * Create a new comment on an entity. + * @param Entity $entity + * @param array $data + * @return Comment + */ + public function create (Entity $entity, $data = []) + { $userId = user()->id; - $comment = $this->comment->newInstance(); - $comment->fill($data); - // new comment - $comment->page_id = $page->id; + $comment = $this->comment->newInstance($data); $comment->created_by = $userId; - $comment->save(); + $comment->updated_by = $userId; + $comment->local_id = $this->getNextLocalId($entity); + $entity->comments()->save($comment); return $comment; } - public function update($comment, $input) { - $userId = user()->id; - $comment->updated_by = $userId; - $comment->fill($input); - $comment->save(); + /** + * Update an existing comment. + * @param Comment $comment + * @param array $input + * @return mixed + */ + public function update($comment, $input) + { + $comment->updated_by = user()->id; + $comment->update($input); return $comment; } - public function getCommentsForPage($pageId, $commentId, $count = 20) { - // requesting parent comments - $query = $this->comment->getCommentsByPage($pageId, $commentId); - return $query->paginate($count); + /** + * Delete a comment from the system. + * @param Comment $comment + * @return mixed + */ + public function delete($comment) + { + return $comment->delete(); } - public function getCommentCount($pageId) { - return $this->comment->where('page_id', '=', $pageId)->count(); + /** + * Get the next local ID relative to the linked entity. + * @param Entity $entity + * @return int + */ + protected function getNextLocalId(Entity $entity) + { + $comments = $entity->comments(false)->orderBy('local_id', 'desc')->first(); + if ($comments === null) return 1; + return $comments->local_id + 1; } } \ No newline at end of file