Promo endpoint that display only products on sale.
This commit is contained in:
@@ -2,17 +2,31 @@
|
||||
|
||||
namespace Krzysiej\RyobiCrawler\Controller;
|
||||
|
||||
use Illuminate\Support\ItemNotFoundException;
|
||||
use Krzysiej\RyobiCrawler\Models\Product;
|
||||
use Twig\Error\LoaderError;
|
||||
use Twig\Error\RuntimeError;
|
||||
use Twig\Error\SyntaxError;
|
||||
|
||||
final class ProductController extends BaseController
|
||||
{
|
||||
/**
|
||||
* @throws SyntaxError
|
||||
* @throws RuntimeError
|
||||
* @throws LoaderError
|
||||
*/
|
||||
public function __invoke(int $productId): void
|
||||
{
|
||||
$product = Product::with([
|
||||
'price' => fn($query) => $query->orderBy('created_at', 'desc')
|
||||
])->find($productId);
|
||||
|
||||
if(null === $product) {
|
||||
throw new ItemNotFoundException('Product not found');
|
||||
}
|
||||
|
||||
$priceList = $product->price()->pluck('price')->implode(',');
|
||||
$priceDates = $product->price()->pluck('created_at')->map(fn($date) => $date->format('Y-m-d'))->implode("','");
|
||||
$this->twig->display('product.html.twig', ['product' => $product ?? [], 'price_list' => $priceList, 'price_dates' => $priceDates]);
|
||||
$this->twig->display('product.html.twig', ['product' => $product, 'price_list' => $priceList, 'price_dates' => $priceDates]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user