Message.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace app\Types\DatabaseObjects;
  3. use app\Exceptions\MessageNotFoundException;
  4. use app\Interfaces\IDatabaseObject;
  5. use app\Hajeebtok;
  6. use app\Logger;
  7. use Hajeebtok\Types\Exceptions\SecurityFaultException;
  8. class Message implements IDatabaseObject
  9. {
  10. public private(set) ?int $id;
  11. public private(set) ?int $sender_id;
  12. public private(set) ?int $recipient_id;
  13. public private(set) ?int $reply_id;
  14. public private(set) ?int $video_id;
  15. public private(set) ?string $content;
  16. 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)
  17. {
  18. $this->id = $id;
  19. $this->sender_id = $sender_id;
  20. $this->recipient_id = $recipient_id;
  21. $this->reply_id = $reply_id;
  22. $this->video_id = $video_id;
  23. $this->content = $content;
  24. }
  25. /**
  26. * Creates the table for the object type in the database.
  27. */
  28. public static function CreateTable(): void
  29. {
  30. throw new SecurityFaultException("Attempt to create table on message object.");
  31. }
  32. /**
  33. * Drops the table for the object type from the database.
  34. */
  35. public static function DropTable(): void
  36. {
  37. throw new SecurityFaultException("Attempt to drop table on message object.");
  38. }
  39. /**
  40. * Saves the object to the database.
  41. */
  42. public function Save()
  43. {
  44. 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);", [
  45. "sender_id" => $this->sender_id,
  46. "recipient_id" => $this->recipient_id,
  47. "reply_id" => $this->reply_id,
  48. "video_id" => $this->video_id,
  49. "content" => $this->content,
  50. ]);
  51. $id = Hajeebtok::$Database->LastInsertId();
  52. Logger::Debug("Saved message id ($id).");
  53. }
  54. /**
  55. * Deletes the object from the database.
  56. */
  57. public function Delete()
  58. {
  59. Hajeebtok::$Database->Query("DELETE FROM messages WHERE id = :id", ["id" => $this->id]);
  60. }
  61. /**
  62. * Loads the object from the database.
  63. */
  64. public function Load()
  65. {
  66. if ($this->id === null) throw new MessageNotFoundException(0, 404);
  67. $data = Hajeebtok::$Database->Row("SELECT * FROM messages WHERE id = :id", ["id" => $this->id]);
  68. if (empty($data)) throw new MessageNotFoundException($this->id, 404);
  69. $this->recipient_id = $data["recipient_id"];
  70. $this->sender_id = $data["sender_id"];
  71. $this->reply_id = $data["reply_id"];
  72. $this->video_id = $data["video_id"];
  73. $this->content = $data["content"];
  74. }
  75. public function LoadMany(): array {
  76. if($this->recipient_id === null) throw new MessageNotFoundException(0, 404);
  77. $data = Hajeebtok::$Database->Query("SELECT * FROM messages WHERE recipient_id = :recipient_id", ["recipient_id" => $this->recipient_id]);
  78. if(empty($data)) throw new MessageNotFoundException($this->recipient_id, 404);
  79. return $data;
  80. }
  81. }