Comment.php 2.6 KB

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