diff --git a/browser.php b/browser.php index e2fba90..af1bbc8 100644 --- a/browser.php +++ b/browser.php @@ -18,7 +18,8 @@ if (isset($_GET['product_id'])) { } if (isset($_GET['category'])) { $template = 'productList.html.twig'; - $products = Product::with('price')->selectRaw('products.*')->fromRaw('products, json_each(products.categories)')->whereRaw('json_each.value = ?', [$_GET['category']])->get(); + $products = Product::with('price')->selectRaw('products.*')->fromRaw('products, json_each(products.categories)')->whereRaw('json_each.value = ?', [$_GET['category']]) + ->orderByDesc('starred')->orderByDesc('created_by')->get(); } if (isset($_GET['search'])) { $template = 'productList.html.twig'; @@ -26,9 +27,12 @@ if (isset($_GET['search'])) { ->orWhere([['name', 'like', "%{$_GET['search']}%"]]) ->orWhere([['subTitle', 'like', "%{$_GET['search']}%"]])->get(); } +if (isset($_GET['star'])) { + Product::find($_GET['star'])->toggleStarred()->save(); + header('Location: '.$_SERVER['HTTP_REFERER']); +} if (empty($_GET)) { $template = 'productList.html.twig'; - $products = Product::with('price')->get(); + $products = Product::with('price')->orderByDesc('starred')->orderByDesc('created_by')->get(); } - $twig->display($template, ['products' => $products, 'product' => $product, 'search' => $_GET['search']]); diff --git a/migration.php b/migration.php index 4326cec..3a7fe3c 100644 --- a/migration.php +++ b/migration.php @@ -31,6 +31,7 @@ if (!Capsule::schema()->hasTable('products')) { $table->string('variantCode'); $table->string('modelCode'); $table->string('url'); + $table->boolean('starred')->default(false); $table->timestamps(); }); } diff --git a/src/Models/Product.php b/src/Models/Product.php index c332e02..5ecf358 100644 --- a/src/Models/Product.php +++ b/src/Models/Product.php @@ -18,6 +18,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany; * @property string $variantCode * @property string $modelCode * @property string $url + * @property boolean $starred */ class Product extends Model { @@ -29,6 +30,12 @@ class Product extends Model return $this->hasMany(Price::class); } + public function toggleStarred(): self + { + $this->starred = !$this->starred; + return $this; + } + protected function categories(): Attribute { return Attribute::make( diff --git a/src/templates/productList.html.twig b/src/templates/productList.html.twig index fea02b6..074080a 100644 --- a/src/templates/productList.html.twig +++ b/src/templates/productList.html.twig @@ -4,18 +4,19 @@ {% for product in products %} + - - - + + - - + + {% endfor %}
{% if product.starred %}★{% else %} ☆ {% endif %} {{ product.name }}{{ product.name }}{{ product.subTitle }} + {{ product.name }}{{ product.subTitle }} link{{ product.price.last.price }}link{{ product.price.last.price }}