1 <?php namespace BookStack\Repos;
8 * @package BookStack\Repos
14 * @var Comment $comment
19 * CommentRepo constructor.
20 * @param Comment $comment
22 public function __construct(Comment $comment)
24 $this->comment = $comment;
28 * Get a comment by ID.
30 * @return Comment|\Illuminate\Database\Eloquent\Model
32 public function getById($id)
34 return $this->comment->newQuery()->findOrFail($id);
38 * Create a new comment on an entity.
39 * @param Entity $entity
43 public function create(Entity $entity, $data = [])
46 $comment = $this->comment->newInstance($data);
47 $comment->created_by = $userId;
48 $comment->updated_by = $userId;
49 $comment->local_id = $this->getNextLocalId($entity);
50 $entity->comments()->save($comment);
55 * Update an existing comment.
56 * @param Comment $comment
60 public function update($comment, $input)
62 $comment->updated_by = user()->id;
63 $comment->update($input);
68 * Delete a comment from the system.
69 * @param Comment $comment
72 public function delete($comment)
74 return $comment->delete();
78 * Get the next local ID relative to the linked entity.
79 * @param Entity $entity
82 protected function getNextLocalId(Entity $entity)
84 $comments = $entity->comments(false)->orderBy('local_id', 'desc')->first();
85 if ($comments === null) {
88 return $comments->local_id + 1;