123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- namespace app\Types\DatabaseObjects;
- use app\Exceptions\ViewNotFoundException;
- use app\Hajeebtok;
- use app\Interfaces\IDatabaseObject;
- use app\Logger;
- use app\Types\LinkEnum;
- use app\Exceptions\LinkNotFoundException;
- use Hajeebtok\Types\Exceptions\SecurityFaultException;
- class View implements IDatabaseObject
- {
- public private(set) ?int $video_id;
- public private(set) ?int $account_id;
- public function __construct(?int $video_id = null, ?int $account_id = null) {
- $this->video_id = $video_id;
- $this->account_id = $account_id;
- }
- /**
- * Creates the table for the object type in the database.
- */
- public static function CreateTable(): void {
- throw new SecurityFaultException("Attempt to create table on view object.");
- }
- /**
- * Drops the table for the object type from the database.
- */
- public static function DropTable(): void {
- throw new SecurityFaultException("Attempt to drop table on view object.");
- }
- /**
- * Saves the object to the database.
- */
- public function Save() {
- Hajeebtok::$Database->Query("INSERT INTO views (account_id, video_id) VALUES (:account_id, :video_id)", [
- "account_id" => $this->account_id,
- "video_id" => $this->video_id
- ]);
- Logger::Debug("Saved view.");
- }
- /**
- * Deletes the object from the database.
- */
- public function Delete() {
- // TODO: make this thing speficifc
- //Hajeebtok::$Database->Query("DELETE FROM link WHERE account_id = :id", ["id" => $this->id]);
- }
- public function DeleteMany() {
- if(!empty($this->account_id)) return Hajeebtok::$Database->Query("DELETE FROM views WHERE account_id = :account_id", ["account_id" => $this->account_id]);
- if(!empty($this->video_id)) return Hajeebtok::$Database->Query("DELETE FROM views WHERE video_id = :video_id", ["video_id" => $this->video_id]);
- }
- /**
- * Loads the object from the database.
- */
- public function Load() {
- // not implemented
- }
- public function LoadMany(): array {
- if(!empty($this->account_id)) {
- $array = ["account_id" => $this->account_id];
- $query = "SELECT * FROM links WHERE account_id = :account_id";
- } else if(!empty($this->video_id)) {
- $array = ["video_id" => $this->video_id];
- $query = "SELECT * FROM links WHERE video_id = :video_id";
- } else {
- throw new ViewNotFoundException(0, 404);
- }
- $data = Hajeebtok::$Database->Query($query, $array);
- if(empty($data)) throw new ViewNotFoundException(0, 404);
- return $data;
- }
- }
|