123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- namespace app\Types\DatabaseObjects;
- use app\Exceptions\AccountNotFoundException;
- use app\Exceptions\VideoNotFoundException;
- use app\Interfaces\IDatabaseObject;
- use app\Hajeebtok;
- use app\Logger;
- use Hajeebtok\Types\Exceptions\SecurityFaultException;
- class Video implements IDatabaseObject
- {
- public private(set) ?int $id;
- public private(set) ?string $title;
- public private(set) ?string $description;
- public private(set) ?int $author_id;
- public private(set) ?int $likes;
- public private(set) ?int $dislikes;
- public private(set) ?int $shares;
- public private(set) ?int $comments;
-
- public function __construct(?int $id=null, ?string $title=null, ?string $description=null, ?int $author_id=null, ?int $likes=0, ?int $dislikes=0) {
- $this->id = $id;
- $this->title = $title;
- $this->description = $description;
- $this->author_id = $author_id;
- $this->likes = $likes;
- $this->dislikes = $dislikes;
- }
- /**
- * Creates the table for the object type in the database.
- */
- public static function CreateTable(): void {
- throw new SecurityFaultException("Attempt to create table on video object.");
- }
- /**
- * Drops the table for the object type from the database.
- */
- public static function DropTable(): void {
- throw new SecurityFaultException("Attempt to drop table on video object.");
- }
- /**
- * Saves the object to the database.
- */
- public function Save() {
- Hajeebtok::$Database->Query("INSERT INTO videos (title, description, author_id, likes, dislikes) VALUES (:title, :description, :author_id, :likes, :dislikes)", [
- "title" => $this->title,
- "description" => $this->description,
- "author_id" => $this->author_id,
- "likes" => $this->likes,
- "dislikes" => $this->dislikes
- ]);
- $id = Hajeebtok::$Database->LastInsertId();
- Logger::Debug("Saved video id ($id).");
- }
- /**
- * Deletes the object from the database.
- */
- public function Delete() {
- Hajeebtok::$Database->Query("DELETE FROM videos WHERE id = :id", ["id" => $this->id]);
- }
- /**
- * Loads the object from the database.
- */
- public function Load() {
- if($this->id === null) throw new VideoNotFoundException(0, 404);
- $data = Hajeebtok::$Database->Row("SELECT * FROM videos WHERE id = :id", ["id" => $this->id]);
- if(empty($data)) throw new VideoNotFoundException($this->id, 404);
- Logger::Debug("$data");
- $this->title = $data["title"];
- $this->description = $data["description"];
- $this->author_id = $data["author_id"];
- $this->likes = $data["likes"];
- $this->dislikes = $data["dislikes"];
- $this->comments = Hajeebtok::$Database->Single("SELECT COUNT(*) FROM comments WHERE video_id = :id", ["id" => $this->id]);
- $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]);
- }
- public function LoadMany(): array {
- if(empty($this->title)) throw new VideoNotFoundException(0, 404);
- $videos = Hajeebtok::$Database->Query("SELECT * FROM videos WHERE title LIKE :title", ["title" => "%$this->title%"]);
- if(empty($videos)) throw new VideoNotFoundException(0, 404);
- return $videos;
- }
- }
|