diff --git a/app/Livewire/BlogPost/ListView.php b/app/Livewire/BlogPost/ListView.php index bde3fa9..99558da 100644 --- a/app/Livewire/BlogPost/ListView.php +++ b/app/Livewire/BlogPost/ListView.php @@ -9,6 +9,7 @@ use Illuminate\Support\Str; use Illuminate\View\View; use Livewire\Component; use Livewire\WithPagination; +use App\Services\BlogPost\ListViewService; class ListView extends Component { @@ -29,28 +30,11 @@ class ListView extends Component 'author' => ['except' => ''], ]; - public function render(): View + public function render(ListViewService $service): View { - if ($this->author) { - $author = $this->author; - $user = User::find($author); - $posts = $user ? $user->author_of_blog_post()->distinct() : []; - } elseif ($this->tag) { - $tagId = Str::of($this->tag)->split('/-/')->first(); - $posts = Tag::find($tagId)->blogPosts(); - } elseif ($this->search && mb_strlen($this->search) > 2) { - $posts = BlogPost::where('title', 'rlike', $this->search); - } else { - $posts = (new BlogPost()); - } - - if ($this->typeFilter === 'user') { - $posts = $posts->whereRelation('author', 'is_consultant', false); - } - if ($this->typeFilter === 'consultant') { - $posts = $posts->whereRelation('author', 'is_consultant', true); - } + $posts = $service->buildBlogPosts(); return view('livewire.blog-post.list-view', ['blogPosts' => $posts->orderBy('created_at', 'desc')->paginate(10)]); } + } diff --git a/app/Livewire/Recipe/ListView.php b/app/Livewire/Recipe/ListView.php index 3790be5..6d2e62b 100644 --- a/app/Livewire/Recipe/ListView.php +++ b/app/Livewire/Recipe/ListView.php @@ -5,10 +5,13 @@ namespace App\Livewire\Recipe; use App\Models\Recipe; use App\Models\Tag; use App\Models\User; +use Illuminate\Database\Eloquent\Relations\Relation; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Support\Str; use Illuminate\View\View; use Livewire\Component; use Livewire\WithPagination; +use App\Services\Recipe\ListViewService; class ListView extends Component { @@ -29,57 +32,16 @@ class ListView extends Component 'author' => ['except' => ''], ]; - public function render(): View + public function render(ListViewService $service): View { - if ($this->author) { - $author = $this->author; - $user = User::find($author); - $recipes = $user ? $user->author_of_recipe() : []; - } elseif ($this->tag) { - $tagId = Str::of($this->tag)->split('/-/')->first(); - $recipes = Tag::find($tagId)->recipes(); - } elseif ($this->search && mb_strlen($this->search) > 2) { - $recipes = Recipe::where('title', 'rlike', $this->search); - } else { - $recipes = (new Recipe()); - } - - if ($this->typeFilter === 'user') { - $recipes = $recipes->whereRelation('author', 'is_consultant', false); - } - - if ($this->typeFilter === 'consultant') { - $recipes = $recipes->whereRelation('author', 'is_consultant', true); - } + $recipes = $service->buildRecipes(); return view( 'livewire.recipe.list-view', [ 'recipes' => $recipes->orderByDesc('updated_at')->paginate(10), - 'rawTypes' => $this->getTagsCollection(), + 'rawTypes' => $service->getTagsCollection(), ] ); } - - private function getTagsCollection(): array - { - return Recipe::all()->map( - fn ($r) => [ - $r->type, - $r->tags->map(fn ($t) => $t->id.'-'.$t->name)->all(), - ] - )->reduce(function ($acc, $item) { - [$type, $tagNames] = $item; - - foreach ($tagNames as $name) { - if (isset($acc[$type][$name])) { - $acc[$type][$name] += 1; - } else { - $acc[$type][$name] = 1; - } - } - - return $acc; - }); - } } diff --git a/app/Services/BlogPost/ListViewService.php b/app/Services/BlogPost/ListViewService.php new file mode 100644 index 0000000..e8f2cb8 --- /dev/null +++ b/app/Services/BlogPost/ListViewService.php @@ -0,0 +1,98 @@ +parentState = $parent; + } + + public function buildBlogPosts(): BlogPost + { + $recipes = $this->getPrimaryCriteriedBlogPosts(); + + return $this->getSecondaryCriteriedBlogPosts($recipes); + } + + private function getPrimaryCriteriedBlogPosts(): Builder|BlogPost|MorphToMany|array + { + if ($this->parentState->author) { + $posts = $this->getAuthorBlogPosts(); + } elseif ($this->parentState->tag) { + $posts = $this->getTagBlogPosts(); + } elseif ($this->parentState->search && mb_strlen($this->parentState->search) > 2) { + $posts = $this->getSearchBlogPosts(); + } else { + $posts = (new BlogPost()); + } + + return $posts; + } + + private function getSecondaryCriteriedBlogPosts(Builder|BlogPost|MorphToMany|array $posts): BlogPost + { + if ($this->parentState->typeFilter === 'user') { + $posts = $posts->whereRelation('author', 'is_consultant', false); + } + + if ($this->parentState->typeFilter === 'consultant') { + $posts = $posts->whereRelation('author', 'is_consultant', true); + } + + return $posts; + } + + private function getAuthorBlogPosts(): array + { + $author = $this->parentState->author; + $user = User::find($author); + + return $user ? $user->author_of_blog_post()->distinct() : []; + } + + private function getTagBlogPosts(): MorphToMany + { + $tagId = Str::of($this->parentState->tag)->split('/-/')->first(); + return Tag::find($tagId)->blogPosts(); + } + + private function getSearchBlogPosts(): Builder + { + return BlogPost::where('title', 'rlike', $this->parentState->search); + } + + // private function getTagsCollection(): array + // { + // return Recipe::all()->map( + // fn($r) => [ + // $r->type, + // $r->tags->map(fn($t) => $t->id . '-' . $t->name)->all(), + // ] + // )->reduce(function ($acc, $item) { + // [$type, $tagNames] = $item; + // + // foreach ($tagNames as $name) { + // if (isset($acc[$type][$name])) { + // $acc[$type][$name] += 1; + // } else { + // $acc[$type][$name] = 1; + // } + // } + // + // return $acc; + // }); + // } +} diff --git a/app/Services/Recipe/ListViewService.php b/app/Services/Recipe/ListViewService.php new file mode 100644 index 0000000..af056f9 --- /dev/null +++ b/app/Services/Recipe/ListViewService.php @@ -0,0 +1,98 @@ +parentState = $parent; + } + + public function buildRecipes(): Recipe + { + $recipes = $this->getPrimaryCriteriedRecipes(); + + return $this->getSecondaryCriteriedRecipes($recipes); + } + + private function getPrimaryCriteriedRecipes(): Builder|Recipe|MorphToMany|array + { + if ($this->parentState->author) { + return $this->getAuthorRecipes(); + } + if ($this->parentState->tag) { + return $this->getTagRecipes(); + } + if ($this->parentState->search && mb_strlen($this->parentState->search) > 2) { + return $this->getSearchRecipes(); + } + + return (new Recipe()); + } + + private function getSecondaryCriteriedRecipes(Builder|Recipe|MorphToMany|array $recipes): Recipe + { + if ($this->parentState->typeFilter === 'user') { + $recipes = $recipes->whereRelation('author', 'is_consultant', false); + } + + if ($this->parentState->typeFilter === 'consultant') { + $recipes = $recipes->whereRelation('author', 'is_consultant', true); + } + + return $recipes; + } + + private function getAuthorRecipes(): array + { + $author = $this->parentState->author; + $user = User::find($author); + return $user ? $user->author_of_recipe() : []; + } + + private function getTagRecipes(): MorphToMany + { + $tagId = Str::of($this->parentState->tag)->split('/-/')->first(); + return Tag::find($tagId)->recipes(); + } + + private function getSearchRecipes(): Builder + { + return Recipe::where('title', 'rlike', $this->parentState->search); + } + + public function getTagsCollection(): array + { + return Recipe::all()->map( + fn($r) => [ + $r->type, + $r->tags->map(fn($t) => $t->id . '-' . $t->name)->all(), + ] + )->reduce(function ($acc, $item) { + [$type, $tagNames] = $item; + + foreach ($tagNames as $name) { + if (isset($acc[$type][$name])) { + $acc[$type][$name] += 1; + } else { + $acc[$type][$name] = 1; + } + } + + return $acc; + }); + } +}