Display stock in product info in price table.

This commit is contained in:
Krzysztof Płaczek
2024-11-17 16:38:22 +01:00
parent 48ee68f71a
commit f963dcd4c8
6 changed files with 20 additions and 7 deletions

View File

@@ -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');

View File

@@ -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;

View File

@@ -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');
}

View File

@@ -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

View File

@@ -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));
}
}