Video.php 3.1 KB

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