Skip to content

Commit 5a83780

Browse files
committed
add only parameter for collection and save search
1 parent 9376f6f commit 5a83780

17 files changed

+305
-268
lines changed

src/Ushahidi/Modules/V5/Actions/Collection/Handlers/FetchCollectionByIdQueryHandler.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ protected function isSupported(Query $query)
3333
public function __invoke($query) //: array
3434
{
3535
$this->isSupported($query);
36-
return $this->collection_repository->findById($query->getId());
36+
return $this->collection_repository->findById(
37+
$query->getId(),
38+
false,
39+
array_unique(array_merge(
40+
$query->getFields(),
41+
$query->getFieldsForRelationship()
42+
)),
43+
$query->getWithRelationship()
44+
);
3745
}
3846
}

src/Ushahidi/Modules/V5/Actions/Collection/Handlers/FetchCollectionQueryHandler.php

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -46,41 +46,13 @@ protected function isSupported(Query $query)
4646
*/
4747
public function __invoke($query) //: LengthAwarePaginator
4848
{
49-
// TODO: This is redundant, we should be able to remove this
5049
$this->isSupported($query);
51-
52-
$search_fields = $query->getSearchData();
53-
54-
$search = new SearchData();
55-
56-
// TODO: Move this to the Query class
57-
$user = Auth::guard()->user();
58-
59-
$search->setFilter('is_saved_search', false);
60-
$search->setFilter('with_post_count', true);
61-
62-
// Querying Values
63-
$search->setFilter('keyword', $search_fields->q());
64-
$search->setFilter('role', $search_fields->role());
65-
$search->setFilter('is_admin', $search_fields->role() === "admin");
66-
$search->setFilter('user_id', $user->id ?? null);
67-
68-
// Paging Values
69-
$limit = $query->getLimit();
70-
71-
$search->setFilter('limit', $limit);
72-
$search->setFilter('skip', $limit * ($query->getPage() - 1));
73-
74-
// Sorting Values
75-
$search->setFilter('sort', $query->getSortBy());
76-
$search->setFilter('order', $query->getOrder());
77-
78-
$this->collection_repository->setSearchParams($search);
79-
80-
// TODO: We shouldn't let the repository return a Laravel paginator instance,
81-
// this should be created in the controller
82-
$result = $this->collection_repository->fetch();
83-
84-
return $result;
50+
$only_fields = array_unique(array_merge($query->getFields(), $query->getFieldsForRelationship()));
51+
return $this->collection_repository->paginate(
52+
$query->getPaging(),
53+
$query->getSearchFields(),
54+
$only_fields,
55+
$query->getWithRelationship()
56+
);
8557
}
8658
}

src/Ushahidi/Modules/V5/Actions/Collection/Queries/FetchCollectionByIdQuery.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
namespace Ushahidi\Modules\V5\Actions\Collection\Queries;
44

55
use App\Bus\Query\Query;
6+
use Ushahidi\Modules\V5\Traits\OnlyParameter\QueryWithOnlyParameter;
7+
use Illuminate\Http\Request;
8+
use Ushahidi\Modules\V5\Models\Set as CollectionModel;
69

710
class FetchCollectionByIdQuery implements Query
811
{
9-
12+
use QueryWithOnlyParameter;
1013

1114
/**
1215
* int
@@ -15,10 +18,24 @@ class FetchCollectionByIdQuery implements Query
1518

1619
public function __construct(int $id = 0)
1720
{
18-
1921
$this->id = $id;
2022
}
2123

24+
public static function fromRequest(int $id, Request $request): self
25+
{
26+
if ($id <= 0) {
27+
throw new \InvalidArgumentException('Id must be a positive number');
28+
}
29+
$query = new self($id);
30+
$query->addOnlyParameteresFromRequest(
31+
$request,
32+
CollectionModel::COLLECTION_ALLOWED_FIELDS,
33+
CollectionModel::ALLOWED_RELATIONSHIPS,
34+
CollectionModel::REQUIRED_FIELDS
35+
);
36+
return $query;
37+
}
38+
2239
public function getId(): int
2340
{
2441
return $this->id;

src/Ushahidi/Modules/V5/Actions/Collection/Queries/FetchCollectionQuery.php

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,33 @@
44

55
use App\Bus\Query\Query;
66
use Ushahidi\Modules\V5\DTO\CollectionSearchFields;
7+
use Ushahidi\Modules\V5\Traits\OnlyParameter\QueryWithOnlyParameter;
8+
use Ushahidi\Modules\V5\Traits\HasPaginate;
9+
use Ushahidi\Modules\V5\Traits\HasSearchFields;
10+
use Illuminate\Http\Request;
11+
use Ushahidi\Modules\V5\Models\Set as CollectionModel;
712

813
class FetchCollectionQuery implements Query
914
{
15+
use QueryWithOnlyParameter;
16+
use HasPaginate;
17+
use HasSearchFields;
18+
1019
const DEFAULT_LIMIT = 0;
1120
const DEFAULT_ORDER = "DESC";
1221
const DEFAULT_SORT_BY = "featured";
1322

14-
private $limit;
15-
private $page;
16-
private $sortBy;
17-
private $order;
18-
private $search_data;
19-
20-
public function __construct(
21-
int $limit,
22-
int $page,
23-
string $sortBy,
24-
string $order,
25-
CollectionSearchFields $search_data
26-
) {
27-
$this->limit = $limit;
28-
$this->page = $page;
29-
$this->sortBy = $sortBy;
30-
$this->order = $order;
31-
$this->search_data = $search_data;
32-
}
33-
34-
public function getLimit(): int
35-
{
36-
return $this->limit > 0 ? $this->limit : config('paging.default_laravel_pageing_limit');
37-
}
38-
39-
public function getPage(): int
40-
{
41-
return $this->page;
42-
}
43-
44-
public function getSortBy(): string
45-
{
46-
return $this->sortBy;
47-
}
48-
49-
public function getOrder(): string
50-
{
51-
return $this->order;
52-
}
53-
54-
public function getSearchData()
23+
public static function fromRequest(Request $request): self
5524
{
56-
return $this->search_data;
25+
$query = new self();
26+
$query->setPaging($request, self::DEFAULT_SORT_BY, self::DEFAULT_ORDER, self::DEFAULT_LIMIT);
27+
$query->setSearchFields(new CollectionSearchFields($request));
28+
$query->addOnlyParameteresFromRequest(
29+
$request,
30+
CollectionModel::COLLECTION_ALLOWED_FIELDS,
31+
CollectionModel::ALLOWED_RELATIONSHIPS,
32+
CollectionModel::REQUIRED_FIELDS
33+
);
34+
return $query;
5735
}
5836
}

src/Ushahidi/Modules/V5/Actions/SavedSearch/Handlers/FetchSavedSearchByIdQueryHandler.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,21 @@ protected function isSupported(Query $query)
2525
);
2626
}
2727

28-
2928
/**
3029
* @param FetchSavedSearchByIdQuery $query
3130
* @return array
3231
*/
3332
public function __invoke($query) //: array
3433
{
3534
$this->isSupported($query);
36-
return $this->saved_search_repository->findById($query->getId(), 1);
35+
return $this->saved_search_repository->findById(
36+
$query->getId(),
37+
1,
38+
array_unique(array_merge(
39+
$query->getFields(),
40+
$query->getFieldsForRelationship()
41+
)),
42+
$query->getWithRelationship()
43+
);
3744
}
3845
}

src/Ushahidi/Modules/V5/Actions/SavedSearch/Handlers/FetchSavedSearchQueryHandler.php

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
use App\Bus\Query\Query;
77
use Ushahidi\Modules\V5\Actions\SavedSearch\Queries\FetchSavedSearchQuery;
88
use Ushahidi\Modules\V5\Repository\Set\SetRepository as SavedSearchRepository;
9-
use Ushahidi\Core\Tool\SearchData;
10-
use Illuminate\Support\Facades\Auth;
119

1210
class FetchSavedSearchQueryHandler extends AbstractQueryHandler
1311
{
@@ -33,32 +31,12 @@ protected function isSupported(Query $query)
3331
public function __invoke($query) //: LengthAwarePaginator
3432
{
3533
$this->isSupported($query);
36-
37-
$search_fields = $query->getSearchData();
38-
39-
$search = new SearchData();
40-
$user = Auth::guard()->user();
41-
42-
$search->setFilter('keyword', $search_fields->q());
43-
$search->setFilter('role', $search_fields->role());
44-
$search->setFilter('is_admin', $search_fields->role() == "admin");
45-
// $search->setFilter('is_guest', !Auth::user() || !Auth::user()->id);
46-
// $search->setFilter('is_me_only', $search_fields->public());
47-
$search->setFilter('user_id', $user->id ?? null);
48-
49-
$search->setFilter('is_saved_search', true);
50-
51-
// Paging Values
52-
$limit = $query->getLimit() ?? config('paging.default_laravel_pageing_limit');
53-
$search->setFilter('limit', $limit);
54-
$search->setFilter('skip', $limit * ($query->getPage() - 1));
55-
56-
// Sorting Values
57-
$search->setFilter('sort', $query->getSortBy());
58-
$search->setFilter('order', $query->getOrder());
59-
60-
$this->saved_search_repository->setSearchParams($search);
61-
62-
return $this->saved_search_repository->fetch();
34+
$only_fields = array_unique(array_merge($query->getFields(), $query->getFieldsForRelationship()));
35+
return $this->saved_search_repository->paginate(
36+
$query->getPaging(),
37+
$query->getSearchFields(),
38+
$only_fields,
39+
$query->getWithRelationship()
40+
);
6341
}
6442
}

src/Ushahidi/Modules/V5/Actions/SavedSearch/Queries/FetchSavedSearchByIdQuery.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
namespace Ushahidi\Modules\V5\Actions\SavedSearch\Queries;
44

55
use App\Bus\Query\Query;
6+
use Ushahidi\Modules\V5\Traits\OnlyParameter\QueryWithOnlyParameter;
7+
use Illuminate\Http\Request;
8+
use Ushahidi\Modules\V5\Models\Set as SavedSearch;
69

710
class FetchSavedSearchByIdQuery implements Query
811
{
9-
12+
use QueryWithOnlyParameter;
1013

1114
/**
1215
* int
@@ -15,10 +18,19 @@ class FetchSavedSearchByIdQuery implements Query
1518

1619
public function __construct(int $id = 0)
1720
{
18-
1921
$this->id = $id;
2022
}
2123

24+
public static function fromRequest(int $id, Request $request): self
25+
{
26+
if ($id <= 0) {
27+
throw new \InvalidArgumentException('Id must be a positive number');
28+
}
29+
$query = new self($id);
30+
$query->addOnlyParameteresFromRequest($request, SavedSearch::ALLOWED_FIELDS, SavedSearch::ALLOWED_RELATIONSHIPS, SavedSearch::REQUIRED_FIELDS);
31+
return $query;
32+
}
33+
2234
public function getId(): int
2335
{
2436
return $this->id;

src/Ushahidi/Modules/V5/Actions/SavedSearch/Queries/FetchSavedSearchQuery.php

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,28 @@
44

55
use App\Bus\Query\Query;
66
use Ushahidi\Modules\V5\DTO\SavedSearchSearchFields;
7+
use Ushahidi\Modules\V5\Traits\OnlyParameter\QueryWithOnlyParameter;
8+
use Ushahidi\Modules\V5\Traits\HasPaginate;
9+
use Ushahidi\Modules\V5\Traits\HasSearchFields;
10+
use Illuminate\Http\Request;
11+
use Ushahidi\Modules\V5\Models\Set;
712

813
class FetchSavedSearchQuery implements Query
914
{
15+
use QueryWithOnlyParameter;
16+
use HasPaginate;
17+
use HasSearchFields;
18+
1019
const DEFAULT_LIMIT = 0;
1120
const DEFAULT_ORDER = "ASC";
1221
const DEFAULT_SORT_BY = "id";
1322

14-
private $limit;
15-
private $page;
16-
private $sortBy;
17-
private $order;
18-
private $search_data;
19-
20-
public function __construct(
21-
int $limit,
22-
int $page,
23-
string $sortBy,
24-
string $order,
25-
SavedSearchSearchFields $search_data
26-
) {
27-
$this->limit = $limit;
28-
$this->page = $page;
29-
$this->sortBy = $sortBy;
30-
$this->order = $order;
31-
$this->search_data = $search_data;
32-
}
33-
34-
public function getLimit(): int
35-
{
36-
return $this->limit;
37-
}
38-
39-
public function getPage(): int
40-
{
41-
return $this->page;
42-
}
43-
44-
public function getSortBy(): string
45-
{
46-
return $this->sortBy;
47-
}
48-
49-
public function getOrder(): string
50-
{
51-
return $this->order;
52-
}
53-
54-
public function getSearchData()
23+
public static function fromRequest(Request $request): self
5524
{
56-
return $this->search_data;
25+
$query = new self();
26+
$query->setPaging($request, self::DEFAULT_SORT_BY, self::DEFAULT_ORDER, self::DEFAULT_LIMIT);
27+
$query->setSearchFields(new SavedSearchSearchFields($request));
28+
$query->addOnlyParameteresFromRequest($request, Set::ALLOWED_FIELDS, Set::ALLOWED_RELATIONSHIPS, Set::REQUIRED_FIELDS);
29+
return $query;
5730
}
5831
}

0 commit comments

Comments
 (0)