mirror of
https://github.com/flextype/flextype.git
synced 2025-08-08 14:16:46 +02:00
feat(endpoints): update Content Endpoints #565
This commit is contained in:
406
src/flextype/core/Endpoints/Content.php
Normal file
406
src/flextype/core/Endpoints/Content.php
Normal file
@@ -0,0 +1,406 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype (https://flextype.org)
|
||||
* Founded by Sergey Romanenko and maintained by Flextype Community.
|
||||
*/
|
||||
|
||||
namespace Flextype\Endpoints;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
class Content extends Endpoints
|
||||
{
|
||||
/**
|
||||
* Fetch content.
|
||||
*
|
||||
* @param ServerRequestInterface $request PSR7 request.
|
||||
* @param ResponseInterface $response PSR7 response.
|
||||
*
|
||||
* @return ResponseInterface Response.
|
||||
*/
|
||||
public function fetch(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
// Get Query Params
|
||||
$queryParams = $request->getQueryParams();
|
||||
|
||||
// Check is utils api enabled
|
||||
if (! registry()->get('flextype.settings.api.images.enabled')) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Check is token param exists
|
||||
if (! isset($queryParams['token'])) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Check is id param exists
|
||||
if (! isset($queryParams['id'])) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Check is token exists
|
||||
if (! tokens()->has($queryParams['token'])) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(401), 401);
|
||||
}
|
||||
|
||||
// Fetch token
|
||||
$tokenData = tokens()->fetch($queryParams['token']);
|
||||
|
||||
// Check token state and limit_calls
|
||||
if (
|
||||
$tokenData['state'] === 'disabled' ||
|
||||
($tokenData['limit_calls'] !== 0 && $tokenData['calls'] >= $tokenData['limit_calls'])
|
||||
) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Update token calls
|
||||
tokens()->update($queryParams['token'], ['calls' => $tokenData['calls'] + 1]);
|
||||
|
||||
// Get content data
|
||||
$contentData = content()->fetch($queryParams['id'], $queryParams['options'] ?? [])->toArray();
|
||||
|
||||
if (count($contentData) > 0) {
|
||||
return $this->getApiResponse($response, $contentData, 200);
|
||||
} else {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(404), 404);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create content.
|
||||
*
|
||||
* @param ServerRequestInterface $request PSR7 request.
|
||||
* @param ResponseInterface $response PSR7 response.
|
||||
*
|
||||
* @return ResponseInterface Response.
|
||||
*/
|
||||
public function create(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
// Get Parser Body
|
||||
$data = $request->getParsedBody();
|
||||
|
||||
// Check is content api enabled
|
||||
if (! registry()->get('flextype.settings.api.images.enabled')) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Check is token param exists
|
||||
if (! isset($data['token'])) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Check is id param exists
|
||||
if (! isset($data['id'])) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Check is token exists
|
||||
if (! tokens()->has($data['token'])) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(401), 401);
|
||||
}
|
||||
|
||||
// Fetch token
|
||||
$tokenData = tokens()->fetch($data['token']);
|
||||
|
||||
// Verify access token
|
||||
if (! password_verify($data['access_token'], $tokenData['hashed_access_token'])) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(401), 401);
|
||||
}
|
||||
|
||||
// Check token state and limit_calls
|
||||
if (
|
||||
$tokenData['state'] === 'disabled' ||
|
||||
($tokenData['limit_calls'] !== 0 && $tokenData['calls'] >= $tokenData['limit_calls'])
|
||||
) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Update token calls
|
||||
tokens()->update($data['token'], ['calls' => $tokenData['calls'] + 1]);
|
||||
|
||||
// Create new content
|
||||
content()->create($data['id'], $data['data'] ?? []);
|
||||
|
||||
// Fetch content
|
||||
$contentData = content()->fetch($data['id']);
|
||||
|
||||
// Return response
|
||||
if (count($contentData) > 0) {
|
||||
return $this->getApiResponse($response, $contentData, 200);
|
||||
} else {
|
||||
return $this->getApiResponse($response, [], 404);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update content.
|
||||
*
|
||||
* @param ServerRequestInterface $request PSR7 request.
|
||||
* @param ResponseInterface $response PSR7 response.
|
||||
*
|
||||
* @return ResponseInterface Response.
|
||||
*/
|
||||
public function update(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
// Get Parser Body
|
||||
$data = $request->getParsedBody();
|
||||
|
||||
// Check is content api enabled
|
||||
if (! registry()->get('flextype.settings.api.images.enabled')) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Check is token param exists
|
||||
if (! isset($data['token'])) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Check is id param exists
|
||||
if (! isset($data['id'])) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Check is token exists
|
||||
if (! tokens()->has($data['token'])) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(401), 401);
|
||||
}
|
||||
|
||||
// Fetch token
|
||||
$tokenData = tokens()->fetch($data['token']);
|
||||
|
||||
// Verify access token
|
||||
if (! password_verify($data['access_token'], $tokenData['hashed_access_token'])) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(401), 401);
|
||||
}
|
||||
|
||||
// Check token state and limit_calls
|
||||
if (
|
||||
$tokenData['state'] === 'disabled' ||
|
||||
($tokenData['limit_calls'] !== 0 && $tokenData['calls'] >= $tokenData['limit_calls'])
|
||||
) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Update token calls
|
||||
tokens()->update($data['token'], ['calls' => $tokenData['calls'] + 1]);
|
||||
|
||||
// Update content
|
||||
content()->update($data['id'], $data['data'] ?? []);
|
||||
|
||||
// Fetch content
|
||||
$contentData = content()->fetch($data['id']);
|
||||
|
||||
// Return response
|
||||
if (count($contentData) > 0) {
|
||||
return $this->getApiResponse($response, $contentData, 200);
|
||||
} else {
|
||||
return $this->getApiResponse($response, [], 404);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Move content.
|
||||
*
|
||||
* @param ServerRequestInterface $request PSR7 request.
|
||||
* @param ResponseInterface $response PSR7 response.
|
||||
*
|
||||
* @return ResponseInterface Response.
|
||||
*/
|
||||
public function move(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
// Get Parser Body
|
||||
$data = $request->getParsedBody();
|
||||
|
||||
// Check is content api enabled
|
||||
if (! registry()->get('flextype.settings.api.content.enabled')) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Check is token param exists
|
||||
if (! isset($data['token'])) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Check is id param exists
|
||||
if (! isset($data['id'])) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Check is new_id param exists
|
||||
if (! isset($data['new_id'])) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Check is token exists
|
||||
if (! tokens()->has($data['token'])) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(401), 401);
|
||||
}
|
||||
|
||||
// Fetch token
|
||||
$tokenData = tokens()->fetch($data['token']);
|
||||
|
||||
// Verify access token
|
||||
if (! password_verify($data['access_token'], $tokenData['hashed_access_token'])) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(401), 401);
|
||||
}
|
||||
|
||||
// Check token state and limit_calls
|
||||
if (
|
||||
$tokenData['state'] === 'disabled' ||
|
||||
($tokenData['limit_calls'] !== 0 && $tokenData['calls'] >= $tokenData['limit_calls'])
|
||||
) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Update token calls
|
||||
tokens()->update($data['token'], ['calls' => $tokenData['calls'] + 1]);
|
||||
|
||||
// Move content
|
||||
content()->move($data['id'], $data['new_id']);
|
||||
|
||||
// Fetch content
|
||||
$contentData = content()->fetch($data['new_id']);
|
||||
|
||||
// Return response
|
||||
if (count($contentData) > 0) {
|
||||
return $this->getApiResponse($response, $contentData, 200);
|
||||
} else {
|
||||
return $this->getApiResponse($response, [], 404);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy content.
|
||||
*
|
||||
* @param ServerRequestInterface $request PSR7 request.
|
||||
* @param ResponseInterface $response PSR7 response.
|
||||
*
|
||||
* @return ResponseInterface Response.
|
||||
*/
|
||||
public function copy(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
// Get Parser Body
|
||||
$data = $request->getParsedBody();
|
||||
|
||||
// Check is content api enabled
|
||||
if (! registry()->get('flextype.settings.api.content.enabled')) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Check is token param exists
|
||||
if (! isset($data['token'])) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Check is id param exists
|
||||
if (! isset($data['id'])) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Check is new_id param exists
|
||||
if (! isset($data['new_id'])) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Check is token exists
|
||||
if (! tokens()->has($data['token'])) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(401), 401);
|
||||
}
|
||||
|
||||
// Fetch token
|
||||
$tokenData = tokens()->fetch($data['token']);
|
||||
|
||||
// Verify access token
|
||||
if (! password_verify($data['access_token'], $tokenData['hashed_access_token'])) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(401), 401);
|
||||
}
|
||||
|
||||
// Check token state and limit_calls
|
||||
if (
|
||||
$tokenData['state'] === 'disabled' ||
|
||||
($tokenData['limit_calls'] !== 0 && $tokenData['calls'] >= $tokenData['limit_calls'])
|
||||
) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Update token calls
|
||||
tokens()->update($data['token'], ['calls' => $tokenData['calls'] + 1]);
|
||||
|
||||
// Copy content
|
||||
content()->copy($data['id'], $data['new_id']);
|
||||
|
||||
// Fetch content
|
||||
$contentData = content()->fetch($data['new_id']);
|
||||
|
||||
// Return response
|
||||
if (count($contentData) > 0) {
|
||||
return $this->getApiResponse($response, $contentData, 200);
|
||||
} else {
|
||||
return $this->getApiResponse($response, [], 404);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete content.
|
||||
*
|
||||
* @param ServerRequestInterface $request PSR7 request.
|
||||
* @param ResponseInterface $response PSR7 response.
|
||||
*
|
||||
* @return ResponseInterface Response.
|
||||
*/
|
||||
public function delete(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
// Get Parser Body
|
||||
$data = $request->getParsedBody();
|
||||
|
||||
// Check is content api enabled
|
||||
if (! registry()->get('flextype.settings.api.content.enabled')) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Check is token param exists
|
||||
if (! isset($data['token'])) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Check is id param exists
|
||||
if (! isset($data['id'])) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Check is token exists
|
||||
if (! tokens()->has($data['token'])) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(401), 401);
|
||||
}
|
||||
|
||||
// Fetch token
|
||||
$tokenData = tokens()->fetch($data['token']);
|
||||
|
||||
// Verify access token
|
||||
if (! password_verify($data['access_token'], $tokenData['hashed_access_token'])) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(401), 401);
|
||||
}
|
||||
|
||||
// Check token state and limit_calls
|
||||
if (
|
||||
$tokenData['state'] === 'disabled' ||
|
||||
($tokenData['limit_calls'] !== 0 && $tokenData['calls'] >= $tokenData['limit_calls'])
|
||||
) {
|
||||
return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400);
|
||||
}
|
||||
|
||||
// Update token calls
|
||||
tokens()->update($data['token'], ['calls' => $tokenData['calls'] + 1]);
|
||||
|
||||
// Copy content
|
||||
content()->delete($data['id']);
|
||||
|
||||
// Return success response
|
||||
return $this->getApiResponse($response, [], 204);
|
||||
}
|
||||
}
|
@@ -448,9 +448,9 @@ plugins()->init();
|
||||
* Include API ENDPOINTS
|
||||
*/
|
||||
require_once ROOT_DIR . '/src/flextype/routes/endpoints/utils.php';
|
||||
//include_once ROOT_DIR . '/src/flextype/Endpoints/errors.php';
|
||||
//include_once ROOT_DIR . '/src/flextype/Endpoints/images.php';
|
||||
//include_once ROOT_DIR . '/src/flextype/Endpoints/utils.php';
|
||||
require_once ROOT_DIR . '/src/flextype/routes/endpoints/images.php';
|
||||
require_once ROOT_DIR . '/src/flextype/routes/endpoints/content.php';
|
||||
|
||||
|
||||
// Enable lazy CORS
|
||||
//
|
||||
|
106
src/flextype/routes/endpoints/content.php
Normal file
106
src/flextype/routes/endpoints/content.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype (https://flextype.org)
|
||||
* Founded by Sergey Romanenko and maintained by Flextype Community.
|
||||
*/
|
||||
|
||||
namespace Flextype;
|
||||
|
||||
use Flextype\Endpoints\Content;
|
||||
|
||||
/**
|
||||
* Fetch content
|
||||
*
|
||||
* endpoint: GET /api/content
|
||||
*
|
||||
* Query:
|
||||
* id - [REQUIRED] - Unique identifier of the content.
|
||||
* token - [REQUIRED] - Valid public token.
|
||||
* options - [OPTIONAL] - Select items in collection by given conditions.
|
||||
*
|
||||
* Returns:
|
||||
* An array of content objects.
|
||||
*/
|
||||
app()->get('/api/content', [Content::class, 'fetch']);
|
||||
|
||||
/**
|
||||
* Create content
|
||||
*
|
||||
* endpoint: POST /api/content
|
||||
*
|
||||
* Body:
|
||||
* id - [REQUIRED] - Unique identifier of the content.
|
||||
* token - [REQUIRED] - Valid public token.
|
||||
* access_token - [REQUIRED] - Valid access token.
|
||||
* data - [REQUIRED] - Data to store for the content.
|
||||
*
|
||||
* Returns:
|
||||
* Returns the content object for the content that was just created.
|
||||
*/
|
||||
app()->post('/api/content', [Content::class, 'create']);
|
||||
|
||||
/**
|
||||
* Update content
|
||||
*
|
||||
* endpoint: PATCH /api/content
|
||||
*
|
||||
* Body:
|
||||
* id - [REQUIRED] - Unique identifier of the content.
|
||||
* token - [REQUIRED] - Valid public token.
|
||||
* access_token - [REQUIRED] - Valid access token.
|
||||
* data - [REQUIRED] - Data to update for the content.
|
||||
*
|
||||
* Returns:
|
||||
* Returns the content object for the content that was just updated.
|
||||
*/
|
||||
app()->patch('/api/content', [Content::class, 'update']);
|
||||
|
||||
/**
|
||||
* Move content
|
||||
*
|
||||
* endpoint: PUT /api/content
|
||||
*
|
||||
* Body:
|
||||
* id - [REQUIRED] - Unique identifier of the content.
|
||||
* new_id - [REQUIRED] - New Unique identifier of the content.
|
||||
* token - [REQUIRED] - Valid public token.
|
||||
* access_token - [REQUIRED] - Valid access token.
|
||||
*
|
||||
* Returns:
|
||||
* Returns the content object for the content that was just moved.
|
||||
*/
|
||||
app()->put('/api/content', [Content::class, 'move']);
|
||||
|
||||
/**
|
||||
* Copy content
|
||||
*
|
||||
* endpoint: PUT /api/content/copy
|
||||
*
|
||||
* Body:
|
||||
* id - [REQUIRED] - Unique identifier of the content.
|
||||
* new_id - [REQUIRED] - New Unique identifier of the content.
|
||||
* token - [REQUIRED] - Valid public token.
|
||||
* access_token - [REQUIRED] - Valid access token.
|
||||
*
|
||||
* Returns:
|
||||
* Returns the content object for the content that was just copied.
|
||||
*/
|
||||
app()->put('/api/content/copy', [Content::class, 'copy']);
|
||||
|
||||
/**
|
||||
* Delete content
|
||||
*
|
||||
* endpoint: DELETE /api/content
|
||||
*
|
||||
* Body:
|
||||
* id - [REQUIRED] - Unique identifier of the content.
|
||||
* token - [REQUIRED] - Valid pulbic token.
|
||||
* access_token - [REQUIRED] - Valid access token.
|
||||
*
|
||||
* Returns:
|
||||
* Returns an empty body with HTTP status 204
|
||||
*/
|
||||
app()->delete('/api/content', [Content::class, 'delete']);
|
Reference in New Issue
Block a user