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