diff --git a/src/Ushahidi/Modules/V5/Actions/Collection/Handlers/FetchCollectionByIdQueryHandler.php b/src/Ushahidi/Modules/V5/Actions/Collection/Handlers/FetchCollectionByIdQueryHandler.php index 4a733b0325..80f93a8962 100644 --- a/src/Ushahidi/Modules/V5/Actions/Collection/Handlers/FetchCollectionByIdQueryHandler.php +++ b/src/Ushahidi/Modules/V5/Actions/Collection/Handlers/FetchCollectionByIdQueryHandler.php @@ -33,6 +33,14 @@ protected function isSupported(Query $query) public function __invoke($query) //: array { $this->isSupported($query); - return $this->collection_repository->findById($query->getId()); + return $this->collection_repository->findById( + $query->getId(), + false, + array_unique(array_merge( + $query->getFields(), + $query->getFieldsForRelationship() + )), + $query->getWithRelationship() + ); } } diff --git a/src/Ushahidi/Modules/V5/Actions/Collection/Handlers/FetchCollectionQueryHandler.php b/src/Ushahidi/Modules/V5/Actions/Collection/Handlers/FetchCollectionQueryHandler.php index af0a981e33..1679e6ec57 100644 --- a/src/Ushahidi/Modules/V5/Actions/Collection/Handlers/FetchCollectionQueryHandler.php +++ b/src/Ushahidi/Modules/V5/Actions/Collection/Handlers/FetchCollectionQueryHandler.php @@ -46,41 +46,13 @@ protected function isSupported(Query $query) */ public function __invoke($query) //: LengthAwarePaginator { - // TODO: This is redundant, we should be able to remove this $this->isSupported($query); - - $search_fields = $query->getSearchData(); - - $search = new SearchData(); - - // TODO: Move this to the Query class - $user = Auth::guard()->user(); - - $search->setFilter('is_saved_search', false); - $search->setFilter('with_post_count', true); - - // Querying Values - $search->setFilter('keyword', $search_fields->q()); - $search->setFilter('role', $search_fields->role()); - $search->setFilter('is_admin', $search_fields->role() === "admin"); - $search->setFilter('user_id', $user->id ?? null); - - // Paging Values - $limit = $query->getLimit(); - - $search->setFilter('limit', $limit); - $search->setFilter('skip', $limit * ($query->getPage() - 1)); - - // Sorting Values - $search->setFilter('sort', $query->getSortBy()); - $search->setFilter('order', $query->getOrder()); - - $this->collection_repository->setSearchParams($search); - - // TODO: We shouldn't let the repository return a Laravel paginator instance, - // this should be created in the controller - $result = $this->collection_repository->fetch(); - - return $result; + $only_fields = array_unique(array_merge($query->getFields(), $query->getFieldsForRelationship())); + return $this->collection_repository->paginate( + $query->getPaging(), + $query->getSearchFields(), + $only_fields, + $query->getWithRelationship() + ); } } diff --git a/src/Ushahidi/Modules/V5/Actions/Collection/Queries/FetchCollectionByIdQuery.php b/src/Ushahidi/Modules/V5/Actions/Collection/Queries/FetchCollectionByIdQuery.php index 1d0574da13..f351410e70 100644 --- a/src/Ushahidi/Modules/V5/Actions/Collection/Queries/FetchCollectionByIdQuery.php +++ b/src/Ushahidi/Modules/V5/Actions/Collection/Queries/FetchCollectionByIdQuery.php @@ -3,10 +3,13 @@ namespace Ushahidi\Modules\V5\Actions\Collection\Queries; use App\Bus\Query\Query; +use Ushahidi\Modules\V5\Traits\OnlyParameter\QueryWithOnlyParameter; +use Illuminate\Http\Request; +use Ushahidi\Modules\V5\Models\Set as CollectionModel; class FetchCollectionByIdQuery implements Query { - + use QueryWithOnlyParameter; /** * int @@ -15,10 +18,24 @@ class FetchCollectionByIdQuery implements Query public function __construct(int $id = 0) { - $this->id = $id; } + public static function fromRequest(int $id, Request $request): self + { + if ($id <= 0) { + throw new \InvalidArgumentException('Id must be a positive number'); + } + $query = new self($id); + $query->addOnlyParameteresFromRequest( + $request, + CollectionModel::COLLECTION_ALLOWED_FIELDS, + CollectionModel::ALLOWED_RELATIONSHIPS, + CollectionModel::REQUIRED_FIELDS + ); + return $query; + } + public function getId(): int { return $this->id; diff --git a/src/Ushahidi/Modules/V5/Actions/Collection/Queries/FetchCollectionQuery.php b/src/Ushahidi/Modules/V5/Actions/Collection/Queries/FetchCollectionQuery.php index 744192ad0c..63f9766c9c 100644 --- a/src/Ushahidi/Modules/V5/Actions/Collection/Queries/FetchCollectionQuery.php +++ b/src/Ushahidi/Modules/V5/Actions/Collection/Queries/FetchCollectionQuery.php @@ -4,55 +4,33 @@ use App\Bus\Query\Query; use Ushahidi\Modules\V5\DTO\CollectionSearchFields; +use Ushahidi\Modules\V5\Traits\OnlyParameter\QueryWithOnlyParameter; +use Ushahidi\Modules\V5\Traits\HasPaginate; +use Ushahidi\Modules\V5\Traits\HasSearchFields; +use Illuminate\Http\Request; +use Ushahidi\Modules\V5\Models\Set as CollectionModel; class FetchCollectionQuery implements Query { + use QueryWithOnlyParameter; + use HasPaginate; + use HasSearchFields; + const DEFAULT_LIMIT = 0; const DEFAULT_ORDER = "DESC"; const DEFAULT_SORT_BY = "featured"; - private $limit; - private $page; - private $sortBy; - private $order; - private $search_data; - - public function __construct( - int $limit, - int $page, - string $sortBy, - string $order, - CollectionSearchFields $search_data - ) { - $this->limit = $limit; - $this->page = $page; - $this->sortBy = $sortBy; - $this->order = $order; - $this->search_data = $search_data; - } - - public function getLimit(): int - { - return $this->limit > 0 ? $this->limit : config('paging.default_laravel_pageing_limit'); - } - - public function getPage(): int - { - return $this->page; - } - - public function getSortBy(): string - { - return $this->sortBy; - } - - public function getOrder(): string - { - return $this->order; - } - - public function getSearchData() + public static function fromRequest(Request $request): self { - return $this->search_data; + $query = new self(); + $query->setPaging($request, self::DEFAULT_SORT_BY, self::DEFAULT_ORDER, self::DEFAULT_LIMIT); + $query->setSearchFields(new CollectionSearchFields($request)); + $query->addOnlyParameteresFromRequest( + $request, + CollectionModel::COLLECTION_ALLOWED_FIELDS, + CollectionModel::ALLOWED_RELATIONSHIPS, + CollectionModel::REQUIRED_FIELDS + ); + return $query; } } diff --git a/src/Ushahidi/Modules/V5/Actions/Post/HandlePostOnlyParameters.php b/src/Ushahidi/Modules/V5/Actions/Post/HandlePostOnlyParameters.php index 30a981e401..447cd51c6c 100644 --- a/src/Ushahidi/Modules/V5/Actions/Post/HandlePostOnlyParameters.php +++ b/src/Ushahidi/Modules/V5/Actions/Post/HandlePostOnlyParameters.php @@ -83,12 +83,17 @@ public function addHydrateRelationships(Post $post, array $hydrates) $relations['enabled_languages'] = true; break; case 'post_media': - $post->post_media = $post->valuesPostMedia; - $post->post_media = $post->post_media->map(function ($media) { - $media = $media->toArray(); - unset($media['post']); // Remove the 'post' property - return $media; - }); + $post->post_media = $post->valuesPostMedia->first(); + if ($post->post_media) { + $media = $post->post_media->toArray(); + unset($media['post']); + // convert it to field structure + $media_field = $media['attribute']; + unset($media['attribute']); + unset($media['translations']); + $media_field['value'] = $media; + $post->post_media = $media_field; + } break; } } diff --git a/src/Ushahidi/Modules/V5/Actions/SavedSearch/Handlers/FetchSavedSearchByIdQueryHandler.php b/src/Ushahidi/Modules/V5/Actions/SavedSearch/Handlers/FetchSavedSearchByIdQueryHandler.php index a6234cdc8a..f7773cbf1d 100644 --- a/src/Ushahidi/Modules/V5/Actions/SavedSearch/Handlers/FetchSavedSearchByIdQueryHandler.php +++ b/src/Ushahidi/Modules/V5/Actions/SavedSearch/Handlers/FetchSavedSearchByIdQueryHandler.php @@ -25,7 +25,6 @@ protected function isSupported(Query $query) ); } - /** * @param FetchSavedSearchByIdQuery $query * @return array @@ -33,6 +32,14 @@ protected function isSupported(Query $query) public function __invoke($query) //: array { $this->isSupported($query); - return $this->saved_search_repository->findById($query->getId(), 1); + return $this->saved_search_repository->findById( + $query->getId(), + 1, + array_unique(array_merge( + $query->getFields(), + $query->getFieldsForRelationship() + )), + $query->getWithRelationship() + ); } } diff --git a/src/Ushahidi/Modules/V5/Actions/SavedSearch/Handlers/FetchSavedSearchQueryHandler.php b/src/Ushahidi/Modules/V5/Actions/SavedSearch/Handlers/FetchSavedSearchQueryHandler.php index b1356b9da2..cab373f665 100644 --- a/src/Ushahidi/Modules/V5/Actions/SavedSearch/Handlers/FetchSavedSearchQueryHandler.php +++ b/src/Ushahidi/Modules/V5/Actions/SavedSearch/Handlers/FetchSavedSearchQueryHandler.php @@ -6,8 +6,6 @@ use App\Bus\Query\Query; use Ushahidi\Modules\V5\Actions\SavedSearch\Queries\FetchSavedSearchQuery; use Ushahidi\Modules\V5\Repository\Set\SetRepository as SavedSearchRepository; -use Ushahidi\Core\Tool\SearchData; -use Illuminate\Support\Facades\Auth; class FetchSavedSearchQueryHandler extends AbstractQueryHandler { @@ -33,32 +31,12 @@ protected function isSupported(Query $query) public function __invoke($query) //: LengthAwarePaginator { $this->isSupported($query); - - $search_fields = $query->getSearchData(); - - $search = new SearchData(); - $user = Auth::guard()->user(); - - $search->setFilter('keyword', $search_fields->q()); - $search->setFilter('role', $search_fields->role()); - $search->setFilter('is_admin', $search_fields->role() == "admin"); - // $search->setFilter('is_guest', !Auth::user() || !Auth::user()->id); - // $search->setFilter('is_me_only', $search_fields->public()); - $search->setFilter('user_id', $user->id ?? null); - - $search->setFilter('is_saved_search', true); - - // Paging Values - $limit = $query->getLimit() ?? config('paging.default_laravel_pageing_limit'); - $search->setFilter('limit', $limit); - $search->setFilter('skip', $limit * ($query->getPage() - 1)); - - // Sorting Values - $search->setFilter('sort', $query->getSortBy()); - $search->setFilter('order', $query->getOrder()); - - $this->saved_search_repository->setSearchParams($search); - - return $this->saved_search_repository->fetch(); + $only_fields = array_unique(array_merge($query->getFields(), $query->getFieldsForRelationship())); + return $this->saved_search_repository->paginate( + $query->getPaging(), + $query->getSearchFields(), + $only_fields, + $query->getWithRelationship() + ); } } diff --git a/src/Ushahidi/Modules/V5/Actions/SavedSearch/Queries/FetchSavedSearchByIdQuery.php b/src/Ushahidi/Modules/V5/Actions/SavedSearch/Queries/FetchSavedSearchByIdQuery.php index f359afb652..22e1a87551 100644 --- a/src/Ushahidi/Modules/V5/Actions/SavedSearch/Queries/FetchSavedSearchByIdQuery.php +++ b/src/Ushahidi/Modules/V5/Actions/SavedSearch/Queries/FetchSavedSearchByIdQuery.php @@ -3,10 +3,13 @@ namespace Ushahidi\Modules\V5\Actions\SavedSearch\Queries; use App\Bus\Query\Query; +use Ushahidi\Modules\V5\Traits\OnlyParameter\QueryWithOnlyParameter; +use Illuminate\Http\Request; +use Ushahidi\Modules\V5\Models\Set as SavedSearch; class FetchSavedSearchByIdQuery implements Query { - + use QueryWithOnlyParameter; /** * int @@ -15,10 +18,19 @@ class FetchSavedSearchByIdQuery implements Query public function __construct(int $id = 0) { - $this->id = $id; } + public static function fromRequest(int $id, Request $request): self + { + if ($id <= 0) { + throw new \InvalidArgumentException('Id must be a positive number'); + } + $query = new self($id); + $query->addOnlyParameteresFromRequest($request, SavedSearch::ALLOWED_FIELDS, SavedSearch::ALLOWED_RELATIONSHIPS, SavedSearch::REQUIRED_FIELDS); + return $query; + } + public function getId(): int { return $this->id; diff --git a/src/Ushahidi/Modules/V5/Actions/SavedSearch/Queries/FetchSavedSearchQuery.php b/src/Ushahidi/Modules/V5/Actions/SavedSearch/Queries/FetchSavedSearchQuery.php index ccafc11b2f..2e33d80053 100644 --- a/src/Ushahidi/Modules/V5/Actions/SavedSearch/Queries/FetchSavedSearchQuery.php +++ b/src/Ushahidi/Modules/V5/Actions/SavedSearch/Queries/FetchSavedSearchQuery.php @@ -4,55 +4,28 @@ use App\Bus\Query\Query; use Ushahidi\Modules\V5\DTO\SavedSearchSearchFields; +use Ushahidi\Modules\V5\Traits\OnlyParameter\QueryWithOnlyParameter; +use Ushahidi\Modules\V5\Traits\HasPaginate; +use Ushahidi\Modules\V5\Traits\HasSearchFields; +use Illuminate\Http\Request; +use Ushahidi\Modules\V5\Models\Set; class FetchSavedSearchQuery implements Query { + use QueryWithOnlyParameter; + use HasPaginate; + use HasSearchFields; + const DEFAULT_LIMIT = 0; const DEFAULT_ORDER = "ASC"; const DEFAULT_SORT_BY = "id"; - private $limit; - private $page; - private $sortBy; - private $order; - private $search_data; - - public function __construct( - int $limit, - int $page, - string $sortBy, - string $order, - SavedSearchSearchFields $search_data - ) { - $this->limit = $limit; - $this->page = $page; - $this->sortBy = $sortBy; - $this->order = $order; - $this->search_data = $search_data; - } - - public function getLimit(): int - { - return $this->limit; - } - - public function getPage(): int - { - return $this->page; - } - - public function getSortBy(): string - { - return $this->sortBy; - } - - public function getOrder(): string - { - return $this->order; - } - - public function getSearchData() + public static function fromRequest(Request $request): self { - return $this->search_data; + $query = new self(); + $query->setPaging($request, self::DEFAULT_SORT_BY, self::DEFAULT_ORDER, self::DEFAULT_LIMIT); + $query->setSearchFields(new SavedSearchSearchFields($request)); + $query->addOnlyParameteresFromRequest($request, Set::ALLOWED_FIELDS, Set::ALLOWED_RELATIONSHIPS, Set::REQUIRED_FIELDS); + return $query; } } diff --git a/src/Ushahidi/Modules/V5/DTO/CollectionSearchFields.php b/src/Ushahidi/Modules/V5/DTO/CollectionSearchFields.php index 4b3031196f..56d0cb3a29 100644 --- a/src/Ushahidi/Modules/V5/DTO/CollectionSearchFields.php +++ b/src/Ushahidi/Modules/V5/DTO/CollectionSearchFields.php @@ -11,20 +11,27 @@ class CollectionSearchFields extends SearchFields * @var ?string */ protected $query; - - protected $search; - + protected $is_saved_search; protected $role; + private $name; + private $keyword; + private $user_id; public function __construct(Request $request) { $this->query = $request->query('q'); - $this->search = false; + $this->is_saved_search = 0; + if (Auth::user()) { $this->role = Auth::user()->role; + $this->user_id = Auth::user()->id; } else { $this->role = null; + $this->user_id = null; } + + $this->name = $request->query('name'); + $this->keyword = $request->query('keyword'); } public function q(): ?string @@ -32,13 +39,31 @@ public function q(): ?string return $this->query; } - public function search(): bool + public function isSavedSearch() { - return $this->search; + return $this->is_saved_search; } - public function role(): ?string + public function role() { return $this->role; } + + public function name() + { + return $this->name; + } + + public function keyword() + { + return $this->keyword; + } + public function userId() + { + return $this->user_id; + } + public function isAdmin() + { + return ($this->role === "admin"); + } } diff --git a/src/Ushahidi/Modules/V5/DTO/SavedSearchSearchFields.php b/src/Ushahidi/Modules/V5/DTO/SavedSearchSearchFields.php index e8d1cff52a..a6973cc177 100644 --- a/src/Ushahidi/Modules/V5/DTO/SavedSearchSearchFields.php +++ b/src/Ushahidi/Modules/V5/DTO/SavedSearchSearchFields.php @@ -9,6 +9,6 @@ class SavedSearchSearchFields extends CollectionSearchFields public function __construct(Request $request) { parent::__construct($request); - $this->search = true; + $this->is_saved_search = 1; } } diff --git a/src/Ushahidi/Modules/V5/Http/Controllers/CollectionController.php b/src/Ushahidi/Modules/V5/Http/Controllers/CollectionController.php index 56765b36e6..454695a914 100644 --- a/src/Ushahidi/Modules/V5/Http/Controllers/CollectionController.php +++ b/src/Ushahidi/Modules/V5/Http/Controllers/CollectionController.php @@ -10,7 +10,6 @@ use Ushahidi\Modules\V5\Actions\Collection\Commands\CreateCollectionCommand; use Ushahidi\Modules\V5\Actions\Collection\Commands\UpdateCollectionCommand; use Ushahidi\Modules\V5\Actions\Collection\Commands\DeleteCollectionCommand; -use Ushahidi\Modules\V5\DTO\CollectionSearchFields; use Ushahidi\Core\Entity\Set as CollectionEntity; use Ushahidi\Modules\V5\Requests\CollectionRequest; use Ushahidi\Modules\V5\Models\Set as CollectionModel; @@ -18,7 +17,6 @@ class CollectionController extends V5Controller { - /** * Display the specified resource. * @@ -26,10 +24,11 @@ class CollectionController extends V5Controller * @return mixed * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function show(int $id) + public function show(Request $request, int $id) { - - $collection = $this->queryBus->handle(new FetchCollectionByIdQuery($id)); + $collection = $this->queryBus->handle( + FetchCollectionByIdQuery::fromRequest($id, $request) + ); $this->authorizeAnyone('view', $collection); return new CollectionResource($collection); } //end show() @@ -45,21 +44,32 @@ public function show(int $id) public function index(Request $request) { $this->authorizeAnyone('viewAny', CollectionModel::class); - - $action = new FetchCollectionQuery( - $request->query('limit', FetchCollectionQuery::DEFAULT_LIMIT), - $request->query('page', 1), - $request->query('sortBy', FetchCollectionQuery::DEFAULT_SORT_BY), - $request->query('order', FetchCollectionQuery::DEFAULT_ORDER), - new CollectionSearchFields($request) + $collections = $this->queryBus->handle( + FetchCollectionQuery::fromRequest($request) ); - $collections = $this->queryBus->handle($action); - return new CollectionCollection($collections); } //end index() + private function getCollection(int $id, ?array $fields = null, ?array $haydrates = null) + { + if (!$fields) { + $fields = CollectionModel::ALLOWED_FIELDS; + } + if (!$haydrates) { + $haydrates = array_keys(CollectionModel::ALLOWED_RELATIONSHIPS); + } + $find_collection_query = new FetchCollectionByIdQuery($id); + $find_collection_query->addOnlyValues( + $fields, + $haydrates, + CollectionModel::ALLOWED_RELATIONSHIPS, + CollectionModel::REQUIRED_FIELDS + ); + return $this->queryBus->handle($find_collection_query); + } + /** * Create new Collection. * @@ -70,7 +80,9 @@ public function index(Request $request) public function store(CollectionRequest $request) { $this->authorize('store', CollectionModel::class); + // to do fix the create of this return $this->show( + $request, $this->commandBus->handle( new CreateCollectionCommand( CollectionEntity::buildEntity($request->input()) @@ -81,18 +93,18 @@ public function store(CollectionRequest $request) public function update(int $id, CollectionRequest $request) { - $collection = $this->queryBus->handle(new FetchCollectionByIdQuery($id)); + $collection = $this->getCollection($id, CollectionModel::ALLOWED_FIELDS, []); $collection_entity = CollectionEntity::buildEntity($request->input(), 'update', $collection->toArray()); $new_collection = new CollectionModel($collection_entity->asArray()); $new_collection->id = $collection_entity->id; $this->authorize('update', $new_collection); $this->commandBus->handle(new UpdateCollectionCommand($id, $collection_entity)); - return $this->show($id); + return $this->show($request, $id); } public function delete(int $id) { - $collection = $this->queryBus->handle(new FetchCollectionByIdQuery($id)); + $collection = $this->getCollection($id, ['id'], []); $this->authorize('delete', $collection); $this->commandBus->handle(new DeleteCollectionCommand($id)); return response()->json(['result' => ['deleted' => $id]]); diff --git a/src/Ushahidi/Modules/V5/Http/Controllers/SavedSearchController.php b/src/Ushahidi/Modules/V5/Http/Controllers/SavedSearchController.php index 585cb9f65e..c394269480 100644 --- a/src/Ushahidi/Modules/V5/Http/Controllers/SavedSearchController.php +++ b/src/Ushahidi/Modules/V5/Http/Controllers/SavedSearchController.php @@ -10,7 +10,6 @@ use Ushahidi\Modules\V5\Actions\SavedSearch\Commands\CreateSavedSearchCommand; use Ushahidi\Modules\V5\Actions\SavedSearch\Commands\UpdateSavedSearchCommand; use Ushahidi\Modules\V5\Actions\SavedSearch\Commands\DeleteSavedSearchCommand; -use Ushahidi\Modules\V5\DTO\SavedSearchSearchFields; use Ushahidi\Core\Entity\SavedSearch as SavedSearchEntity; use Ushahidi\Modules\V5\Requests\SavedSearchRequest; use Ushahidi\Modules\V5\Models\Set as SavedSearch; @@ -20,19 +19,19 @@ class SavedSearchController extends V5Controller /** * Display the specified resource. - * + * @param Request $request * @param integer $id * @return mixed * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function show(int $id) + public function show(Request $request, int $id) { - $saved_search = $this->queryBus->handle(new FetchSavedSearchByIdQuery($id)); + $saved_search = $this->queryBus->handle( + FetchSavedSearchByIdQuery::fromRequest($id, $request) + ); return new SavedSearchResource($saved_search); } //end show() - - /** * Display the specified resource. * @@ -41,19 +40,31 @@ public function show(int $id) */ public function index(Request $request) { - $surveys = $this->queryBus->handle( - new FetchSavedSearchQuery( - $request->query('limit', FetchSavedSearchQuery::DEFAULT_LIMIT), - $request->query('page', 1), - $request->query('sortBy', FetchSavedSearchQuery::DEFAULT_SORT_BY), - $request->query('order', FetchSavedSearchQuery::DEFAULT_ORDER), - new SavedSearchSearchFields($request) - ) + $saved_searches = $this->queryBus->handle( + FetchSavedSearchQuery::fromRequest($request) ); - return new SavedSearchCollection($surveys); + return new SavedSearchCollection($saved_searches); } //end index() + private function getSavedSearch(int $id, ?array $fields = null, ?array $haydrates = null) + { + if (!$fields) { + $fields = SavedSearch::ALLOWED_FIELDS; + } + if (!$haydrates) { + $haydrates = array_keys(SavedSearch::ALLOWED_RELATIONSHIPS); + } + $find_saved_search_query = new FetchSavedSearchByIdQuery($id); + $find_saved_search_query->addOnlyValues( + $fields, + $haydrates, + SavedSearch::ALLOWED_RELATIONSHIPS, + SavedSearch::REQUIRED_FIELDS + ); + return $this->queryBus->handle($find_saved_search_query); + } + /** * Create new Saved Search. * @@ -65,6 +76,7 @@ public function store(SavedSearchRequest $request) { $this->authorize('store', SavedSearch::class); return $this->show( + $request, $this->commandBus->handle( new CreateSavedSearchCommand( SavedSearchEntity::buildEntity($request->input()) @@ -75,18 +87,18 @@ public function store(SavedSearchRequest $request) public function update(int $id, SavedSearchRequest $request) { - $saved_search = $this->queryBus->handle(new FetchSavedSearchByIdQuery($id)); + $saved_search = $this->getSavedSearch($id, SavedSearch::ALLOWED_FIELDS, []); $saved_search_entity = SavedSearchEntity::buildEntity($request->input(), 'update', $saved_search->toArray()); $new_saved_search = new SavedSearch($saved_search_entity->asArray()); $new_saved_search->id = $saved_search_entity->id; $this->authorize('update', $new_saved_search); $this->commandBus->handle(new UpdateSavedSearchCommand($id, $saved_search_entity)); - return $this->show($id); + return $this->show($request, $id); } public function delete(int $id) { - $saved_search = $this->queryBus->handle(new FetchSavedSearchByIdQuery($id)); + $saved_search = $this->getSavedSearch($id, ['id'], []); $this->authorize('delete', $saved_search); $this->commandBus->handle(new DeleteSavedSearchCommand($id)); return response()->json(['result' => ['deleted' => $id]]); diff --git a/src/Ushahidi/Modules/V5/Http/Resources/Collection/CollectionResource.php b/src/Ushahidi/Modules/V5/Http/Resources/Collection/CollectionResource.php index 980708369a..e58f3ae663 100644 --- a/src/Ushahidi/Modules/V5/Http/Resources/Collection/CollectionResource.php +++ b/src/Ushahidi/Modules/V5/Http/Resources/Collection/CollectionResource.php @@ -34,19 +34,8 @@ private function getResourcePrivileges() */ public function toArray($request) { - return [ - 'id' => $this->id, - 'name' => $this->name, - 'user_id'=> $this->user_id, - 'description' => $this->description, - 'role' => $this->role, - 'view' => $this->view, - 'view_options' => $this->view_options, - 'featured' => $this->featured, - 'created' => $this->created, - 'updated' => $this->updated, - 'posts_count'=>$this->posts_count, - 'allowed_privileges' => $this->getResourcePrivileges() - ]; + $data = $this->resource->toArray(); + $data['allowed_privileges'] = $this->getResourcePrivileges(); + return $data; } } diff --git a/src/Ushahidi/Modules/V5/Http/Resources/SavedSearch/SavedSearchResource.php b/src/Ushahidi/Modules/V5/Http/Resources/SavedSearch/SavedSearchResource.php index 400ec9d26d..028692ed0a 100644 --- a/src/Ushahidi/Modules/V5/Http/Resources/SavedSearch/SavedSearchResource.php +++ b/src/Ushahidi/Modules/V5/Http/Resources/SavedSearch/SavedSearchResource.php @@ -34,19 +34,8 @@ private function getResourcePrivileges() */ public function toArray($request) { - return [ - 'id' => $this->id, - 'name' => $this->name, - 'user_id'=> $this->user_id, - 'description' => $this->description, - 'role' => $this->role, - 'filter' => $this->filter, - 'view' => $this->view, - 'view_options' => $this->view_options, - 'featured' => $this->featured, - 'created' => $this->created, - 'updated' => $this->updated, - 'allowed_privileges' => $this->getResourcePrivileges() - ]; + $data = $this->resource->toArray(); + $data['allowed_privileges'] = $this->getResourcePrivileges(); + return $data; } } diff --git a/src/Ushahidi/Modules/V5/Models/Post/Post.php b/src/Ushahidi/Modules/V5/Models/Post/Post.php index 8fcff36c62..f7d3c6b119 100644 --- a/src/Ushahidi/Modules/V5/Models/Post/Post.php +++ b/src/Ushahidi/Modules/V5/Models/Post/Post.php @@ -765,7 +765,10 @@ public function valuesPostsMedia() public function valuesPostMedia() { return $this->hasMany('Ushahidi\Modules\V5\Models\PostValues\PostMedia', 'post_id', 'id') - ->select('post_media.*'); + ->select('post_media.*')->with('attribute')->whereHas('attribute', function ($query) { + $query->where('input', 'image') + ->orWhere('input', 'upload'); // for old uploaded images + }); } public function valuesPostsSet() { diff --git a/src/Ushahidi/Modules/V5/Models/Set.php b/src/Ushahidi/Modules/V5/Models/Set.php index 40476ba381..05f478c70b 100644 --- a/src/Ushahidi/Modules/V5/Models/Set.php +++ b/src/Ushahidi/Modules/V5/Models/Set.php @@ -50,6 +50,45 @@ class Set extends BaseModel 'updated' ]; + /** Data used for only parameters + * + */ + + public const REQUIRED_FIELDS = [ + 'id', + ]; + + public const ALLOWED_FIELDS = [ + 'id', + 'user_id', + 'name', + 'description', + 'filter', + 'view', + 'view_options', + 'role', + 'search', + 'featured', + 'created', + 'updated' + ]; + + public const COLLECTION_ALLOWED_FIELDS = [ + 'id', + 'user_id', + 'name', + 'description', + 'view', + 'view_options', + 'role', + 'featured', + 'created', + 'updated' + ]; + + public const ALLOWED_RELATIONSHIPS = [ + 'posts' => ['fields' => [], 'relationships' => ["posts"]], + ]; /** * Return all validation rules * diff --git a/src/Ushahidi/Modules/V5/Repository/Set/EloquentSetRepository.php b/src/Ushahidi/Modules/V5/Repository/Set/EloquentSetRepository.php index 51a26d11b8..50ec72d4f4 100644 --- a/src/Ushahidi/Modules/V5/Repository/Set/EloquentSetRepository.php +++ b/src/Ushahidi/Modules/V5/Repository/Set/EloquentSetRepository.php @@ -10,8 +10,8 @@ use Illuminate\Support\Facades\DB; use Ushahidi\Modules\V5\DTO\CollectionSearchFields; use Ushahidi\Core\Entity\Set as CollectionEntity; -use Illuminate\Support\Facades\Auth; use Ushahidi\Core\Tool\SearchData; +use Ushahidi\Modules\V5\DTO\Paging; class EloquentSetRepository implements SetRepository { @@ -20,51 +20,60 @@ class EloquentSetRepository implements SetRepository */ protected $searchData; + private function addTableNamePrefix($fields) + { + $after_update = []; + foreach ($fields as $field) { + $after_update[] = 'sets.' . $field; + } + return $after_update; + } + /** * This method will fetch all the Set for the logged user from the database utilising * Laravel Eloquent ORM and return them as an array - * @param int $limit - * @param int $skip - * @param string $sortBy - * @param string $order + * @param Paging $limit + * @param CollectionSearchFields $search_fields + * @param array $fields + * @param array $with * * @return Set[]|LengthAwarePaginator */ - public function fetch() - { - $data = $this->searchData; + public function paginate( + Paging $paging, + CollectionSearchFields $search_fields, + array $fields = [], + array $with = [] + ): LengthAwarePaginator { + $fields = $this->addTableNamePrefix($fields); + // add the order field if not found + if (!in_array('sets.'.$paging->getOrderBy(), $fields)) { + $fields[] = 'sets.'.$paging->getOrderBy(); + } + $query = Set::take($paging->getLimit()) + ->orderBy('sets.'.$paging->getOrderBy(), $paging->getOrder()); - $query = $this->setSearchCondition(Set::query(), $data); + $query = $this->setSearchCondition($search_fields, $query); - $sort = $data->getFilter('sort'); - $order = $data->getFilter('order'); - if (isset($sort)) { - $query->orderBy($sort, $order); + if (count($fields)) { + $query->select($fields); } - - if ($data->getFilter('with_post_count') == true) { - $query->withCount('posts'); + if (count($with)) { + $query->with($with); } + $query->distinct(); - $limit = $data->getFilter('limit'); - if (isset($limit)) { - return $query->paginate($limit); - } - return $query->get(); + return $query->paginate($paging->getLimit()); } - - private function setSearchCondition(Builder $builder, SearchData $search_fields) + private function setSearchCondition(CollectionSearchFields $search_fields, $builder) { - $is_saved_search = (int) $search_fields->getFilter('is_saved_search'); - $builder->where('search', '=', $is_saved_search); + $builder->where('search', '=', $search_fields->isSavedSearch()); - $keyword = $search_fields->getFilter('keyword'); - if (isset($keyword) && !empty($keyword)) { - $builder->where('name', 'LIKE', "%" . $keyword . "%"); + if ($search_fields->q()) { + $builder->where('name', 'LIKE', "%" . $search_fields->q() . "%"); } - - $is_admin = $search_fields->getFilter('is_admin'); - if ($is_admin == false) { + + if (!$search_fields->isAdmin()) { // Default search for everyone and guest user $builder->where(function (Builder $query) { $query->whereNull('role'); @@ -73,7 +82,7 @@ private function setSearchCondition(Builder $builder, SearchData $search_fields) }); // is owner - $user_id = $search_fields->getFilter('user_id'); + $user_id = $search_fields->userID(); if (isset($user_id) && !is_null($user_id)) { $builder->orWhere(function (Builder $query) use ($user_id) { $query->where('role', 'LIKE', "%me%") @@ -82,7 +91,7 @@ private function setSearchCondition(Builder $builder, SearchData $search_fields) } // is not admin and has role - $role = $search_fields->getFilter('role'); + $role = $search_fields->role(); if (isset($role) && !is_null($role)) { $builder->orWhere(function (Builder $query) use ($role) { $query->where('role', 'LIKE', "%" . $role . "%"); @@ -104,12 +113,21 @@ public function setSearchParams(SearchData $searchData) * not exist in the database. * @param int $id * @param bool $search + * @param array fields + * @param array with * @return Set * @throws NotFoundException */ - public function findById(int $id, bool $search = false): Set + public function findById(int $id, bool $search = false, array $fields = [], array $with = []): Set { - $set = Set::where('id', '=', $id)->where('search', '=', $search)->first(); + $query = Set::where('id', '=', $id)->where('search', '=', $search); + if (count($fields)) { + $query->select($fields); + } + if (count($with)) { + $query->with($with); + } + $set = $query->first(); if (!$set instanceof Set) { throw new NotFoundException('set not found'); } diff --git a/src/Ushahidi/Modules/V5/Repository/Set/SetRepository.php b/src/Ushahidi/Modules/V5/Repository/Set/SetRepository.php index a4e927e0c0..338a35d756 100644 --- a/src/Ushahidi/Modules/V5/Repository/Set/SetRepository.php +++ b/src/Ushahidi/Modules/V5/Repository/Set/SetRepository.php @@ -7,20 +7,26 @@ use Ushahidi\Core\Exception\NotFoundException; use Ushahidi\Core\Tool\SearchData; use Ushahidi\Modules\V5\DTO\CollectionSearchFields; +use Ushahidi\Modules\V5\DTO\Paging; +use Illuminate\Pagination\LengthAwarePaginator; interface SetRepository { /** * This method will fetch all the Set for the logged user from the database utilising * Laravel Eloquent ORM and return them as an array - * @param int $limit - * @param int $skip - * @param string $sortBy - * @param string $order - * @param bool $search + * @param Paging $limit + * @param CollectionSearchFields $search_fields + * @param array $fields + * @param array $with * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator */ - public function fetch(); + public function paginate( + Paging $paging, + CollectionSearchFields $search_fields, + array $fields = [], + array $with = [] + ): LengthAwarePaginator; /** * This method will fetch a single Set from the database utilising @@ -28,10 +34,12 @@ public function fetch(); * not exist in the database. * @param int $id * @param bool $search + * @param array fields + * @param array with * @return Set * @throws NotFoundException */ - public function findById(int $id, bool $search = false): Set; + public function findById(int $id, bool $search = false, array $fields = [], array $with = []): Set; /** * This method will create a Set