]> BookStack Code Mirror - bookstack/blobdiff - app/Repos/CommentRepo.php
#47 - Fixes the issues with the test case.
[bookstack] / app / Repos / CommentRepo.php
index 7d0c4ebd7b419ad11035cb680dfa03995dcc1f13..ce71b923498ac0ed376ac25ab154f42852ddfb2b 100644 (file)
@@ -31,10 +31,26 @@ class CommentRepo {
         return $comment;
     }
 
-    public function update($comment, $input) {
+    public function update($comment, $input, $activeOnly = true) {
         $userId = user()->id;
         $comment->updated_by = $userId;
         $comment->fill($input);
+
+        // only update active comments by default.
+        $whereClause = ['active' => 1];
+        if (!$activeOnly) {
+            $whereClause = [];
+        }
+        $comment->update($whereClause);
+        return $comment;
+    }
+
+    public function delete($comment) {
+        $comment->text = trans('entities.comment_deleted');
+        $comment->html = trans('entities.comment_deleted');
+        $comment->active = false;
+        $userId = user()->id;
+        $comment->updated_by = $userId;
         $comment->save();
         return $comment;
     }
@@ -43,11 +59,14 @@ class CommentRepo {
         $comments = $this->comment->getAllPageComments($pageId);
         $index = [];
         $totalComments = count($comments);
+        $finalCommentList = [];
+
         // normalizing the response.
-        foreach($comments as &$comment) {
-            $comment = $this->normalizeComment($comment);
+        for ($i = 0; $i < count($comments); ++$i) {
+            $comment = $this->normalizeComment($comments[$i]);
             $parentId = $comment->parent_id;
             if (empty($parentId)) {
+                $finalCommentList[] = $comment;
                 $index[$comment->id] = $comment;
                 continue;
             }
@@ -63,7 +82,7 @@ class CommentRepo {
             $index[$comment->id] = $comment;
         }
         return [
-            'comments' => $comments,
+            'comments' => $finalCommentList,
             'total' => $totalComments
         ];
     }