]> BookStack Code Mirror - bookstack/blobdiff - app/Repos/CommentRepo.php
Merge branch 'master' of https://github.com/Abijeet/BookStack
[bookstack] / app / Repos / CommentRepo.php
index e8db3f83e21a918cfb6263b23398cc2c7aa906c7..c2a19ec0b06bddf478d710ff34b68c92d8139dc9 100644 (file)
@@ -1,7 +1,7 @@
 <?php namespace BookStack\Repos;
 
 use BookStack\Comment;
-use BookStack\Entity;
+use BookStack\Page;
 
 /**
  * Class TagRepo
@@ -13,5 +13,39 @@ class CommentRepo {
      * @var Comment $comment 
      */
     protected $comment;
+
+    public function __construct(Comment $comment)
+    {
+        $this->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;
+    }
+
+    public function update($comment, $input) {
+        $userId = user()->id;
+        $comment->updated_by = $userId;
+        $comment->fill($input);
+        $comment->save();
+        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();
+        }        
+    }
 }
\ No newline at end of file