From 4375ac1fc8201c2745bed04b12b2cc65d6c39de5 Mon Sep 17 00:00:00 2001 From: Jaroslav Drzik Date: Mon, 25 Dec 2023 17:23:45 +0100 Subject: [PATCH] Basic search --- .../Controllers/CountryCodeController.php | 4 + .../Controllers/IkeaProductsController.php | 2 +- .../Controllers/ProductsCompareController.php | 43 +++++++++ app/Models/CountryCode.php | 56 ++++++----- app/Models/IkeaProducts.php | 5 + resources/js/Pages/IkeaRoot.vue | 95 ++++++++++++++----- routes/web.php | 6 +- 7 files changed, 161 insertions(+), 50 deletions(-) create mode 100644 app/Http/Controllers/ProductsCompareController.php diff --git a/app/Http/Controllers/CountryCodeController.php b/app/Http/Controllers/CountryCodeController.php index 08b74ef..fa15d6b 100644 --- a/app/Http/Controllers/CountryCodeController.php +++ b/app/Http/Controllers/CountryCodeController.php @@ -22,6 +22,10 @@ class CountryCodeController extends Controller return CountryCode::active("Y"); } + public function codes() + { + return CountryCode::countryHash(); + } /** * Store a newly created resource in storage. * diff --git a/app/Http/Controllers/IkeaProductsController.php b/app/Http/Controllers/IkeaProductsController.php index a6d0f12..479a45c 100644 --- a/app/Http/Controllers/IkeaProductsController.php +++ b/app/Http/Controllers/IkeaProductsController.php @@ -6,7 +6,7 @@ use Illuminate\Http\Request; use Illuminate\Http\Client\Pool; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; -use PhpParser\Node\Expr\Cast\String_; + class IkeaProductsController extends Controller { diff --git a/app/Http/Controllers/ProductsCompareController.php b/app/Http/Controllers/ProductsCompareController.php new file mode 100644 index 0000000..6a3b962 --- /dev/null +++ b/app/Http/Controllers/ProductsCompareController.php @@ -0,0 +1,43 @@ +input("codes"); + $countries = $request->input("countries"); + + Log::info("{codes} {countries}", ["codes" => $codes, "countries" => $countries]); + + $codes = collect($codes); + // $countries = collect(json_decode($countries, true)); + + if ($countries != null && count($countries)) { + $hCountry = CountryCode::countryHash(); + $countries = $countries->map(function ($country) { + return $hCountry[$country]; + }); + } + + // $aCodes = $codes->map(function ($code) { + // return $code['code']; + // }); + + $aCodes = $codes['code']; + $products = IkeaProducts::where("code", $aCodes)->get(); + Log::info("{products}", ["products" => $products]); + return Inertia::render('IkeaRoot', [ + 'products' => $products + ]); + } +} diff --git a/app/Models/CountryCode.php b/app/Models/CountryCode.php index 6e43c4b..86c4ab8 100644 --- a/app/Models/CountryCode.php +++ b/app/Models/CountryCode.php @@ -7,30 +7,38 @@ use Illuminate\Database\Eloquent\Model; class CountryCode extends Model { - /** - * The table associated with the model. - * - * @var string - */ - protected $table = 'c_ikea_country_code'; + /** + * The table associated with the model. + * + * @var string + */ + protected $table = 'c_ikea_country_code'; - /** - * The primary key associated with the table. - * - * @var string - */ - protected $primaryKey = 'currency_code'; - /** - * Indicates if the model's ID is auto-incrementing. - * - * @var bool - */ - public $incrementing = false; - - use HasFactory; + /** + * The primary key associated with the table. + * + * @var string + */ + protected $primaryKey = 'currency_code'; + /** + * Indicates if the model's ID is auto-incrementing. + * + * @var bool + */ + public $incrementing = false; - protected function active(String $status) - { - return $this->where('status', $status)->get(); - } + use HasFactory; + + protected function active(String $status) + { + return $this->where('status', $status)->get(); + } + + protected function countryHash() + { + $codes = $this->select('country_name', 'country_code')->get(); + return $codes->mapWithKeys(function ($item) { + return [$item['country_name'] => $item['country_code']]; +}); + } } diff --git a/app/Models/IkeaProducts.php b/app/Models/IkeaProducts.php index ca43f15..de63728 100644 --- a/app/Models/IkeaProducts.php +++ b/app/Models/IkeaProducts.php @@ -32,4 +32,9 @@ class IkeaProducts extends Model return []; } } + + protected function multisearch($codes, $countries) { + //$countries = $ + // return $this->where('code',$codes)->where('country',$countries); + } } diff --git a/resources/js/Pages/IkeaRoot.vue b/resources/js/Pages/IkeaRoot.vue index 1d3d8b2..caa1d27 100644 --- a/resources/js/Pages/IkeaRoot.vue +++ b/resources/js/Pages/IkeaRoot.vue @@ -1,5 +1,5 @@