X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/8e2437498f879f79f222119f8afeacf22f640f3d..refs/pull/711/head:/app/Repos/CommentRepo.php diff --git a/app/Repos/CommentRepo.php b/app/Repos/CommentRepo.php index c2a19ec0b..7f89e7f8d 100644 --- a/app/Repos/CommentRepo.php +++ b/app/Repos/CommentRepo.php @@ -1,51 +1,90 @@ comment = $comment; } - public function create (Page $page, $data = []) { - $userId = user()->id; - $comment = $this->comment->newInstance(); - $comment->fill($data); - // new comment - $comment->page_id = $page->id; - $comment->created_by = $userId; - $comment->save(); - return $comment; + /** + * Get a comment by ID. + * @param $id + * @return Comment|\Illuminate\Database\Eloquent\Model + */ + public function getById($id) + { + return $this->comment->newQuery()->findOrFail($id); } - public function update($comment, $input) { + /** + * 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($data); + $comment->created_by = $userId; $comment->updated_by = $userId; - $comment->fill($input); - $comment->save(); + $comment->local_id = $this->getNextLocalId($entity); + $entity->comments()->save($comment); + return $comment; + } + + /** + * 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) { - 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(); - } + + /** + * Delete a comment from the system. + * @param Comment $comment + * @return mixed + */ + public function delete($comment) + { + return $comment->delete(); + } + + /** + * 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 +}