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 function find(string $query, int $page = 1, ?int $perPage = NULL): \Traversable
{ {
$query = '%'.$query.'%'; $query = "%$query%";
$perPage = $perPage ?? OPENVK_DEFAULT_PER_PAGE; $result = $this->clubs->where("name LIKE ? OR about LIKE ?", $query, $query);
foreach($this->clubs->where("name LIKE ? OR about LIKE ?", $query, $query)->page($page, $perPage) as $result)
yield new Club($result);
}
function getFoundCount(string $query): int return new Util\EntityStream("Club", $result);
{
$query = '%'.$query.'%';
return sizeof($this->clubs->where("name LIKE ? OR about LIKE ?", $query, $query));
} }
use \Nette\SmartObject; use \Nette\SmartObject;

View file

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

View file

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