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

Merged
krzysiej merged 2 commits from feature/display-stock into master 2024-11-17 16:38:33 +01:00
6 changed files with 20 additions and 7 deletions
Showing only changes of commit f963dcd4c8 - Show all commits

View File

@@ -51,7 +51,6 @@ class Migrate extends Command
$table->string('name'); $table->string('name');
$table->integer('skuID')->unique(); $table->integer('skuID')->unique();
$table->integer('availableQuantity'); $table->integer('availableQuantity');
$table->integer('stock');
$table->json('categories'); $table->json('categories');
$table->string('image'); $table->string('image');
$table->string('subTitle'); $table->string('subTitle');

View File

@@ -69,7 +69,7 @@ class ScrapeWebsite extends Command
$products = array_merge($products, $responseObject->products); $products = array_merge($products, $responseObject->products);
$page++; $page++;
$canLoadMore = $responseObject->canLoadMore; $canLoadMore = $responseObject->canLoadMore;
} catch (GuzzleException $e) { } catch (GuzzleException) {
return $products; return $products;
} }
} while ($canLoadMore); } while ($canLoadMore);
@@ -81,12 +81,10 @@ class ScrapeWebsite extends Command
{ {
/** @var Product $productModel */ /** @var Product $productModel */
$productModel = Product::firstOrNew(['skuID' => $product->skuID]); $productModel = Product::firstOrNew(['skuID' => $product->skuID]);
//dd($productModel);
$productModel->skuID = $product->skuID; $productModel->skuID = $product->skuID;
$productModel->name = $product->name; $productModel->name = $product->name;
$productModel->availableQuantity = $product->availableQuantity; $productModel->availableQuantity = $product->availableQuantity;
$productModel->stock = $product->stock;
$productModel->categories = $product->categories; $productModel->categories = $product->categories;
$productModel->image = $product->image; $productModel->image = $product->image;
$productModel->subTitle = $product->subTitle; $productModel->subTitle = $product->subTitle;

View File

@@ -23,7 +23,6 @@ final class ProductController extends BaseController
'price' => fn($query) => $query->orderBy('created_at', 'desc'), 'price' => fn($query) => $query->orderBy('created_at', 'desc'),
'stock' => fn($query) => $query->orderBy('created_at', 'desc'), 'stock' => fn($query) => $query->orderBy('created_at', 'desc'),
])->find($productId); ])->find($productId);
if(null === $product) { if(null === $product) {
throw $this->createNotFoundException('Product not found'); throw $this->createNotFoundException('Product not found');
} }

View File

@@ -11,7 +11,6 @@ use Illuminate\Database\Eloquent\Relations\HasOne;
* @property integer $skuID * @property integer $skuID
* @property string $name * @property string $name
* @property integer $availableQuantity * @property integer $availableQuantity
* @property integer $stock
* @property string[] $categories * @property string[] $categories
* @property string $image * @property string $image
* @property string $subTitle * @property string $subTitle

View File

@@ -3,21 +3,37 @@
namespace Krzysiej\RyobiCrawler\Twig; namespace Krzysiej\RyobiCrawler\Twig;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Krzysiej\RyobiCrawler\Models\Price;
use Krzysiej\RyobiCrawler\Models\Product; use Krzysiej\RyobiCrawler\Models\Product;
use Krzysiej\RyobiCrawler\Models\Stock;
use Twig\Extension\AbstractExtension; use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction; use Twig\TwigFunction;
class AppExtension extends AbstractExtension class AppExtension extends AbstractExtension
{ {
public function getFunctions() public function getFunctions(): array
{ {
return [ return [
new TwigFunction('promosCount', [$this, 'promosCount']), new TwigFunction('promosCount', [$this, 'promosCount']),
]; ];
} }
public function getFilters(): array
{
return [
new TwigFilter('findByCreatedAtDate', [$this, 'findByCreatedAtDate']),
];
}
public function promosCount(): int public function promosCount(): int
{ {
return Product::whereHas('currentPrice', fn(Builder $query) => $query->whereColumn('price', '<', 'productStandardPrice'))->with(['currentPrice'])->count(); 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));
}
} }

View File

@@ -32,6 +32,7 @@
<th>price</th> <th>price</th>
<th>lowest product price in 30 days</th> <th>lowest product price in 30 days</th>
<th colspan='2'>standard price</th> <th colspan='2'>standard price</th>
<th>Stock</th>
</tr> </tr>
</thead> </thead>
{% for price in product.price %} {% for price in product.price %}
@@ -40,6 +41,7 @@
<td>{{ price.lowestProductPrice30Days | format_currency('PLN', {}, 'pl') }}</td> <td>{{ price.lowestProductPrice30Days | format_currency('PLN', {}, 'pl') }}</td>
<td>{{ price.productStandardPrice | format_currency('PLN', {}, 'pl') }}</td> <td>{{ price.productStandardPrice | format_currency('PLN', {}, 'pl') }}</td>
<td>{{ price.created_at }}</td> <td>{{ price.created_at }}</td>
<td>{{ (product.stock | findByCreatedAtDate(price.created_at | slice(0,10))).stock ?? '' }}</td>
</tr> </tr>
{% endfor %} {% endfor %}
</table> </table>