From 2ed60763a13b41700d2503cd8ed29d49d39d520d Mon Sep 17 00:00:00 2001 From: Alma Armas Date: Sun, 22 Nov 2020 10:29:27 +0000 Subject: [PATCH] Refactor search --- Web/Models/Repositories/Clubs.php | 14 ++++---------- Web/Models/Repositories/Users.php | 13 +++---------- Web/Presenters/SearchPresenter.php | 16 ++++++---------- 3 files changed, 13 insertions(+), 30 deletions(-) diff --git a/Web/Models/Repositories/Clubs.php b/Web/Models/Repositories/Clubs.php index 7e45760f..17ae58f2 100644 --- a/Web/Models/Repositories/Clubs.php +++ b/Web/Models/Repositories/Clubs.php @@ -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); - } - - function getFoundCount(string $query): int - { - $query = '%'.$query.'%'; - return sizeof($this->clubs->where("name LIKE ? OR about LIKE ?", $query, $query)); + $query = "%$query%"; + $result = $this->clubs->where("name LIKE ? OR about LIKE ?", $query, $query); + + return new Util\EntityStream("Club", $result); } use \Nette\SmartObject; diff --git a/Web/Models/Repositories/Users.php b/Web/Models/Repositories/Users.php index d7ca6a97..0beefdb1 100644 --- a/Web/Models/Repositories/Users.php +++ b/Web/Models/Repositories/Users.php @@ -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) [ diff --git a/Web/Presenters/SearchPresenter.php b/Web/Presenters/SearchPresenter.php index 58f98bd4..6af05712 100644 --- a/Web/Presenters/SearchPresenter.php +++ b/Web/Presenters/SearchPresenter.php @@ -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;