View.php 2.5 KB

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