]> BookStack Code Mirror - bookstack/blobdiff - app/Repos/CommentRepo.php
Added 'zh_CN' to app.locales
[bookstack] / app / Repos / CommentRepo.php
index e8db3f83e21a918cfb6263b23398cc2c7aa906c7..7f89e7f8dfb3011eabbf60aca05c8fd5b5c3451d 100644 (file)
@@ -4,14 +4,87 @@ use BookStack\Comment;
 use BookStack\Entity;
 
 /**
- * Class TagRepo
+ * Class CommentRepo
  * @package BookStack\Repos
  */
-class CommentRepo {
+class CommentRepo
+{
+
     /**
-     *
-     * @var Comment $comment 
+     * @var Comment $comment
      */
     protected $comment;
-    
-}
\ No newline at end of file
+
+    /**
+     * CommentRepo constructor.
+     * @param Comment $comment
+     */
+    public function __construct(Comment $comment)
+    {
+        $this->comment = $comment;
+    }
+
+    /**
+     * 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($data);
+        $comment->created_by = $userId;
+        $comment->updated_by = $userId;
+        $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;
+    }
+
+    /**
+     * 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;
+    }
+}