Add new POST route /msearch with associated controller

This commit is contained in:
Hypolite Petovan 2018-12-23 21:22:52 -05:00
parent dc67cff8ef
commit 8db05cfa7c
3 changed files with 88 additions and 0 deletions

View file

@ -0,0 +1,68 @@
<?php
namespace Friendica\Directory\Controllers\Api;
use Friendica\Directory\Content\Pager;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
/**
* @author Hypolite Petovan <mrpetovan@gmail.com>
*/
class MatchSearch
{
/**
* @var \Atlas\Pdo\Connection
*/
private $atlas;
/**
* @var \Friendica\Directory\Models\Profile
*/
private $profileModel;
/**
* @var \Gettext\TranslatorInterface
*/
private $l10n;
public function __construct(
\Atlas\Pdo\Connection $atlas,
\Friendica\Directory\Models\Profile $profileModel,
\Gettext\TranslatorInterface $l10n
)
{
$this->atlas = $atlas;
$this->profileModel = $profileModel;
$this->l10n = $l10n;
}
public function render(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args): \Slim\Http\Response
{
$perpage = filter_input(INPUT_POST, 'n', FILTER_SANITIZE_NUMBER_INT);
$query = filter_input(INPUT_POST, 's', FILTER_SANITIZE_STRING);
if (!$perpage) {
$perpage = 80;
}
$pager = new Pager($this->l10n, $request, $perpage);
$pager->setPage(filter_input(INPUT_POST, 'p', FILTER_SANITIZE_NUMBER_INT));
$sql_where = "MATCH (p.`tags`) AGAINST (:query)";
$values = ['query' => $query];
$profiles = $this->profileModel->getListForDisplay($pager->getItemsPerPage(), $pager->getStart(), $sql_where, $values);
$count = $this->profileModel->getCountForDisplay($sql_where, $values);
$vars = [
'query' => $query,
'page' => $pager->getPage(),
'itemsperpage' => $pager->getItemsPerPage(),
'count' => $count,
'profiles' => $profiles
];
return $response->withJson($vars);
}
}

View file

@ -0,0 +1,18 @@
<?php
namespace Friendica\Directory\Routes\Http;
/**
* @author Hypolite Petovan <mrpetovan@gmail.com>
*/
class MatchSearch extends BaseRoute
{
public function __invoke(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args): \Slim\Http\Response
{
return (new \Friendica\Directory\Controllers\Api\MatchSearch(
$this->container->atlas,
$this->container->get('\Friendica\Directory\Models\Profile'),
$this->container->l10n
))->render($request, $response, $args);
}
}

View file

@ -21,6 +21,8 @@ $app->get('/search[/{account_type}]', function (Request $request, Response $resp
return $route($request, $response, $args); return $route($request, $response, $args);
})->setName('search'); })->setName('search');
$app->post('/msearch', \Friendica\Directory\Routes\Http\MatchSearch::class);
$app->get('/stats', \Friendica\Directory\Routes\Web\Statistics::class); $app->get('/stats', \Friendica\Directory\Routes\Web\Statistics::class);
$app->get('/submit', \Friendica\Directory\Routes\Http\Submit::class); $app->get('/submit', \Friendica\Directory\Routes\Http\Submit::class);