Skip to content

Commit

Permalink
Adding relation API endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyM84 committed Apr 2, 2021
1 parent 70c24c0 commit 76b38de
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
73 changes: 73 additions & 0 deletions api/1/Account.api.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,12 @@ protected function registerEndpoints() : void {
$this->registerEndpoint('GET', '/^Account\/Profile\/?/i', 'getProfile', true);
$this->registerEndpoint('POST', '/^Account\/Register\/?/i', 'registerUser', null);
$this->registerEndpoint('GET', '/^Account\/Relations\/?/i', 'getRelations', true);
$this->registerEndpoint('GET', '/^Account\/RelatedTo\/?/i', 'relatedTo', true);
$this->registerEndpoint('POST', '/^Account\/RemoveRelation\/?/i', 'removeRelation', true);
$this->registerEndpoint('POST', '/^Account\/ResetPassword\/?/i', 'resetPassword', false);
$this->registerEndpoint('POST', '/^Account\/SendPasswordReset\/?/i', 'sendPasswordReset', false);
$this->registerEndpoint('GET', '/^Account\/Settings\/?/i', 'getSettings', true);
$this->registerEndpoint('POST', '/^Account\/SetRelation\/?/i', 'setRelation', true);
$this->registerEndpoint('GET', '/^Account\/?/i', 'get', true);

return;
Expand All @@ -497,6 +500,52 @@ public function registerUser(Request $request, array $matches = null) : Response
return $ret;
}

/**
* Determines if the user is related to the given identifier.
*
* @param \Stoic\Web\Request $request The current request which routed to the endpoint.
* @param array|null $matches Array of matches returned by endpoint regex pattern.
* @return \Stoic\Web\Api\Response
*/
public function relatedTo(Request $request, array $matches = null) : Response {
$user = $this->getUser();
$ret = $this->newResponse();
$params = $request->getInput();

if (!$params->has('id')) {
$ret->setAsError('Invalid parameters supplied for request');

return $ret;
}

$ret->setData((new UserRelations($this->db, $this->log))->areRelated($user->id, $params->getInt('id')));

return $ret;
}

/**
* Attempts to remove a relationship between the authenticated user and another.
*
* @param \Stoic\Web\Request $request The current request which routed to the endpoint.
* @param array|null $matches Array of matches returned by endpoint regex pattern.
* @return \Stoic\Web\Api\Response
*/
public function removeRelation(Request $request, array $matches = null) : Response {
$user = $this->getUser();
$ret = $this->newResponse();
$params = $request->getInput();

if (!$params->has('id')) {
$ret->setAsError('Invalid parameters supplied');

return $ret;
}

$ret->setData((new UserRelations($this->db, $this->log))->deleteRelation($user->id, $params->getInt('id')));

return $ret;
}

/**
* Attempts to reset the user's password.
*
Expand Down Expand Up @@ -540,4 +589,28 @@ public function sendPasswordReset(Request $request, array $matches = null) : Res

return $ret;
}

/**
* Sets the stage of relationship between two users.
*
* @param \Stoic\Web\Request $request The current request which routed to the endpoint.
* @param array|null $matches Array of matches returned by endpoint regex pattern.
* @return \Stoic\Web\Api\Response
*/
public function setRelation(Request $request, array $matches = null) : Response {
$user = $this->getUser();
$ret = $this->newResponse();
$params = $request->getInput();
$rels = new UserRelations($this->db, $this->log);

if (!$params->hasAll('id', 'stage')) {
$ret->setAsError('Invalid parameters supplied');

return $ret;
}

$ret->setData($rels->changeStage($user->id, $params->getInt('id'), $params->getInt('stage')));

return $ret;
}
}
10 changes: 7 additions & 3 deletions inc/repositories/UserRelations.rpo.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ protected function __initialize() : void {
$this->urObj = new UserRelation($this->db, $this->log);

if (!static::$dbInitialized) {
PdoHelper::storeQuery(PdoDrivers::PDO_SQLSRV, self::SQL_DELREL, "DELETE FROM {$this->urObj->getDbTableName()} WHERE [UserID_One] = :userOne AND [UserID_Two] = :userTwo");
PdoHelper::storeQuery(PdoDrivers::PDO_MYSQL, self::SQL_DELREL, "DELETE FROM {$this->urObj->getDbTableName()} WHERE `UserID_One` = :userOne AND `UserID_Two` = :userTwo");
PdoHelper::storeQuery(PdoDrivers::PDO_SQLSRV, self::SQL_DELREL, "DELETE FROM {$this->urObj->getDbTableName()} WHERE ([UserID_One] = :userOne AND [UserID_Two] = :userTwo) OR ([UserID_One] = :userTwo AND [UserID_Two] = :userOne)");
PdoHelper::storeQuery(PdoDrivers::PDO_MYSQL, self::SQL_DELREL, "DELETE FROM {$this->urObj->getDbTableName()} WHERE (`UserID_One` = :userOne AND `UserID_Two` = :userTwo) OR (`UserID_One` = :userTwo AND `UserID_Two` = :userOne)");

PdoHelper::storeQuery(PdoDrivers::PDO_SQLSRV, self::SQL_DELALLFORUSR, "DELETE FROM {$this->urObj->getDbTableName()} WHERE [UserID_One] = :userId OR [UserID_Two] = :userId");
PdoHelper::storeQuery(PdoDrivers::PDO_MYSQL, self::SQL_DELALLFORUSR, "DELETE FROM {$this->urObj->getDbTableName()} WHERE `UserID_One` = :userId OR `UserID_Two` = :userId");
Expand Down Expand Up @@ -157,6 +157,10 @@ public function changeStage(int $userOne, int $userTwo, int $stage) : bool {
return false;
}

if ($rel[0]->stage->is(UserRelationStages::INVITED) && $stage > UserRelationStages::INVITED && $rel[0]->userOne == $userOne) {
return false;
}

if ($rel[0]->stage->is($stage)) {
return true;
}
Expand Down Expand Up @@ -190,7 +194,7 @@ public function deleteRelation(int $userOne, int $userTwo) : bool {
$stmt->execute();
}, "Failed to delete user's relation");

return false;
return true;
}

/**
Expand Down

0 comments on commit 76b38de

Please sign in to comment.