with(['recipes']) ->where('id', (string) $id->value) ->limit(1) ->get() ->map(fn (Product $product) => $this->mapToEntity($product)) ->first(); } public function findBySlug(string $slug): ?DomainProduct { return Product::query() ->with(['recipes']) ->where('slug', $slug) ->limit(1) ->get() ->map(fn (Product $product) => $this->mapToEntity($product)) ->first(); } public function getAllProductsSummaries(): array { return Product::query() ->select(['id', 'name', 'picture']) ->orderBy('name', 'asc') ->get() ->map(fn (Product $product) => $this->mapToSummary($product)) ->all(); } public function getPaginatedProducts( Pagination $pagination, string $search = '', ): LengthAwarePaginator { $dbQueryCallback = function (Builder $query) { /** @var \Illuminate\Database\Eloquent\Builder $query */ $query->with(['recipes']); }; if (! empty($search)) { $scout = Product::search($search)->query($dbQueryCallback); /** @var \Illuminate\Pagination\LengthAwarePaginator $paginated */ $paginated = $scout->paginate( $pagination->pageSize(), 'page', $pagination->pageNumber() ); return $paginated->through(fn ($product) => $this->mapToEntity($product)); } /** @var \Illuminate\Database\Eloquent\Builder $query */ $query = Product::query(); $dbQueryCallback($query); return $query->paginate( perPage: $pagination->pageSize(), page: $pagination->pageNumber() )->through(fn (Product $product) => $this->mapToEntity($product)); } /** * @param \Domain\Models\ValueObjects\Products\BreedingMethod[] $breedingMethods * @param \Domain\Models\ValueObjects\Products\Technology[] $technologies * @param \Domain\Models\ValueObjects\Products\Business[] $businesses */ public function getCategoryProducts( ProductCategory $category, array $breedingMethods = [], array $technologies = [], array $businesses = [], ?bool $isEgalim = null, string $search = '', int $limitPerCategory = 6 ): ?CategoryProductsDto { $dbQueryCallback = function (Builder $query) use ($category, $breedingMethods, $technologies, $businesses, $isEgalim) { /** @var \Illuminate\Database\Eloquent\Builder $query */ $query->where('category', $category->value) ->where('is_published', true) ->with(['recipes']); if (! empty($breedingMethods)) { $query->where(function ($q) use ($breedingMethods) { foreach ($breedingMethods as $method) { $q->orWhereJsonContains('breeding_methods', $method->value); } }); } if (! empty($technologies)) { $techValues = array_map(fn ($t) => $t->value, $technologies); $query->whereIn('technology', $techValues); } if (! empty($businesses)) { $query->where(function ($q) use ($businesses) { foreach ($businesses as $business) { $q->orWhereJsonContains('businesses', $business->value); } }); } if ($isEgalim === true) { $query->where('is_egalim', true); } }; $query = Product::query(); if (! empty($search)) { $scout = Product::search($search); // Get all matching IDs from Meilisearch $matchingIds = $scout->keys(); if ($matchingIds->isEmpty()) { return null; } $query->whereIn('id', $matchingIds); } $dbQueryCallback($query); $count = $query->count(); if ($count === 0) { return null; } $products = (clone $query)->limit($limitPerCategory) ->get() ->map(fn (Product $product) => $this->mapToEntity($product)) ->all(); return new CategoryProductsDto( category: $category, total: $count, products: $products, ); } private function mapToSummary(Product $product): ProductSummary { return new ProductSummary( id: (string) $product->id, name: $product->name, picture: $product->picture?->value, ); } private function mapToEntity(Product $product): DomainProduct { return new DomainProduct( id: new ProductId(Uuid::fromString($product->id)), name: $product->name, slug: $product->slug, category: $product->category, technology: $product->technology, isEgalim: $product->is_egalim, isPublished: (bool) $product->is_published, breedingMethods: $product->breeding_methods->all(), businesses: $product->businesses->all(), detailsAndAdvantages: $product->details_and_advantages, formats: $product->formats->all(), handlingAdvices: $product->handling_advices, ingredients: $product->ingredients, conservationBeforeOpening: $product->conservation_before_opening, description: $product->description, seoTitle: $product->seo_title, seoDescription: $product->seo_description, conservationAfterOpening: $product->conservation_after_opening, nutritionalValues: $product->nutritional_values, picture: $product->picture?->value, pictureAlt: $product->picture_alt, pictureTitle: $product->picture_title, pdfFilePath: $product->pdf?->value, associatedRecipes: $product->relationLoaded('recipes') ? $product->recipes->map( fn (Recipe $recipe): UuidInterface => $recipe->getId()->value )->all() : [], ); } public function getRelatedProducts(RecipeId $recipeId, int $limit = 4): array { $recipe = Recipe::query() ->where('id', $recipeId->value) ->first(); if (! $recipe) { return []; } return $recipe->products() ->where('is_published', true) ->limit($limit) ->get() ->map(fn (Product $product) => $this->mapToEntity($product)) ->all(); } }