Refactor search

This commit is contained in:
Alma Armas 2020-11-22 10:29:27 +00:00
parent 7314f369af
commit 2ed60763a1
3 changed files with 13 additions and 30 deletions

View file

@ -32,16 +32,10 @@ class Clubs
function find(string $query, int $page = 1, ?int $perPage = NULL): \Traversable
{
$query = '%'.$query.'%';
$perPage = $perPage ?? OPENVK_DEFAULT_PER_PAGE;
foreach($this->clubs->where("name LIKE ? OR about LIKE ?", $query, $query)->page($page, $perPage) as $result)
yield new Club($result);
}
$query = "%$query%";
$result = $this->clubs->where("name LIKE ? OR about LIKE ?", $query, $query);
function getFoundCount(string $query): int
{
$query = '%'.$query.'%';
return sizeof($this->clubs->where("name LIKE ? OR about LIKE ?", $query, $query));
return new Util\EntityStream("Club", $result);
}
use \Nette\SmartObject;

View file

@ -36,21 +36,14 @@ class Users
return $this->toUser($this->users->where("user", $user->getId())->fetch());
}
function find(string $query): \Traversable
function find(string $query): Util\EntityStream
{
$query = "%$query%";
$perPage = $perPage ?? OPENVK_DEFAULT_PER_PAGE;
$result = $this->users->where("CONCAT_WS(' ', first_name, last_name) LIKE ?", $query);
$query = "%$query%";
$result = $this->users->where("CONCAT_WS(' ', first_name, last_name) LIKE ?", $query);
return new Util\EntityStream("User", $result);
}
function getFoundCount(string $query): int
{
$query = "%$query%";
return sizeof($this->users->where("CONCAT_WS(' ', first_name, last_name) LIKE ?", $query));
}
function getStatistics(): object
{
return (object) [

View file

@ -27,16 +27,12 @@ final class SearchPresenter extends OpenVKPresenter
// https://youtu.be/pSAWM5YuXx8
switch($type) {
case "groups":
$iterator = $this->clubs->find($query, $page);
$count = $this->clubs->getFoundCount($query);
break;
case "users":
$iterator = $this->users->find($query)->page($page);
$count = $this->users->find($query)->size();
break;
}
$repos = [ "groups" => "clubs", "users" => "users" ];
$repo = $repos[$type] or $this->throwError(400, "Bad Request", "Invalid search entity $type.");
$results = $this->{$repo}->find($query);
$iterator = $results->page($page);
$count = $results->size();
$this->template->iterator = iterator_to_array($iterator);
$this->template->count = $count;