Skip to content

Commit

Permalink
fix category resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Mh-Asmi committed Nov 25, 2024
1 parent 8b22ba3 commit 33a5a6a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function __invoke(Action $query): Category
$this->isSupported($query);
return $this->categoryRepository->findById(
$query->getId(),
false,
array_unique(array_merge(
$query->getFields(),
$query->getFieldsForRelationship()
Expand Down
55 changes: 34 additions & 21 deletions src/Ushahidi/Modules/V5/Http/Resources/CategoryResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Ushahidi\Modules\V5\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource as Resource;
use Ushahidi\Core\Entity\Tag;

class CategoryResource extends Resource
{
Expand All @@ -10,6 +11,24 @@ class CategoryResource extends Resource

public static $wrap = 'result';

private function getResourcePrivileges()
{
$authorizer = service('authorizer.tag');
// Obtain v3 entity from the v5 post model
// Note that we use attributesToArray instead of toArray because the first
// would have the effect of causing unnecessary requests to the database
// (relations are not needed in this case by the authorizer)
$resource_array = $this->resource->attributesToArray();
unset($resource_array['completed_stages']);
$entity = new Tag($resource_array);
// if there's no user the guards will kick them off already, but if there
// is one we need to check the authorizer to ensure we don't let
// users without admin perms create forms etc
// this is an unfortunate problem with using an old version of lumen
// that doesn't let me do guest user checks without adding more risk.
return $authorizer->getAllowedPrivs($entity);
}

/**
* Transform the resource into an array.
*
Expand All @@ -18,27 +37,21 @@ class CategoryResource extends Resource
*/
public function toArray($request)
{
// Preload key relations
$this->resource->loadMissing(['parent', 'children', 'translations']);
return [
'id' => $this->id,
'parent_id' => $this->parent_id,
'tag' => $this->tag,
'slug' => $this->slug,
'type' => $this->type,
'color' => $this->color,
'icon' => $this->icon,
'description' => $this->description,
'role' => $this->makeRole($this->role),
'priority' => $this->priority,
'children' => $this->makeChildren($this->parent, $this->children),
'parent' => $this->makeParent($this->parent),
'translations' => new TranslationCollection($this->translations),
'enabled_languages' => [
'default'=> $this->base_language,
'available' => $this->translations->groupBy('language')->keys()
]
];
$data = $this->resource->toArray();
if (isset($data['role'])) {
$data['role'] = $this->makeRole($this->role);
}
if (isset($data['children'])) {
$data['children'] = $this->makeChildren($this->parent, $this->children);
}
if (isset($data['parent'])) {
$data['parent'] = $this->makeRole($this->parent);
}
if (isset($data['translations'])) {
$data['translations'] = (new TranslationCollection($this->translations))->toArray(null);
}
$data['allowed_privileges']= $this->getResourcePrivileges();
return $data;
}

protected function makeRole($role)
Expand Down
4 changes: 2 additions & 2 deletions src/Ushahidi/Modules/V5/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ class Category extends BaseModel
'created'
];
public const ALLOWED_RELATIONSHIPS = [
'children' => ['fields' => [], 'relationships' => ['children']],
'parent' => ['fields' => [], 'relationships' => ['parent']],
'children' => ['fields' => ['parent_id'], 'relationships' => ['children']],
'parent' => ['fields' => ['parent_id'], 'relationships' => ['parent']],
'translations' => ['fields' => [], 'relationships' => ["translations"]],
'enabled_languages' => ['fields' => ['base_language'], 'relationships' => ['translations']],
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ interface CategoryRepository
* Laravel Eloquent ORM. Will throw an exception if provided identifier does
* not exist in the database.
* @param int $id
* @param array $fields
* @param array $with
* @return Category
* @throws NotFoundException
*/
public function findById(int $id): Category;
public function findById(int $id, array $fields = [], array $with = []): Category;


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,23 @@ public function setSearchParams(SearchData $searchData)
* Laravel Eloquent ORM. Will throw an exception if provided identifier does
* not exist in the database.
* @param int $id
* @param array $fields
* @param array $with
* @return Category
* @throws NotFoundException
*/
public function findById(int $id): Category
public function findById(int $id, array $fields = [], array $with = []): Category
{
$category = Category::find($id);
$query = Category::where('id', '=', $id);
if (count($fields)) {
$query->select($fields);
}
if (count($with)) {
$query->with($with);
}
$category = $query->first();
dd($category);

if (!$category instanceof Category) {
throw new NotFoundException('Category not found');
}
Expand Down Expand Up @@ -136,7 +147,6 @@ public function paginate(
->orderBy('tags.'.$paging->getOrderBy(), $paging->getOrder());

$query = $this->setSearchCondition($search_fields, $query);

if (count($fields)) {
$query->select($fields);
}
Expand Down

0 comments on commit 33a5a6a

Please sign in to comment.