From 90895be05f254283238361c948471996987b3991 Mon Sep 17 00:00:00 2001 From: Kamil Niemczycki Date: Sun, 13 Feb 2022 19:55:25 +0100 Subject: [PATCH] Updated repositories --- app/Repository/CategoryRepository.php | 36 ++++++++++- .../Interfaces/CategoryRepository.php | 10 ++- .../Interfaces/ProjectRepository.php | 10 ++- app/Repository/ProjectRepository.php | 61 +++++++++++++------ 4 files changed, 94 insertions(+), 23 deletions(-) diff --git a/app/Repository/CategoryRepository.php b/app/Repository/CategoryRepository.php index 71c4904..5943f0e 100644 --- a/app/Repository/CategoryRepository.php +++ b/app/Repository/CategoryRepository.php @@ -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; + } + } diff --git a/app/Repository/Interfaces/CategoryRepository.php b/app/Repository/Interfaces/CategoryRepository.php index cccf99d..9654467 100644 --- a/app/Repository/Interfaces/CategoryRepository.php +++ b/app/Repository/Interfaces/CategoryRepository.php @@ -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; } diff --git a/app/Repository/Interfaces/ProjectRepository.php b/app/Repository/Interfaces/ProjectRepository.php index b0cb52d..3143c6f 100644 --- a/app/Repository/Interfaces/ProjectRepository.php +++ b/app/Repository/Interfaces/ProjectRepository.php @@ -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; } diff --git a/app/Repository/ProjectRepository.php b/app/Repository/ProjectRepository.php index 07b2700..bac03ac 100644 --- a/app/Repository/ProjectRepository.php +++ b/app/Repository/ProjectRepository.php @@ -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; } }