From d9460c2dbc732a1eae52629f842d732da52b526d Mon Sep 17 00:00:00 2001 From: Awilum Date: Tue, 10 Aug 2021 17:45:45 +0300 Subject: [PATCH] feat(endpoints): update Content Endpoints #565 --- src/flextype/core/Endpoints/Content.php | 406 ++++++++++++++++++++++ src/flextype/flextype.php | 6 +- src/flextype/routes/endpoints/content.php | 106 ++++++ 3 files changed, 515 insertions(+), 3 deletions(-) create mode 100644 src/flextype/core/Endpoints/Content.php create mode 100644 src/flextype/routes/endpoints/content.php diff --git a/src/flextype/core/Endpoints/Content.php b/src/flextype/core/Endpoints/Content.php new file mode 100644 index 00000000..118c3758 --- /dev/null +++ b/src/flextype/core/Endpoints/Content.php @@ -0,0 +1,406 @@ +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); + } +} diff --git a/src/flextype/flextype.php b/src/flextype/flextype.php index dad8fa86..7591374e 100644 --- a/src/flextype/flextype.php +++ b/src/flextype/flextype.php @@ -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 // diff --git a/src/flextype/routes/endpoints/content.php b/src/flextype/routes/endpoints/content.php new file mode 100644 index 00000000..0723a69f --- /dev/null +++ b/src/flextype/routes/endpoints/content.php @@ -0,0 +1,106 @@ +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']);