Split browser.php to commands with routes.
This commit is contained in:
@@ -40,6 +40,7 @@ class ScrapeWebsite extends Command
|
||||
$products = $this->getProducts();
|
||||
$progress->setMaxSteps(count($products));
|
||||
foreach ($products as $product) {
|
||||
//dd($product);
|
||||
$this->saveProduct($product);
|
||||
$progress->advance();
|
||||
}
|
||||
@@ -80,11 +81,13 @@ 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 = json_encode($product->categories);
|
||||
$productModel->categories = $product->categories;
|
||||
$productModel->image = $product->image;
|
||||
$productModel->subTitle = $product->subTitle;
|
||||
$productModel->variantCode = $product->variantCode;
|
||||
@@ -92,7 +95,8 @@ class ScrapeWebsite extends Command
|
||||
$productModel->url = $product->url;
|
||||
$productModel->save();
|
||||
$priceExists = $productModel->price()->whereRaw("strftime('%Y-%m-%d', created_at) = ?", [date('Y-m-d')])->exists();
|
||||
if (!$priceExists) {
|
||||
|
||||
if (false === $priceExists) {
|
||||
$price = new Price();
|
||||
$price->price = $product->productPrice;
|
||||
$price->productStandardPrice = $product->productStandardPrice;
|
||||
@@ -100,7 +104,7 @@ class ScrapeWebsite extends Command
|
||||
$productModel->price()->save($price);
|
||||
}
|
||||
$stockExist = $productModel->stock()->whereRaw("strftime('%Y-%m-%d', created_at) = ?", [date('Y-m-d')])->exists();
|
||||
if (!$stockExist) {
|
||||
if (false === $stockExist) {
|
||||
$stock = new Stock();
|
||||
$stock->availableQuantity = $product->availableQuantity;
|
||||
$stock->stock = $product->stock;
|
||||
|
||||
22
src/Controller/BaseController.php
Normal file
22
src/Controller/BaseController.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Krzysiej\RyobiCrawler\Controller;
|
||||
|
||||
use Twig\Environment;
|
||||
use Twig\Loader\FilesystemLoader;
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
|
||||
class BaseController
|
||||
{
|
||||
protected Environment $twig;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$capsule = new Capsule;
|
||||
$capsule->addConnection(['driver' => 'sqlite', 'database' => __DIR__ . '/../../database.sqlite']);
|
||||
$capsule->setAsGlobal();
|
||||
$capsule->bootEloquent();
|
||||
$loader = new FilesystemLoader( __DIR__ . '/../../src/templates');
|
||||
$this->twig = new Environment($loader);
|
||||
}
|
||||
}
|
||||
20
src/Controller/CategoryController.php
Normal file
20
src/Controller/CategoryController.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Krzysiej\RyobiCrawler\Controller;
|
||||
|
||||
use Krzysiej\RyobiCrawler\Models\Product;
|
||||
|
||||
final class CategoryController extends BaseController
|
||||
{
|
||||
public function __invoke(string $category): void
|
||||
{
|
||||
$products = Product::with('price')
|
||||
->selectRaw('products.*')
|
||||
->fromRaw('products, json_each(products.categories)')
|
||||
->whereRaw('json_each.value = ?', [$category])
|
||||
->orderByDesc('starred')
|
||||
->orderByDesc('created_by')
|
||||
->get();
|
||||
$this->twig->display('productList.html.twig', ['products' => $products]);
|
||||
}
|
||||
}
|
||||
14
src/Controller/IndexController.php
Normal file
14
src/Controller/IndexController.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Krzysiej\RyobiCrawler\Controller;
|
||||
|
||||
use Krzysiej\RyobiCrawler\Models\Product;
|
||||
|
||||
final class IndexController extends BaseController
|
||||
{
|
||||
public function __invoke(): void
|
||||
{
|
||||
$products = Product::with('price')->orderByDesc('starred')->orderByDesc('created_by')->get();
|
||||
$this->twig->display('productList.html.twig', ['products' => $products]);
|
||||
}
|
||||
}
|
||||
14
src/Controller/ProductController.php
Normal file
14
src/Controller/ProductController.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Krzysiej\RyobiCrawler\Controller;
|
||||
|
||||
use Krzysiej\RyobiCrawler\Models\Product;
|
||||
|
||||
final class ProductController extends BaseController
|
||||
{
|
||||
public function __invoke(int $productId): void
|
||||
{
|
||||
$product = Product::with('price')->find($productId);
|
||||
$this->twig->display('product.html.twig', ['product' => $product ?? []]);
|
||||
}
|
||||
}
|
||||
17
src/Controller/SearchController.php
Normal file
17
src/Controller/SearchController.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Krzysiej\RyobiCrawler\Controller;
|
||||
|
||||
use Krzysiej\RyobiCrawler\Models\Product;
|
||||
|
||||
final class SearchController extends BaseController
|
||||
{
|
||||
public function __invoke(string $search): void
|
||||
{
|
||||
$products = Product::with('price')
|
||||
->orWhere([['name', 'like', "%$search%"]])
|
||||
->orWhere([['subTitle', 'like', "%$search%"]])->get();
|
||||
|
||||
$this->twig->display('productList.html.twig', ['products' => $products ?? [], 'search' => $search]);
|
||||
}
|
||||
}
|
||||
@@ -39,10 +39,11 @@ class Product extends Model
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function categories(): Attribute
|
||||
public function categories(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn(string $value) => array_reverse(json_decode($value, 1)),
|
||||
set: fn(array $value) => json_encode($value),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,12 +4,12 @@
|
||||
<table class='table table-hover'>
|
||||
<tr>
|
||||
<td><img src='{{ product.image }}&width=150' class='img-fluid' alt='{{ product.name }}'/></td>
|
||||
<td><a href='?product_id={{ product.id }}'>{{ product.name }}</a></td>
|
||||
<td><a href='/browser.php/product/{{ product.id }}'>{{ product.name }}</a></td>
|
||||
<td>{{ product.subTitle }}</td>
|
||||
<td>
|
||||
<ul class='nav'>
|
||||
{% for category in product.categories %}
|
||||
<li class="nav-item"><a class="nav-link" href="?category={{ category }}"> {{ category }} </a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="/browser.php/category/{{ category }}"> {{ category }} </a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</td>
|
||||
|
||||
@@ -6,12 +6,12 @@
|
||||
<tr>
|
||||
<td class="align-middle font-weight-bold h3"><a class="text-warning text-decoration-none" href="?star={{ product.id }}">{% if product.starred %}★{% else %} ☆ {% endif %}</a></td>
|
||||
<td><img src='{{ product.image }}&width=70' class='img-fluid' alt='{{ product.name }}'/></td>
|
||||
<td class="align-middle"><a href='?product_id={{ product.id }}'>{{ product.name }}</a></td>
|
||||
<td class="align-middle"><a href='/browser.php/product/{{ product.id }}'>{{ product.name }}</a></td>
|
||||
<td class="align-middle">{{ product.subTitle }}</td>
|
||||
<td class="align-middle">
|
||||
<ul class='nav'>
|
||||
{% for category in product.categories %}
|
||||
<li class="nav-item"><a class="nav-link" href="?category={{ category }}"> {{ category }} </a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="/browser.php/category/{{ category }}"> {{ category }} </a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</td>
|
||||
|
||||
Reference in New Issue
Block a user