Video.php 3.3 KB

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