Skip to content

Commit

Permalink
Merge branch 'develop' into USH-1687
Browse files Browse the repository at this point in the history
  • Loading branch information
Mh-Asmi authored Nov 28, 2024
2 parents 3a0a43f + 514d5ca commit 5b32834
Show file tree
Hide file tree
Showing 17 changed files with 375 additions and 222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ class StoreCategoryCommand implements Command
* @var array
*/
private $availableLanguages;

/**
* @var array
*/
private $translations;

public function __construct(
?int $parentId,
Expand All @@ -75,6 +80,7 @@ public function __construct(
?string $icon,
int $priority,
?array $role,
array $translations,
?string $defaultLanguage = 'en',
array $availableanguages = []
) {
Expand All @@ -89,6 +95,7 @@ public function __construct(
$this->role = $role;
$this->defaultLanguage = $defaultLanguage;
$this->availableLanguages = $availableanguages;
$this->translations = $translations;
}

public static function createFromRequest(CategoryRequest $request): self
Expand All @@ -108,6 +115,7 @@ public static function createFromRequest(CategoryRequest $request): self
$request->input('icon'),
(int) $request->input('priority'),
$request->input('role'),
$request->input('translations')??[],
self::DEFAULT_LANUGAGE,
[]
);
Expand Down Expand Up @@ -194,4 +202,11 @@ public function getAvailableLanguages(): array
{
return $this->availableLanguages;
}
/**
* @return array
*/
public function getTranslations(): array
{
return $this->translations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ class UpdateCategoryCommand implements Command
* @var ?array
*/
private $availableLanguages;

/**
* @var array
*/
private $translations;

public function __construct(
int $categoryId,
Expand All @@ -80,8 +85,9 @@ public function __construct(
?string $icon,
?int $priority,
?array $role,
array $translations,
?string $defaultLanguage,
?array $availableLanguages
?array $availableLanguages = []
) {
$this->categoryId = $categoryId;
$this->parentId = $parentId;
Expand All @@ -95,6 +101,7 @@ public function __construct(
$this->role = $role;
$this->defaultLanguage = $defaultLanguage;
$this->availableLanguages = $availableLanguages;
$this->translations = $translations;
}

public static function fromRequest(int $id, CategoryRequest $request, Category $current_category): self
Expand All @@ -110,6 +117,7 @@ public static function fromRequest(int $id, CategoryRequest $request, Category $
$request->has('icon')?$request->input('icon'):$current_category->icon,
$request->has('priority')?$request->input('priority'):$current_category->priority,
$request->has('role')?$request->input('role'):$current_category->role,
$request->input('translations')??[],
self::DEFAULT_LANUGAGE,
[]
);
Expand Down Expand Up @@ -174,4 +182,12 @@ public function getAvailableLanguages(): ?array
{
return $this->availableLanguages;
}

/**
* @return array
*/
public function getTranslations(): array
{
return $this->translations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
use App\Bus\Action;
use App\Bus\Query\AbstractQueryHandler;
use App\Bus\Query\Query;
use Ushahidi\Core\Tool\SearchData;
use Ushahidi\Modules\V5\Actions\Category\Queries\FetchAllCategoriesQuery;
use Ushahidi\Modules\V5\Repository\Category\CategoryRepository;
use Illuminate\Support\Facades\Auth;

class FetchAllCategoriesQueryHandler extends AbstractQueryHandler
{
Expand All @@ -25,30 +23,18 @@ protected function isSupported(Query $query)
}
}

public function __invoke(Action $action)
public function __invoke(Action $query)
{
/**
* @var FetchAllCategoriesQuery $action
*/
$this->isSupported($action);

$data = new SearchData;

$searchFields = $action->getCategorySearchFields();

$user = Auth::guard()->user();

$data->setFilter('keyword', $searchFields->q());

$data->setFilter('tag', $searchFields->tag());
$data->setFilter('type', $searchFields->type());
$data->setFilter('role', $searchFields->role());
$data->setFilter('user_id', $user->id ?? null);
$data->setFilter('parent_id', $searchFields->parentId());
$data->setFilter('is_parent', $searchFields->level() === 'parent');
$data->setFilter('is_admin', $searchFields->role() && $searchFields->role() == "admin");

$this->categoryRepository->setSearchParams($data);
return $this->categoryRepository->fetchAll($action->getPaging());
$this->isSupported($query);
$only_fields = array_unique(array_merge($query->getFields(), $query->getFieldsForRelationship()));
return $this->categoryRepository->paginate(
$query->getPaging(),
$query->getSearchFields(),
$only_fields,
$query->getWithRelationship()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
use App\Bus\Action;
use App\Bus\Query\AbstractQueryHandler;
use App\Bus\Query\Query;
use Ushahidi\Core\Tool\SearchData;
use Ushahidi\Modules\V5\Actions\Category\Queries\FetchCategoryByIdQuery;
use Ushahidi\Modules\V5\Models\Category;
use Ushahidi\Modules\V5\Repository\Category\CategoryRepository;
use Illuminate\Support\Facades\Auth;

class FetchCategoryByIdQueryHandler extends AbstractQueryHandler
{
Expand All @@ -28,12 +26,19 @@ protected function isSupported(Query $query): void
}
}

public function __invoke(Action $action): Category
public function __invoke(Action $query): Category
{
/**
* @var FetchCategoryByIdQuery $action
*/
$this->isSupported($action);
return $this->categoryRepository->findById($action->getId());
$this->isSupported($query);
return $this->categoryRepository->findById(
$query->getId(),
array_unique(array_merge(
$query->getFields(),
$query->getFieldsForRelationship()
)),
$query->getWithRelationship()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace Ushahidi\Modules\V5\Actions\Category\Handlers;

use App\Bus\Action;
use App\Bus\Command\AbstractCommandHandler;
use App\Bus\Command\Command;
use Ushahidi\Modules\V5\Actions\Category\Commands\StoreCategoryCommand;
use Ushahidi\Modules\V5\Models\Category;
use Ushahidi\Modules\V5\Repository\Category\CategoryRepository;
use Illuminate\Support\Facades\Auth;
use Ushahidi\Modules\V5\Actions\V5CommandHandler;

class StoreCategoryCommandHandler extends AbstractCommandHandler
class StoreCategoryCommandHandler extends V5CommandHandler
{
private $categoryRepository;

Expand Down Expand Up @@ -46,19 +46,29 @@ public function __invoke(Action $action): int

$user_id = Auth::guard()->user()->id ?? null;

return $this->categoryRepository->store(
$parentId,
$user_id,
ucfirst($action->getTag()),
$slug,
$action->getType(),
$action->getDescription(),
$action->getColor(),
$action->getIcon(),
$action->getPriority(),
$action->getRole(),
$action->getDefaultLanguage(),
$action->getAvailableLanguages()
$category = $this->categoryRepository->store(
$parentId,
$user_id,
ucfirst($action->getTag()),
$slug,
$action->getType(),
$action->getDescription(),
$action->getColor(),
$action->getIcon(),
$action->getPriority(),
$action->getRole(),
$action->getDefaultLanguage(),
$action->getAvailableLanguages()
);

$this->saveTranslations(
$category,
$category->toArray(),
$action->getTranslations(),
$category->id,
'category'
);

return $category->id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
namespace Ushahidi\Modules\V5\Actions\Category\Handlers;

use App\Bus\Action;
use App\Bus\Command\AbstractCommandHandler;
use App\Bus\Command\Command;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Ushahidi\Modules\V5\Models\Category;
use Ushahidi\Modules\V5\Repository\Category\CategoryRepository;
use Ushahidi\Modules\V5\Actions\Category\Commands\UpdateCategoryCommand;
use Illuminate\Support\Facades\Auth;
use Ushahidi\Modules\V5\Actions\V5CommandHandler;

class UpdateCategoryCommandHandler extends AbstractCommandHandler
class UpdateCategoryCommandHandler extends V5CommandHandler
{
private $categoryRepository;

Expand Down Expand Up @@ -49,8 +48,15 @@ public function __invoke(Action $action): Category
$action->getDefaultLanguage(),
$action->getAvailableLanguages()
);

return $this->categoryRepository
$category = $this->categoryRepository
->findById($action->getCategoryId());
$this->updateTranslations(
$category,
$category->toArray(),
$action->getTranslations(),
$category->id,
'category'
);
return $category;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@
use App\Bus\Query\Query;
use Ushahidi\Modules\V5\DTO\Paging;
use Ushahidi\Modules\V5\DTO\CategorySearchFields;
use Ushahidi\Modules\V5\Models\Category;
use Illuminate\Http\Request;
use Ushahidi\Modules\V5\Traits\OnlyParameter\QueryWithOnlyParameter;
use Ushahidi\Modules\V5\Traits\HasPaginate;
use Ushahidi\Modules\V5\Traits\HasSearchFields;

class FetchAllCategoriesQuery implements Query
{

/**
* @var Paging
*/
private $paging;
private $category_search_fields;

public function __construct(Paging $paging, CategorySearchFields $category_search_fields)
{
$this->paging = $paging;
$this->category_search_fields = $category_search_fields;
}

public function getPaging(): Paging
{
return $this->paging;
}

public function getCategorySearchFields()
use QueryWithOnlyParameter;
use HasPaginate;
use HasSearchFields;

public static function fromRequest(Request $request): self
{
return $this->category_search_fields;
$query = new self();
$query->setPaging($request);
$query->setSearchFields(new CategorySearchFields($request));
$query->addOnlyParameteresFromRequest(
$request,
Category::ALLOWED_FIELDS,
Category::ALLOWED_RELATIONSHIPS,
Category::REQUIRED_FIELDS
);
return $query;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,35 @@
namespace Ushahidi\Modules\V5\Actions\Category\Queries;

use App\Bus\Query\Query;
use Ushahidi\Modules\V5\Models\Category;
use Illuminate\Http\Request;
use Ushahidi\Modules\V5\Traits\OnlyParameter\QueryWithOnlyParameter;

class FetchCategoryByIdQuery implements Query
{
use QueryWithOnlyParameter;

private $id;

public function __construct(int $id)
{
$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,
Category::ALLOWED_FIELDS,
Category::ALLOWED_RELATIONSHIPS,
Category::REQUIRED_FIELDS
);
return $query;
}
public function getId(): int
{
return $this->id;
Expand Down
15 changes: 11 additions & 4 deletions src/Ushahidi/Modules/V5/Actions/V5CommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Ushahidi\Modules\V5\Actions;

use App\Bus\Command\AbstractCommandHandler;
use Hamcrest\Arrays\IsArray;
use Ushahidi\Modules\V5\Models\Translation;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Exceptions\HttpResponseException;
Expand Down Expand Up @@ -104,11 +105,17 @@ protected function updateTranslations(
int $translatable_id,
string $type
) {
if (empty($translation_input)) {
return [];
// if (empty($translation_input)) {
// return [];
// }
if (is_array($translation_input)) {
Translation::where('translatable_id', $translatable_id)->where('translatable_type', $type)->delete();
if (count($translation_input)) {
return $this->saveTranslations($entity, $entity_array, $translation_input, $translatable_id, $type);
}
}
Translation::where('translatable_id', $translatable_id)->where('translatable_type', $type)->delete();
return $this->saveTranslations($entity, $entity_array, $translation_input, $translatable_id, $type);
return [];
// return $this->saveTranslations($entity, $entity_array, $translation_input, $translatable_id, $type);
} //end updateTranslations()

/**
Expand Down
Loading

0 comments on commit 5b32834

Please sign in to comment.