1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?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 app\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 views WHERE account_id = :account_id";
- } else if(!empty($this->video_id)) {
- $array = ["video_id" => $this->video_id];
- $query = "SELECT * FROM views 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;
- }
- }
|