Add twig extra bundle to use currency formatter filter. Add custom twig method to display number of active promos. Remove old index.php file. #5
@@ -51,7 +51,6 @@ class Migrate extends Command
|
||||
$table->string('name');
|
||||
$table->integer('skuID')->unique();
|
||||
$table->integer('availableQuantity');
|
||||
$table->integer('stock');
|
||||
$table->json('categories');
|
||||
$table->string('image');
|
||||
$table->string('subTitle');
|
||||
|
||||
@@ -69,7 +69,7 @@ class ScrapeWebsite extends Command
|
||||
$products = array_merge($products, $responseObject->products);
|
||||
$page++;
|
||||
$canLoadMore = $responseObject->canLoadMore;
|
||||
} catch (GuzzleException $e) {
|
||||
} catch (GuzzleException) {
|
||||
return $products;
|
||||
}
|
||||
} while ($canLoadMore);
|
||||
@@ -81,12 +81,10 @@ class ScrapeWebsite extends Command
|
||||
{
|
||||
/** @var Product $productModel */
|
||||
$productModel = Product::firstOrNew(['skuID' => $product->skuID]);
|
||||
//dd($productModel);
|
||||
|
||||
$productModel->skuID = $product->skuID;
|
||||
$productModel->name = $product->name;
|
||||
$productModel->availableQuantity = $product->availableQuantity;
|
||||
$productModel->stock = $product->stock;
|
||||
$productModel->categories = $product->categories;
|
||||
$productModel->image = $product->image;
|
||||
$productModel->subTitle = $product->subTitle;
|
||||
|
||||
@@ -23,7 +23,6 @@ final class ProductController extends BaseController
|
||||
'price' => fn($query) => $query->orderBy('created_at', 'desc'),
|
||||
'stock' => fn($query) => $query->orderBy('created_at', 'desc'),
|
||||
])->find($productId);
|
||||
|
||||
if(null === $product) {
|
||||
throw $this->createNotFoundException('Product not found');
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
* @property integer $skuID
|
||||
* @property string $name
|
||||
* @property integer $availableQuantity
|
||||
* @property integer $stock
|
||||
* @property string[] $categories
|
||||
* @property string $image
|
||||
* @property string $subTitle
|
||||
|
||||
@@ -3,21 +3,37 @@
|
||||
namespace Krzysiej\RyobiCrawler\Twig;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Krzysiej\RyobiCrawler\Models\Price;
|
||||
use Krzysiej\RyobiCrawler\Models\Product;
|
||||
use Krzysiej\RyobiCrawler\Models\Stock;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
class AppExtension extends AbstractExtension
|
||||
{
|
||||
public function getFunctions()
|
||||
public function getFunctions(): array
|
||||
{
|
||||
return [
|
||||
new TwigFunction('promosCount', [$this, 'promosCount']),
|
||||
];
|
||||
}
|
||||
|
||||
public function getFilters(): array
|
||||
{
|
||||
return [
|
||||
new TwigFilter('findByCreatedAtDate', [$this, 'findByCreatedAtDate']),
|
||||
];
|
||||
}
|
||||
|
||||
public function promosCount(): int
|
||||
{
|
||||
return Product::whereHas('currentPrice', fn(Builder $query) => $query->whereColumn('price', '<', 'productStandardPrice'))->with(['currentPrice'])->count();
|
||||
}
|
||||
|
||||
public function findByCreatedAtDate(Collection $items, string $date): Stock|Price|null
|
||||
{
|
||||
return $items->first(fn($item) => str_starts_with($item->created_at, $date));
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,7 @@
|
||||
<th>price</th>
|
||||
<th>lowest product price in 30 days</th>
|
||||
<th colspan='2'>standard price</th>
|
||||
<th>Stock</th>
|
||||
</tr>
|
||||
</thead>
|
||||
{% for price in product.price %}
|
||||
@@ -40,6 +41,7 @@
|
||||
<td>{{ price.lowestProductPrice30Days | format_currency('PLN', {}, 'pl') }}</td>
|
||||
<td>{{ price.productStandardPrice | format_currency('PLN', {}, 'pl') }}</td>
|
||||
<td>{{ price.created_at }}</td>
|
||||
<td>{{ (product.stock | findByCreatedAtDate(price.created_at | slice(0,10))).stock ?? '' }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
|
||||
Reference in New Issue
Block a user