Skip to content

Commit

Permalink
refactor: moved APIs to controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Jul 30, 2023
1 parent f10f371 commit 7cb8c2c
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 40 deletions.
20 changes: 11 additions & 9 deletions phpmyfaq/.htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -216,7 +216,9 @@ RewriteRule api/v2.2/register api.php?action=register [L,QS

# REST API v2.3
# * http://[...]/api/v2.3/<ACTION>
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
Expand Down
28 changes: 0 additions & 28 deletions phpmyfaq/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions phpmyfaq/src/api-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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']])
Expand All @@ -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']])
Expand Down
41 changes: 41 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Api/Controller/AttachmentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace phpMyFAQ\Api\Controller;

use phpMyFAQ\Attachment\AttachmentException;
use phpMyFAQ\Attachment\AttachmentFactory;
use phpMyFAQ\Configuration;
use phpMyFAQ\Filter;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class AttachmentController
{
public function list(Request $request): JsonResponse
{
$response = new JsonResponse();
$faqConfig = Configuration::getConfigurationInstance();

$recordId = Filter::filterVar($request->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;
}
}
46 changes: 46 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Api/Controller/CommentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/**
* The Comemnt Controller for the REST API
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*
* @package phpMyFAQ
* @author Thorsten Rinne <thorsten@phpmyfaq.de>
* @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;
}
}
26 changes: 26 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Api/Controller/NewsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace phpMyFAQ\Api\Controller;

use phpMyFAQ\Configuration;
use phpMyFAQ\News;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

class NewsController
{
public function list(): JsonResponse
{
$response = new JsonResponse();
$faqConfig = Configuration::getConfigurationInstance();

$news = new News($faqConfig);
$result = $news->getLatestData(false, true, true);
if ((is_countable($result) ? count($result) : 0) === 0) {
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}
$response->setData($result);

return $response;
}
}
6 changes: 3 additions & 3 deletions phpmyfaq/src/phpMyFAQ/Attachment/AttachmentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class AttachmentFactory
/**
* Storage type.
*/
private static ?int $storageType = null;
private static ?int $storageType = 0;

/**
* File encryption is enabled.
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 7cb8c2c

Please sign in to comment.