1 <?php namespace BookStack\Actions;
3 use BookStack\Entities\Entity;
7 * @package BookStack\Repos
13 * @var \BookStack\Actions\Comment $comment
18 * CommentRepo constructor.
19 * @param \BookStack\Actions\Comment $comment
21 public function __construct(Comment $comment)
23 $this->comment = $comment;
27 * Get a comment by ID.
29 * @return \BookStack\Actions\Comment|\Illuminate\Database\Eloquent\Model
31 public function getById($id)
33 return $this->comment->newQuery()->findOrFail($id);
37 * Create a new comment on an entity.
38 * @param \BookStack\Entities\Entity $entity
40 * @return \BookStack\Actions\Comment
42 public function create(Entity $entity, $data = [])
45 $comment = $this->comment->newInstance($data);
46 $comment->created_by = $userId;
47 $comment->updated_by = $userId;
48 $comment->local_id = $this->getNextLocalId($entity);
49 $entity->comments()->save($comment);
54 * Update an existing comment.
55 * @param \BookStack\Actions\Comment $comment
59 public function update($comment, $input)
61 $comment->updated_by = user()->id;
62 $comment->update($input);
67 * Delete a comment from the system.
68 * @param \BookStack\Actions\Comment $comment
71 public function delete($comment)
73 return $comment->delete();
77 * Get the next local ID relative to the linked entity.
78 * @param \BookStack\Entities\Entity $entity
81 protected function getNextLocalId(Entity $entity)
83 $comments = $entity->comments(false)->orderBy('local_id', 'desc')->first();
84 if ($comments === null) {
87 return $comments->local_id + 1;