From 1eda1fe3c6c099dc6363357705fdafb395f67d29 Mon Sep 17 00:00:00 2001 From: A1Gard Date: Wed, 14 Aug 2024 08:58:39 +0330 Subject: [PATCH] improved code quality fixed some bugs --- app/Http/Controllers/CardController.php | 30 ++++++++++++- app/Http/Controllers/ClientController.php | 50 --------------------- app/Http/Controllers/CustomerController.php | 31 +++++++++++++ app/Http/Middleware/LangControl.php | 32 ++++++------- routes/web.php | 4 +- 5 files changed, 78 insertions(+), 69 deletions(-) diff --git a/app/Http/Controllers/CardController.php b/app/Http/Controllers/CardController.php index 0277a78..eb4ed5e 100644 --- a/app/Http/Controllers/CardController.php +++ b/app/Http/Controllers/CardController.php @@ -60,7 +60,7 @@ class CardController extends Controller $count = 1; $msg = "Product added to card"; \Cookie::queue('card', "[$product->id]", 2000); - \Cookie::queue('q', "[$quantity]", 2000); + \Cookie::queue('q', "[null]", 2000); $qs = [$quantity]; $cards = [$product->id]; } @@ -184,4 +184,32 @@ class CardController extends Controller ]; } } + + + + public function productCompareToggle($slug) + { + + $product = Product::where('slug', $slug)->firstOrFail(); + if (\Cookie::has('compares')) { + $compares = json_decode(\Cookie::get('compares'), true); + if (in_array($product->id, $compares)) { + $msg = __("Product removed from compare"); + unset($compares[array_search($product->id, $compares)]); + } else { + $compares[] = $product->id; + $msg = __( "Product added to compare"); + } + \Cookie::queue('compares', json_encode($compares), 2000); + } else { + $msg = "Product added to compare"; + \Cookie::queue('compares', "[$product->id]", 2000); + } + + if (\request()->ajax()) { + return success(null, $msg); + } else { + return redirect()->back()->with(['message' => $msg]); + } + } } diff --git a/app/Http/Controllers/ClientController.php b/app/Http/Controllers/ClientController.php index 297bdc8..cbebc98 100644 --- a/app/Http/Controllers/ClientController.php +++ b/app/Http/Controllers/ClientController.php @@ -398,58 +398,8 @@ class ClientController extends Controller } - public function productCompareToggle($slug) - { - $product = Product::where('slug', $slug)->firstOrFail(); - if (\Cookie::has('compares')) { - $compares = json_decode(\Cookie::get('compares'), true); - if (in_array($product->id, $compares)) { - $msg = "Product removed from compare"; - unset($compares[array_search($product->id, $compares)]); - } else { - $compares[] = $product->id; - $msg = "Product added to compare"; - } - \Cookie::queue('compares', json_encode($compares), 2000); - } else { - $msg = "Product added to compare"; - \Cookie::queue('compares', "[$product->id]", 2000); - } - if (\request()->ajax()) { - return success(null, $msg); - } else { - return redirect()->back()->with(['message' => $msg]); - } - } - - public function ProductFavToggle($slug) - { - $product = Product::where('slug', $slug)->firstOrFail(); - - if (!auth('customer')->check()) { - return errors([ - __("You need to login first"), - ], 403, __("You need to login first")); - } - - if (auth('customer')->user()->favorites()->where('product_id', $product->id)->count() == 0) { - auth('customer')->user()->favorites()->attach($product->id); - $message = __('Product added to favorites'); - $fav = '1'; - } else { - auth('customer')->user()->favorites()->detach($product->id); - $message = __('Product removed from favorites'); - $fav = '0'; - } - - if (\request()->ajax()) { - return success($fav, $message); - } else { - return redirect()->back()->with(['message' => $message]); - } - } public function compare() diff --git a/app/Http/Controllers/CustomerController.php b/app/Http/Controllers/CustomerController.php index 0f48eae..0948b8f 100644 --- a/app/Http/Controllers/CustomerController.php +++ b/app/Http/Controllers/CustomerController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Models\Invoice; +use App\Models\Product; use Illuminate\Http\Request; use Illuminate\Validation\Rules\In; @@ -63,4 +64,34 @@ class CustomerController extends Controller return $invoice; } + + public function ProductFavToggle($slug) + { + $product = Product::where('slug', $slug)->firstOrFail(); + + if (!auth('customer')->check()) { + return errors([ + __("You need to login first"), + ], 403, __("You need to login first")); + } + + if (auth('customer')->user()->favorites()->where('product_id', $product->id)->count() == 0) { + auth('customer')->user()->favorites()->attach($product->id); + $message = __('Product added to favorites'); + $fav = '1'; + } else { + auth('customer')->user()->favorites()->detach($product->id); + $message = __('Product removed from favorites'); + $fav = '0'; + } + + if (\request()->ajax()) { + return success($fav, $message); + } else { + return redirect()->back()->with(['message' => $message]); + } + } + + + } diff --git a/app/Http/Middleware/LangControl.php b/app/Http/Middleware/LangControl.php index 32d2cc6..f8026f1 100644 --- a/app/Http/Middleware/LangControl.php +++ b/app/Http/Middleware/LangControl.php @@ -27,22 +27,22 @@ class LangControl } else { app()->setLocale(config('app.locale')); } - array_shift($segments); - $url = \request()->path(); - $url = str_replace(app()->getLocale(), '', $url); - // Modify the request - $newPath = '/' . implode('/', $segments); - $newUrl = $request->root() . $newPath . ($request->getQueryString() ? '?'.$request->getQueryString() : ''); - - $request->initialize( - $request->query->all(), - $request->request->all(), - $request->attributes->all(), - $request->cookies->all(), - $request->files->all(), - $request->server->all() - ); - +// array_shift($segments); +// $url = \request()->path(); +// $url = str_replace(app()->getLocale(), '', $url); +// // Modify the request +// $newPath = '/' . implode('/', $segments); +// $newUrl = $request->root() . $newPath . ($request->getQueryString() ? '?'.$request->getQueryString() : ''); +// +// $request->initialize( +// $request->query->all(), +// $request->request->all(), +// $request->attributes->all(), +// $request->cookies->all(), +// $request->files->all(), +// $request->server->all() +// ); +// return $next($request); diff --git a/routes/web.php b/routes/web.php index ce65967..eb204a0 100644 --- a/routes/web.php +++ b/routes/web.php @@ -390,8 +390,8 @@ Route::middleware([\App\Http\Middleware\VisitorCounter::class]) Route::get('attach/download/{attachment}', [ClientController::class, 'attachDl'])->name('attach-dl'); Route::get('/post/{post}', [ClientController::class, 'post'])->name('post'); - Route::get('product/fav/toggle/{product}', [ClientController::class, 'ProductFavToggle'])->name('product-fav-toggle'); - Route::get('product/compare/toggle/{product}', [ClientController::class, 'productCompareToggle'])->name('product-compare-toggle'); + Route::get('product/fav/toggle/{product}', [\App\Http\Controllers\CustomerController::class, 'ProductFavToggle'])->name('product-fav-toggle'); + Route::get('product/compare/toggle/{product}', [\App\Http\Controllers\CardController::class, 'productCompareToggle'])->name('product-compare-toggle'); Route::get('card/toggle/{product}', [\App\Http\Controllers\CardController::class, 'productCardToggle'])->name('product-card-toggle'); Route::post('/comment/submit', [ClientController::class, 'submitComment'])->name('comment.submit');