Update star route.

This commit is contained in:
Krzysztof Płaczek
2024-10-14 09:34:23 +02:00
parent f2e6cba2f5
commit 2b595c1403
4 changed files with 23 additions and 8 deletions

View File

@@ -7,6 +7,7 @@ use Krzysiej\RyobiCrawler\Controller\CategoryController;
use Krzysiej\RyobiCrawler\Controller\IndexController; use Krzysiej\RyobiCrawler\Controller\IndexController;
use Krzysiej\RyobiCrawler\Controller\ProductController; use Krzysiej\RyobiCrawler\Controller\ProductController;
use Krzysiej\RyobiCrawler\Controller\SearchController; use Krzysiej\RyobiCrawler\Controller\SearchController;
use Krzysiej\RyobiCrawler\Controller\StarController;
use Krzysiej\RyobiCrawler\Models\Product; use Krzysiej\RyobiCrawler\Models\Product;
use Twig\{Environment, Loader\FilesystemLoader}; use Twig\{Environment, Loader\FilesystemLoader};
use Symfony\Component\Routing\Matcher\UrlMatcher; use Symfony\Component\Routing\Matcher\UrlMatcher;
@@ -17,15 +18,17 @@ use Symfony\Component\Routing\RouteCollection;
if (!file_exists('database.sqlite')) { if (!file_exists('database.sqlite')) {
exit('Database file <code>database.sqlite</code> missing. Run docker compose <blockquote>docker compose exec php-app php index.php app:migrate</blockquote> to create it.'); exit('Database file <code>database.sqlite</code> missing. Run docker compose <blockquote>docker compose exec php-app php index.php app:migrate</blockquote> to create it.');
} }
$productRoute = new Route('browser.php/product/{product_id}', ['_controller' => ProductController::class], ['product_id' => '\d+']); $productRoute = new Route('/browser.php/product/{product_id}', ['_controller' => ProductController::class], ['product_id' => '\d+']);
$searchRoute = new Route('browser.php?search={search_term}', ['_controller' => SearchController::class]); $searchRoute = new Route('/browser.php?search={search_term}', ['_controller' => SearchController::class]);
$categoryRoute = new Route('/browser.php/category/{category_name}', ['_controller' => CategoryController::class]); $categoryRoute = new Route('/browser.php/category/{category_name}', ['_controller' => CategoryController::class]);
$indexRoute = new Route('browser.php', ['_controller' => IndexController::class]); $starRoute = new Route('/browser.php/star/{product_id}', ['_controller' => StarController::class]);
$indexRoute = new Route('/browser.php', ['_controller' => IndexController::class]);
$routes = new RouteCollection(); $routes = new RouteCollection();
$routes->add('product_show', $productRoute); $routes->add('product_show', $productRoute);
$routes->add('search_show', $searchRoute); $routes->add('search_show', $searchRoute);
$routes->add('category_show', $categoryRoute); $routes->add('category_show', $categoryRoute);
$routes->add('start_show', $starRoute);
$routes->add('index_show', $indexRoute); $routes->add('index_show', $indexRoute);
$context = new RequestContext(); $context = new RequestContext();
@@ -36,12 +39,10 @@ try {
die($e->getMessage()); die($e->getMessage());
} }
//dd($parameters['category_name']);
match ($parameters['_controller']) { match ($parameters['_controller']) {
SearchController::class => (new $parameters['_controller']())($parameters['search_term']), SearchController::class => (new $parameters['_controller']())($parameters['search_term']),
CategoryController::class => (new $parameters['_controller']())($parameters['category_name']), CategoryController::class => (new $parameters['_controller']())($parameters['category_name']),
ProductController::class => (new $parameters['_controller']())($parameters['product_id']), ProductController::class, StarController::class => (new $parameters['_controller']())($parameters['product_id']),
IndexController::class => (new $parameters['_controller']())(), IndexController::class => (new $parameters['_controller']())(),
default => throw new Exception('Route not found') default => throw new Exception('Route not found')
}; };

View File

@@ -0,0 +1,14 @@
<?php
namespace Krzysiej\RyobiCrawler\Controller;
use Krzysiej\RyobiCrawler\Models\Product;
final class StarController extends BaseController
{
public function __invoke(int $productId): void
{
Product::find($productId)->toggleStarred()->save();
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
}

View File

@@ -3,6 +3,7 @@
{% block content %} {% block content %}
<table class='table table-hover'> <table class='table table-hover'>
<tr> <tr>
<td class="align-middle font-weight-bold h3"><a class="text-warning text-decoration-none" href="/browser.php/star/{{ product.id }}">{% if product.starred %}{% else %}{% endif %}</a></td>
<td><img src='{{ product.image }}&width=150' class='img-fluid' alt='{{ product.name }}'/></td> <td><img src='{{ product.image }}&width=150' class='img-fluid' alt='{{ product.name }}'/></td>
<td><a href='/browser.php/product/{{ product.id }}'>{{ product.name }}</a></td> <td><a href='/browser.php/product/{{ product.id }}'>{{ product.name }}</a></td>
<td>{{ product.subTitle }}</td> <td>{{ product.subTitle }}</td>
@@ -17,7 +18,6 @@
</tr> </tr>
<tr> <tr>
<td colspan="4"> <td colspan="4">
<table class='table table-hover table-sm mb-0'> <table class='table table-hover table-sm mb-0'>
<thead> <thead>
<tr> <tr>

View File

@@ -4,7 +4,7 @@
<table class='table table-hover'> <table class='table table-hover'>
{% for product in products %} {% for product in products %}
<tr> <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 class="align-middle font-weight-bold h3"><a class="text-warning text-decoration-none" href="/browser.php/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><img src='{{ product.image }}&width=70' class='img-fluid' alt='{{ product.name }}'/></td>
<td class="align-middle"><a href='/browser.php/product/{{ 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">{{ product.subTitle }}</td>