Files
3dealer/app/Http/Controllers/WishlistController.php
T

57 lines
1.7 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use App\Catalog\ProductCard;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Collection;
use Illuminate\View\View;
use Modules\Core\Catalog\Services\ProductService;
2026-09-28 10:10:50 +03:00
use Modules\Core\Wishlist\Services\WishlistService;
2026-09-28 10:10:50 +03:00
/**
* Page rendering only — the toggle action itself lives in core
* (Modules\Core\Wishlist\Http\Controllers\WishlistController, route
* `wishlist.toggle`), since it needs no app-specific presentation.
*/
class WishlistController extends Controller
{
public function __construct(
2026-09-28 10:10:50 +03:00
private readonly WishlistService $wishlist,
) {}
/**
* The account's wishlist page.
*/
public function index(string $locale, ProductService $products): View
{
return view('account.wishlist', ['products' => $this->products($products)]);
}
/**
* The same list for a guest, from their cookie. Logged-in users are sent
* to the account version.
*/
public function guest(string $locale, ProductService $products): View|RedirectResponse
{
if (auth()->check()) {
return redirect()->route('account.wishlist');
}
return view('wishlist.guest', ['products' => $this->products($products)]);
}
/**
* Product cards for the current wishlist, newest first. Products no longer
* in the search index (deleted, unpublished) are simply skipped.
*/
private function products(ProductService $products): Collection
{
return collect($this->wishlist->ids())
->map(fn (int $id) => $products->getById($id))
->filter()
->map(fn (array $product) => ['id' => $product['id'], ...ProductCard::fromIndexed($product)])
->values();
}
}