Updated repositories

This commit is contained in:
2022-02-13 19:55:25 +01:00
parent b0232f39e6
commit 90895be05f
4 changed files with 94 additions and 23 deletions

View File

@@ -8,6 +8,7 @@ use App\Http\Resources\CategoryCollection;
use App\Http\Resources\CategoryResource;
use App\Models\Category;
use App\Repository\Interfaces\CategoryRepository as CategoryRepositoryInterface;
use Illuminate\Support\Collection;
class CategoryRepository implements CategoryRepositoryInterface
{
@@ -16,7 +17,7 @@ class CategoryRepository implements CategoryRepositoryInterface
private Category $category
) {}
public function all()
public function all(): Collection
{
$categories = $this->category
->query()
@@ -25,7 +26,7 @@ class CategoryRepository implements CategoryRepositoryInterface
return (new CategoryCollection($categories))->collection;
}
public function get(string $slug)
public function get(string $slug): CategoryResource
{
$category = $this->category
->query()
@@ -34,4 +35,35 @@ class CategoryRepository implements CategoryRepositoryInterface
return new CategoryResource($category);
}
public function update(Category $category, array $data = []): bool
{
$data = $this->parseToArray($data);
return $category
->update($data);
}
public function create(array $data = []): Category
{
$data = $this->parseToArray($data);
return $this->category
->query()
->create($data);
}
private function parseToArray(array $data = []): array
{
$toSave = [];
if (isset($data['name']) && !empty($data['name']))
$toSave['name'] = $data['name'];
if (isset($data['slug']) && !empty($data['slug']))
$toSave['slug'] = $data['slug'];
if (isset($data['default']) && is_bool($data['default']))
$toSave['default'] = $data['default'];
if (isset($data['visible']) && is_bool($data['visible']))
$toSave['visible'] = $data['visible'];
return $toSave;
}
}