1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- namespace app\Types\DatabaseObjects;
- use app\Exceptions\FollowNotFoundException;
- use app\Interfaces\IDatabaseObject;
- use app\Hajeebtok;
- use app\Logger;
- use app\Exceptions\SecurityFaultException;
- class Follow implements IDatabaseObject
- {
- public private(set) ?int $follower_id;
- public private(set) ?int $followee_id;
- public function __construct(?int $follower_id = null, ?int $followee_id = null)
- {
- $this->follower_id = $follower_id;
- $this->followee_id = $followee_id;
- }
- /**
- * Creates the table for the object type in the database.
- */
- public static function CreateTable(): void
- {
- throw new SecurityFaultException("Attempt to create table on follow object.");
- }
- /**
- * Drops the table for the object type from the database.
- */
- public static function DropTable(): void
- {
- throw new SecurityFaultException("Attempt to drop table on follow object.");
- }
-
- /**
- * Saves the object to the database.
- */
- public function Save()
- {
- Hajeebtok::$Database->Query("INSERT INTO follows (follower_id, followee_id) VALUES (:follower_id, :followee_id)", [
- "follower_id" => $this->follower_id,
- "followee_id" => $this->followee_id
- ]);
- $id = Hajeebtok::$Database->LastInsertId();
- Logger::Debug("Saved account id ($id).");
- }
- /**
- * Deletes the object from the database.
- */
- public function Delete()
- {
- if(empty($this->follower_id) || empty($this->followee_id)) throw new FollowNotFoundException(0, 404);
- Hajeebtok::$Database->Query("DELETE FROM follows WHERE follower_id = :follower_id AND followee_id = :followee_id", [
- "follower_id" => $this->follower_id,
- "followee_id" => $this->followee_id
- ]);
- }
- /**
- * Loads the object from the database.
- */
- public function Load()
- {
- // unimplemented
- }
- public function LoadMany(): array {
- if(!empty($this->follower_id) && empty($this->followee_id)) {
- $command = "SELECT * FROM follows WHERE follower_id = :follower_id";
- $array = ["follower_id" => $this->follower_id];
- } else if(empty($this->follower_id) && !empty($this->followee_id)) {
- $command = "SELECT * FROM follows WHERE followee_id = :followee_id";
- $array = ["followee_id" => $this->followee_id];
- } else {
- throw new FollowNotFoundException(0, 404);
- }
-
- return Hajeebtok::$Database->Query($command, $array);
- }
- public function Exists(): bool {
- if(empty($this->follower_id) || empty($this->followee_id)) throw new FollowNotFoundException(0, 400);
- $data = Hajeebtok::$Database->Query("SELECT * FROM follows WHERE follower_id = :follower_id AND followee_id = :followee_id", [
- "follower_id" => $this->follower_id,
- "followee_id" => $this->followee_id
- ]);
- return !empty($data);
- }
- }
|