Skip to content

Commit

Permalink
Merge branch 'develop' into ush-1496
Browse files Browse the repository at this point in the history
  • Loading branch information
ushahidlee authored Nov 6, 2024
2 parents e169627 + 1fb3d92 commit a0999a0
Show file tree
Hide file tree
Showing 19 changed files with 320 additions and 275 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
17 changes: 11 additions & 6 deletions src/Ushahidi/Modules/V5/Actions/Post/HandlePostOnlyParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,21 @@ protected function isSupported(Query $query)
);
}


/**
* @param FetchSavedSearchByIdQuery $query
* @return array
*/
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()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Loading

0 comments on commit a0999a0

Please sign in to comment.