Updated repositories
This commit is contained in:
parent
b0232f39e6
commit
90895be05f
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,10 +4,16 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Repository\Interfaces;
|
||||
|
||||
use App\Http\Resources\CategoryResource;
|
||||
use App\Models\Category;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
interface CategoryRepository
|
||||
{
|
||||
|
||||
public function all();
|
||||
public function get(string $slug);
|
||||
public function all(): Collection;
|
||||
public function get(string $slug): CategoryResource;
|
||||
public function update(Category $category, array $data = []): bool;
|
||||
public function create(array $data = []): Category;
|
||||
|
||||
}
|
||||
|
@ -4,10 +4,16 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Repository\Interfaces;
|
||||
|
||||
use App\Http\Resources\ProjectResource;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
interface ProjectRepository
|
||||
{
|
||||
|
||||
public function all();
|
||||
public function get(int $id);
|
||||
public function all(): Collection;
|
||||
public function get(int $id): ProjectResource;
|
||||
public function update(Project $project, array $data = []): bool;
|
||||
public function create(array $data = []): Project;
|
||||
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ use App\Http\Resources\ProjectResource;
|
||||
use App\Models\Project;
|
||||
use App\Repository\Interfaces\ProjectRepository as ProjectRepositoryInterface;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class ProjectRepository implements ProjectRepositoryInterface
|
||||
{
|
||||
@ -17,7 +18,7 @@ class ProjectRepository implements ProjectRepositoryInterface
|
||||
private Project $project
|
||||
) {}
|
||||
|
||||
public function all()
|
||||
public function all(): Collection
|
||||
{
|
||||
$project = $this->project
|
||||
->query()
|
||||
@ -26,7 +27,7 @@ class ProjectRepository implements ProjectRepositoryInterface
|
||||
return (new ProjectCollection($project))->collection;
|
||||
}
|
||||
|
||||
public function get(int $id)
|
||||
public function get(int $id): ProjectResource
|
||||
{
|
||||
$project = $this->project
|
||||
->query()
|
||||
@ -34,29 +35,55 @@ class ProjectRepository implements ProjectRepositoryInterface
|
||||
return new ProjectResource($project);
|
||||
}
|
||||
|
||||
public function add(array $data)
|
||||
public function update(Project $project, array $data = []): bool
|
||||
{
|
||||
$toSave = [
|
||||
'title' => $data['title'],
|
||||
'author' => $data['author'],
|
||||
'release_date' => Carbon::createFromFormat('Y-d-m', $data['release_date']),
|
||||
'project_url' => $data['project_url'] ?? null,
|
||||
'project_version' => $data['project_version'] ?? null,
|
||||
'description' => $data['description'] ?? null
|
||||
];
|
||||
$data = $this->parseToArray($data);
|
||||
|
||||
if (is_array($data['categories']))
|
||||
return $project
|
||||
->update($data);
|
||||
}
|
||||
|
||||
public function create(array $data = []): Project
|
||||
{
|
||||
$data = $this->parseToArray($data);
|
||||
|
||||
return $this->project
|
||||
->query()
|
||||
->create($data);
|
||||
}
|
||||
|
||||
private function parseToArray(array $data = []): array
|
||||
{
|
||||
$toSave = [];
|
||||
|
||||
if (isset($data['title']))
|
||||
$toSave['title'] = $data['title'];
|
||||
|
||||
if (isset($data['author']))
|
||||
$toSave['author'] = $data['author'];
|
||||
|
||||
if (isset($data['release_date']))
|
||||
$toSave['release_date'] = Carbon::createFromFormat('Y-d-m', $data['release_date']);
|
||||
|
||||
if (isset($data['project_url']))
|
||||
$toSave['project_url'] = $data['project_url'];
|
||||
|
||||
if (isset($data['project_version']))
|
||||
$toSave['project_version'] = $data['project_version'];
|
||||
|
||||
if (isset($data['description']))
|
||||
$toSave['description'] = $data['description'];
|
||||
|
||||
if (isset($data['categories']) && is_array($data['categories']))
|
||||
$toSave['categories'] = $data['categories'];
|
||||
|
||||
if (is_array($data['images']))
|
||||
if (isset($data['images']) && is_array($data['images']))
|
||||
$toSave['images'] = $data['images'];
|
||||
|
||||
if (!empty($data['update_date']))
|
||||
if (isset($data['update_date']) && !empty($data['update_date']))
|
||||
$toSave['update_date'] = Carbon::createFromFormat('Y-d-m', $data['update_date']);
|
||||
|
||||
$this->project
|
||||
->query()
|
||||
->create($toSave);
|
||||
return $toSave;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user