1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- namespace app\Types\DatabaseObjects;
- use app\Exceptions\CommentNotFoundException;
- use app\Interfaces\IDatabaseObject;
- use app\Hajeebtok;
- use app\Logger;
- use app\Exceptions\SecurityFaultException;
- class Comment implements IDatabaseObject
- {
- public private(set) ?int $id;
- public private(set) ?int $author_id;
- public private(set) ?int $video_id;
- public private(set) ?int $reply_id;
- public private(set) ?string $content;
-
- public function __construct(?int $id=null, ?int $author_id=null, ?int $video_id=null, ?int $reply_id=null, ?string $content=null) {
- $this->id = $id;
- $this->author_id = $author_id;
- $this->video_id = $video_id;
- $this->reply_id = $reply_id;
- $this->content = $content;
- }
- /**
- * Creates the table for the object type in the database.
- */
- public static function CreateTable(): void {
- throw new SecurityFaultException("Attempt to create table on comment object.");
- }
- /**
- * Drops the table for the object type from the database.
- */
- public static function DropTable(): void {
- throw new SecurityFaultException("Attempt to drop table on comment object.");
- }
- /**
- * Saves the object to the database.
- */
- public function Save() {
- Hajeebtok::$Database->Query("INSERT INTO comments (author_id, video_id, reply_id, content) VALUES (:author_id, :video_id, :reply_id, :content)", [
- "author_id" => $this->author_id,
- "video_id" => $this->video_id,
- "reply_id" => $this->reply_id,
- "content" => $this->content
- ]);
- $id = Hajeebtok::$Database->LastInsertId();
- Logger::Debug("Saved comment id ($id).");
- }
- /**
- * Deletes the object from the database.
- */
- public function Delete() {
- Hajeebtok::$Database->Query("DELETE FROM comments WHERE id = :id", ["id" => $this->id]);
- }
- /**
- * Loads the object from the database.
- */
- public function Load() {
- if($this->id === null) throw new CommentNotFoundException(0, 404);
- $data = Hajeebtok::$Database->Row("SELECT * FROM comments WHERE id = :id", ["id" => $this->id]);
- if(empty($data)) throw new CommentNotFoundException($this->id, 404);
- $this->author_id = $data["author_id"];
- $this->video_id = $data["video_id"];
- $this->reply_id = $data["reply_id"];
- $this->content = $data["content"];
- }
- public function LoadMany(): array {
- if($this->video_id === null) throw new CommentNotFoundException(0, 404);
- $data = Hajeebtok::$Database->Query("SELECT * FROM comments WHERE video_id = :video_id", ["video_id" => $this->video_id]);
- if(empty($data)) throw new CommentNotFoundException($this->video_id, 404);
- return $data;
- }
- }
|