| @@ 122-143 (lines=22) @@ | ||
| 119 | * @throws Exception If comment couldn't be updated |
|
| 120 | * |
|
| 121 | */ |
|
| 122 | public function update($commentId, $content){ |
|
| 123 | ||
| 124 | $validation = new Validation(); |
|
| 125 | if(!$validation->validate([ 'Content' => [$content, 'required|minLen(1)|maxLen(300)']])) { |
|
| 126 | $this->errors = $validation->errors(); |
|
| 127 | return false; |
|
| 128 | } |
|
| 129 | ||
| 130 | $database = Database::openConnection(); |
|
| 131 | $query = "UPDATE comments SET content = :content WHERE id = :id LIMIT 1 "; |
|
| 132 | $database->prepare($query); |
|
| 133 | $database->bindValue(':content', $content); |
|
| 134 | $database->bindValue(':id', $commentId); |
|
| 135 | $result = $database->execute(); |
|
| 136 | ||
| 137 | if(!$result){ |
|
| 138 | throw new Exception("Couldn't update comment of ID: " . $commentId); |
|
| 139 | } |
|
| 140 | ||
| 141 | $comment = $this->getById($commentId); |
|
| 142 | return $comment; |
|
| 143 | } |
|
| 144 | ||
| 145 | /** |
|
| 146 | * counting the number of comments of a post. |
|
| @@ 69-92 (lines=24) @@ | ||
| 66 | * @return array feed created |
|
| 67 | * @throws Exception if feed couldn't be created |
|
| 68 | */ |
|
| 69 | public function create($userId, $content){ |
|
| 70 | ||
| 71 | $validation = new Validation(); |
|
| 72 | if(!$validation->validate(['Content' => [$content, "required|minLen(4)|maxLen(300)"]])) { |
|
| 73 | $this->errors = $validation->errors(); |
|
| 74 | return false; |
|
| 75 | } |
|
| 76 | ||
| 77 | $database = Database::openConnection(); |
|
| 78 | $query = "INSERT INTO newsfeed (user_id, content) VALUES (:user_id, :content)"; |
|
| 79 | ||
| 80 | $database->prepare($query); |
|
| 81 | $database->bindValue(':user_id', $userId); |
|
| 82 | $database->bindValue(':content', $content); |
|
| 83 | $database->execute(); |
|
| 84 | ||
| 85 | if($database->countRows() !== 1){ |
|
| 86 | throw new Exception("Couldn't add news feed"); |
|
| 87 | } |
|
| 88 | ||
| 89 | $newsfeedId = $database->lastInsertedId(); |
|
| 90 | $feed = $this->getById($newsfeedId); |
|
| 91 | return $feed; |
|
| 92 | } |
|
| 93 | ||
| 94 | /** |
|
| 95 | * update news feed. |
|
| @@ 102-124 (lines=23) @@ | ||
| 99 | * @return array feed created |
|
| 100 | * @throws Exception if feed couldn't be updated |
|
| 101 | */ |
|
| 102 | public function update($newsfeedId, $content){ |
|
| 103 | ||
| 104 | $validation = new Validation(); |
|
| 105 | if(!$validation->validate(['Content' => [$content, "required|minLen(4)|maxLen(300)"]])) { |
|
| 106 | $this->errors = $validation->errors(); |
|
| 107 | return false; |
|
| 108 | } |
|
| 109 | ||
| 110 | $database = Database::openConnection(); |
|
| 111 | $query = "UPDATE newsfeed SET content = :content WHERE id = :id LIMIT 1"; |
|
| 112 | ||
| 113 | $database->prepare($query); |
|
| 114 | $database->bindValue(':content', $content); |
|
| 115 | $database->bindValue(':id', $newsfeedId); |
|
| 116 | $result = $database->execute(); |
|
| 117 | ||
| 118 | if(!$result){ |
|
| 119 | throw new Exception("Couldn't update newsfeed of ID: " . $newsfeedId); |
|
| 120 | } |
|
| 121 | ||
| 122 | $feed = $this->getById($newsfeedId); |
|
| 123 | return $feed; |
|
| 124 | } |
|
| 125 | ||
| 126 | } |
|