12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- namespace app\Types\DatabaseObjects;
- use app\Exceptions\FollowNotFoundException;
- use app\Interfaces\IDatabaseObject;
- use app\Hajeebtok;
- use app\Logger;
- use Hajeebtok\Types\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);
- }
- }
|