From 7cb8c2cde1df566fdee27e1ad649c6cb800285aa Mon Sep 17 00:00:00 2001 From: Thorsten Rinne Date: Sun, 30 Jul 2023 12:12:55 +0200 Subject: [PATCH] refactor: moved APIs to controllers --- phpmyfaq/.htaccess | 20 ++++---- phpmyfaq/api.php | 28 ----------- phpmyfaq/src/api-routes.php | 15 ++++++ .../Api/Controller/AttachmentController.php | 41 +++++++++++++++++ .../Api/Controller/CommentController.php | 46 +++++++++++++++++++ .../Api/Controller/NewsController.php | 26 +++++++++++ .../phpMyFAQ/Attachment/AttachmentFactory.php | 6 +-- 7 files changed, 142 insertions(+), 40 deletions(-) create mode 100644 phpmyfaq/src/phpMyFAQ/Api/Controller/AttachmentController.php create mode 100644 phpmyfaq/src/phpMyFAQ/Api/Controller/CommentController.php create mode 100644 phpmyfaq/src/phpMyFAQ/Api/Controller/NewsController.php diff --git a/phpmyfaq/.htaccess b/phpmyfaq/.htaccess index 859b2d7c7c..284479acbf 100644 --- a/phpmyfaq/.htaccess +++ b/phpmyfaq/.htaccess @@ -152,9 +152,9 @@ RewriteRule api/v2.0/searches/popular api/index.php RewriteRule api/v2.0/search api/index.php RewriteRule api/v2.0/tags api/index.php RewriteRule api/v2.0/open-questions api/index.php -RewriteRule api/v2.0/comments/([0-9]+) api.php?action=comments&recordId=$1 [L,QSA] -RewriteRule api/v2.0/attachments/([0-9]+) api.php?action=attachments&recordId=$1 [L,QSA] -RewriteRule api/v2.0/news api.php?action=news [L,QSA] +RewriteRule api/v2.0/comments/([0-9]+) api/index.php +RewriteRule api/v2.0/attachments/([0-9]+) api/index.php +RewriteRule api/v2.0/news api/index.php RewriteRule api/v2.0/login api.php?action=login [L,QSA] RewriteRule api/v2.0/faqs/([0-9]+) api.php?action=faqs&categoryId=$1 [L,QSA] RewriteRule api/v2.0/faqs/popular api.php?action=faqs&filter=popular [L,QSA] @@ -173,9 +173,9 @@ RewriteRule api/v2.1/searches/popular api/index.php RewriteRule api/v2.1/search api/index.php RewriteRule api/v2.1/tags api/index.php RewriteRule api/v2.1/open-questions api/index.php -RewriteRule api/v2.1/comments/([0-9]+) api.php?action=comments&recordId=$1 [L,QSA] -RewriteRule api/v2.1/attachments/([0-9]+) api.php?action=attachments&recordId=$1 [L,QSA] -RewriteRule api/v2.1/news api.php?action=news [L,QSA] +RewriteRule api/v2.1/comments/([0-9]+) api/index.php +RewriteRule api/v2.1/attachments/([0-9]+) api/index.php +RewriteRule api/v2.1/news api/index.php RewriteRule api/v2.1/login api.php?action=login [L,QSA] RewriteRule api/v2.1/faqs/([0-9]+) api.php?action=faqs&categoryId=$1 [L,QSA] RewriteRule api/v2.1/faqs/popular api.php?action=faqs&filter=popular [L,QSA] @@ -200,9 +200,9 @@ RewriteRule api/v2.2/searches/popular api/index.php RewriteRule api/v2.2/search api/index.php RewriteRule api/v2.2/tags api/index.php RewriteRule api/v2.2/open-questions api/index.php -RewriteRule api/v2.2/comments/([0-9]+) api.php?action=comments&recordId=$1 [L,QSA] -RewriteRule api/v2.2/attachments/([0-9]+) api.php?action=attachments&recordId=$1 [L,QSA] -RewriteRule api/v2.2/news api.php?action=news [L,QSA] +RewriteRule api/v2.2/comments/([0-9]+) api/index.php +RewriteRule api/v2.2/attachments/([0-9]+) api/index.php +RewriteRule api/v2.2/news api/index.php RewriteRule api/v2.2/login api.php?action=login [L,QSA] RewriteRule api/v2.2/faqs/([0-9]+) api.php?action=faqs&categoryId=$1 [L,QSA] RewriteRule api/v2.2/faqs/popular api.php?action=faqs&filter=popular [L,QSA] @@ -216,7 +216,9 @@ RewriteRule api/v2.2/register api.php?action=register [L,QS # REST API v2.3 # * http://[...]/api/v2.3/ +RewriteRule api/v2.3/attachments/([0-9]+) api/index.php RewriteRule api/v2.3/categories api/index.php +RewriteRule api/v2.3/comments/([0-9]+) api/index.php RewriteRule api/v2.3/groups api/index.php RewriteRule api/v2.3/language api/index.php RewriteRule api/v2.3/open-questions api/index.php diff --git a/phpmyfaq/api.php b/phpmyfaq/api.php index d0c506ed1d..120e7b83c7 100644 --- a/phpmyfaq/api.php +++ b/phpmyfaq/api.php @@ -221,34 +221,6 @@ $response->setData($result); break; - case 'comments': - $comment = new Comments($faqConfig); - $result = $comment->getCommentsData($recordId, CommentType::FAQ); - if ((is_countable($result) ? count($result) : 0) === 0) { - $response->setStatusCode(Response::HTTP_NOT_FOUND); - } - $response->setData($result); - break; - - case 'attachments': - $attachments = $result = []; - try { - $attachments = AttachmentFactory::fetchByRecordId($faqConfig, $recordId); - } catch (AttachmentException) { - $result = []; - } - foreach ($attachments as $attachment) { - $result[] = [ - 'filename' => $attachment->getFilename(), - 'url' => $faqConfig->getDefaultUrl() . $attachment->buildUrl(), - ]; - } - if (count($result) === 0) { - $response->setStatusCode(Response::HTTP_NOT_FOUND); - } - $response->setData($result); - break; - case 'news': $news = new News($faqConfig); $result = $news->getLatestData(false, true, true); diff --git a/phpmyfaq/src/api-routes.php b/phpmyfaq/src/api-routes.php index 7d34486091..5cc4fa2531 100644 --- a/phpmyfaq/src/api-routes.php +++ b/phpmyfaq/src/api-routes.php @@ -15,9 +15,12 @@ * @since 2023-07-29 */ +use phpMyFAQ\Api\Controller\AttachmentController; use phpMyFAQ\Api\Controller\CategoryController; +use phpMyFAQ\Api\Controller\CommentController; use phpMyFAQ\Api\Controller\GroupController; use phpMyFAQ\Api\Controller\LanguageController; +use phpMyFAQ\Api\Controller\NewsController; use phpMyFAQ\Api\Controller\OpenQuestionController; use phpMyFAQ\Api\Controller\SearchController; use phpMyFAQ\Api\Controller\TagController; @@ -33,10 +36,18 @@ $routes = new RouteCollection(); // Public REST API +$routes->add( + 'api.attachments', + new Route("v{$apiVersion}/attachments/{recordId}", ['_controller' => [AttachmentController::class, 'list']]) +); $routes->add( 'api.categories', new Route("v{$apiVersion}/categories", ['_controller' => [CategoryController::class, 'list']]) ); +$routes->add( + 'api.comments', + new Route("v{$apiVersion}/comments/{recordId}", ['_controller' => [CommentController::class, 'list']]) +); $routes->add( 'api.groups', new Route("v{$apiVersion}/groups", ['_controller' => [GroupController::class, 'list']]) @@ -45,6 +56,10 @@ 'api.language', new Route("v{$apiVersion}/language", ['_controller' => [LanguageController::class, 'index']]) ); +$routes->add( + 'api.news', + new Route("v{$apiVersion}/news", ['_controller' => [NewsController::class, 'list']]) +); $routes->add( 'api.open-questions', new Route("v{$apiVersion}/open-questions", ['_controller' => [OpenQuestionController::class, 'list']]) diff --git a/phpmyfaq/src/phpMyFAQ/Api/Controller/AttachmentController.php b/phpmyfaq/src/phpMyFAQ/Api/Controller/AttachmentController.php new file mode 100644 index 0000000000..883029bea5 --- /dev/null +++ b/phpmyfaq/src/phpMyFAQ/Api/Controller/AttachmentController.php @@ -0,0 +1,41 @@ +get('recordId'), FILTER_VALIDATE_INT); + + $attachments = $result = []; + try { + $attachments = AttachmentFactory::fetchByRecordId($faqConfig, $recordId); + } catch (AttachmentException) { + $result = []; + } + foreach ($attachments as $attachment) { + $result[] = [ + 'filename' => $attachment->getFilename(), + 'url' => $faqConfig->getDefaultUrl() . $attachment->buildUrl(), + ]; + } + if (count($result) === 0) { + $response->setStatusCode(Response::HTTP_NOT_FOUND); + } + $response->setData($result); + + return $response; + } +} diff --git a/phpmyfaq/src/phpMyFAQ/Api/Controller/CommentController.php b/phpmyfaq/src/phpMyFAQ/Api/Controller/CommentController.php new file mode 100644 index 0000000000..cc2a03bf47 --- /dev/null +++ b/phpmyfaq/src/phpMyFAQ/Api/Controller/CommentController.php @@ -0,0 +1,46 @@ + + * @copyright 2023 phpMyFAQ Team + * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 + * @link https://www.phpmyfaq.de + * @since 2023-07-30 + */ + +namespace phpMyFAQ\Api\Controller; + +use phpMyFAQ\Comments; +use phpMyFAQ\Configuration; +use phpMyFAQ\Entity\CommentType; +use phpMyFAQ\Filter; +use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; + +class CommentController +{ + public function list(Request $request): JsonResponse + { + $response = new JsonResponse(); + $faqConfig = Configuration::getConfigurationInstance(); + + $recordId = Filter::filterVar($request->get('recordId'), FILTER_VALIDATE_INT); + + $comment = new Comments($faqConfig); + $result = $comment->getCommentsData($recordId, CommentType::FAQ); + if ((is_countable($result) ? count($result) : 0) === 0) { + $response->setStatusCode(Response::HTTP_NOT_FOUND); + } + $response->setData($result); + + return $response; + } +} diff --git a/phpmyfaq/src/phpMyFAQ/Api/Controller/NewsController.php b/phpmyfaq/src/phpMyFAQ/Api/Controller/NewsController.php new file mode 100644 index 0000000000..6ab8a25dbf --- /dev/null +++ b/phpmyfaq/src/phpMyFAQ/Api/Controller/NewsController.php @@ -0,0 +1,26 @@ +getLatestData(false, true, true); + if ((is_countable($result) ? count($result) : 0) === 0) { + $response->setStatusCode(Response::HTTP_NOT_FOUND); + } + $response->setData($result); + + return $response; + } +} diff --git a/phpmyfaq/src/phpMyFAQ/Attachment/AttachmentFactory.php b/phpmyfaq/src/phpMyFAQ/Attachment/AttachmentFactory.php index c7cd8e7e54..0ff2def270 100644 --- a/phpmyfaq/src/phpMyFAQ/Attachment/AttachmentFactory.php +++ b/phpmyfaq/src/phpMyFAQ/Attachment/AttachmentFactory.php @@ -38,7 +38,7 @@ class AttachmentFactory /** * Storage type. */ - private static ?int $storageType = null; + private static ?int $storageType = 0; /** * File encryption is enabled. @@ -100,12 +100,12 @@ public static function fetchByRecordId(Configuration $config, int $recordId): ar ); $result = $config->getDb()->fetchAll($config->getDb()->query($sql)); + if (!empty($result)) { foreach ($result as $item) { - $files[] = self::create($item->id); + $files[] = self::create((int) $item->id); } } - reset($files); return $files; }