-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from creme332/api
Setup project for API
- Loading branch information
Showing
12 changed files
with
253 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
User-agent: * | ||
Allow: / | ||
Allow: / | ||
Disallow: /api/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Steamy\Controller; | ||
|
||
use Steamy\Core\Controller; | ||
use Steamy\Core\Utility; | ||
|
||
/** | ||
* Router for API. It is called for URLs of the form `/api/v1/...` | ||
* | ||
* E.g., http://localhost/steamy-sips/public/api/products | ||
*/ | ||
class API | ||
{ | ||
use Controller; | ||
|
||
private string $resource; | ||
|
||
public function __construct() | ||
{ | ||
header("Content-Type:application/json"); | ||
|
||
$this->resource = Utility::splitURL()[2] ?? ""; | ||
} | ||
|
||
/** | ||
* Checks if root relative url starts with /api/v1 | ||
* @return bool | ||
*/ | ||
private function validateURLFormat(): bool | ||
{ | ||
return preg_match("/^api\/v1/", $_GET["url"]) > 0; | ||
} | ||
|
||
public function index(): void | ||
{ | ||
if (!$this->validateURLFormat()) { | ||
echo "Invalid API URL: " . $_GET["url"]; | ||
die(); | ||
} | ||
|
||
// call appropriate controller to handle resource | ||
$controllerClassName = 'Steamy\\Controller\\API\\' . ucfirst($this->resource); | ||
|
||
if (class_exists($controllerClassName)) { | ||
(new $controllerClassName())->index(); | ||
} else { | ||
echo "Invalid API resource: " . $this->resource; | ||
die(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Steamy\Controller\API; | ||
|
||
use Steamy\Model\Product; | ||
|
||
/** | ||
* Handles /products route of api | ||
*/ | ||
class Products | ||
{ | ||
private function getProducts(): void | ||
{ | ||
$all_products = Product::getAll(); | ||
$result = []; | ||
foreach ($all_products as $product) { | ||
$result[] = $product->toArray(); | ||
} | ||
echo json_encode($result); | ||
} | ||
|
||
private function addProduct(): void | ||
{ | ||
} | ||
|
||
private function deleteProduct(): void | ||
{ | ||
} | ||
|
||
private function updateProduct(): void | ||
{ | ||
} | ||
|
||
|
||
public function index(): void | ||
{ | ||
switch ($_SERVER['REQUEST_METHOD']) { | ||
case 'GET': | ||
$this->getProducts(); | ||
break; | ||
case 'POST': | ||
$this->addProduct(); | ||
break; | ||
case 'DELETE': | ||
$this->deleteProduct(); | ||
break; | ||
case 'PUT': | ||
$this->updateProduct(); | ||
break; | ||
default: | ||
http_response_code(400); | ||
die(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Steamy\Controller\API; | ||
|
||
use Steamy\Model\Administrator; | ||
|
||
/** | ||
* Handles /sessions route of API | ||
*/ | ||
class Sessions | ||
{ | ||
private function handleLogin(): void | ||
{ | ||
$email = trim($_POST['email'] ?? ""); | ||
$password = trim($_POST['password'] ?? ""); | ||
|
||
if (empty($email) || empty($password)) { | ||
http_response_code(400); | ||
die(); | ||
} | ||
|
||
// fetch administrator account | ||
$admin = Administrator::getByEmail($email); | ||
|
||
// validate email | ||
if (!$admin) { | ||
http_response_code(401); | ||
die(); | ||
} | ||
|
||
// validate password | ||
if (!$admin->verifyPassword($password)) { | ||
http_response_code(401); | ||
die(); | ||
} | ||
|
||
$_SESSION['admin_email'] = $email; | ||
session_regenerate_id(); | ||
} | ||
|
||
public function index(): void | ||
{ | ||
switch ($_SERVER['REQUEST_METHOD']) { | ||
case 'POST': | ||
$this->handleLogin(); | ||
break; | ||
default: | ||
http_response_code(400); | ||
die(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.