From f214f8aab4c1f2c71758abcc0730a37c74cf29ab Mon Sep 17 00:00:00 2001 From: UronShrestha Date: Sun, 14 Jul 2024 15:16:39 +0545 Subject: [PATCH] "Added admin type checks and redirects in AdminController and MainController; updated product views and admin header" --- app/Http/Controllers/AdminController.php | 99 +++++++++++-------- app/Http/Controllers/MainController.php | 38 +++++-- resources/views/Dashboard/products.blade.php | 3 +- .../views/components/adminheader.blade.php | 4 +- 4 files changed, 89 insertions(+), 55 deletions(-) diff --git a/app/Http/Controllers/AdminController.php b/app/Http/Controllers/AdminController.php index 1e57f9c..51530ec 100644 --- a/app/Http/Controllers/AdminController.php +++ b/app/Http/Controllers/AdminController.php @@ -10,12 +10,18 @@ class AdminController extends Controller { public function index() { - return view('Dashboard.index'); + if (session()->get('type') == 'Admin') { + return view('Dashboard.index'); + } + return redirect()->back(); } public function products() { - $products = Product::all(); - return view('Dashboard.products', compact('products')); + if (session()->get('type') == 'Admin') { + $products = Product::all(); + return view('Dashboard.products', compact('products')); + } + return redirect()->back(); } // public function addNewProduct() // { @@ -58,26 +64,29 @@ class AdminController extends Controller //mass assignment for adding new product public function addNewProduct(Request $request) { - $validated = $request->validate([ - 'name' => 'required', - 'price' => 'required|numeric', - 'description' => 'required', - 'file' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048', - 'quantity' => 'required|numeric', - 'category' => 'required', - 'type' => 'required', - ]); + if (session()->get('type') == 'Admin') { + $validated = $request->validate([ + 'name' => 'required', + 'price' => 'required|numeric', + 'description' => 'required', + 'file' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048', + 'quantity' => 'required|numeric', + 'category' => 'required', + 'type' => 'required', + ]); - if ($request->hasFile('file')) { - $file = $request->file('file'); - $fileName = time() . '_' . $file->getClientOriginalName(); - $file->move(public_path('uploads/products'), $fileName); - $validated['picture'] = $fileName; + if ($request->hasFile('file')) { + $file = $request->file('file'); + $fileName = time() . '_' . $file->getClientOriginalName(); + $file->move(public_path('uploads/products'), $fileName); + $validated['picture'] = $fileName; + } + + Product::create($validated); + + return redirect()->back()->with('success', 'Product Added Successfully'); } - - Product::create($validated); - - return redirect()->back()->with('success', 'Product Added Successfully'); + return redirect()->back(); } @@ -114,34 +123,38 @@ class AdminController extends Controller //mass assignment for update public function updateProduct(Request $request) { - $validated = $request->validate([ - 'name' => 'required', - 'price' => 'required|numeric', - 'description' => 'required', - 'quantity' => 'required|numeric', - 'category' => 'required', - 'type' => 'required', - 'file' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048', - ]); + if (session()->get('type') == 'Admin') { + $validated = $request->validate([ + 'name' => 'required', + 'price' => 'required|numeric', + 'description' => 'required', + 'quantity' => 'required|numeric', + 'category' => 'required', + 'type' => 'required', + 'file' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048', + ]); - $product = Product::find($request->input('id')); + $product = Product::find($request->input('id')); - if ($request->hasFile('file')) { - $file = $request->file('file'); - $fileName = time() . '_' . $file->getClientOriginalName(); - $file->move(public_path('uploads/products'), $fileName); - $validated['picture'] = $fileName; + if ($request->hasFile('file')) { + $file = $request->file('file'); + $fileName = time() . '_' . $file->getClientOriginalName(); + $file->move(public_path('uploads/products'), $fileName); + $validated['picture'] = $fileName; + } + $product->update($validated); + return redirect()->back()->with('success', 'Product Updated Successfully'); } - - $product->update($validated); - - return redirect()->back()->with('success', 'Product Updated Successfully'); + return redirect()->back(); } public function deleteProduct($id) { - $product = Product::find($id); - $product->delete(); - return redirect()->back()->with('success', 'Product Deleted Successfully'); + if (session()->get('type') == 'Admin') { + $product = Product::find($id); + $product->delete(); + return redirect()->back()->with('success', 'Product Deleted Successfully'); + } + return redirect()->back(); } } diff --git a/app/Http/Controllers/MainController.php b/app/Http/Controllers/MainController.php index ce7310e..ad6d63c 100644 --- a/app/Http/Controllers/MainController.php +++ b/app/Http/Controllers/MainController.php @@ -20,13 +20,16 @@ class MainController extends Controller { public function index() { - $allProducts = Products::all(); - //dd($allProducts); - $newArrival = Products::where('type', 'new-arrival')->get(); - $hotSale = Products::where('type', 'sale')->get(); + if (session()->get('type') == 'Customer') { + $allProducts = Products::all(); + //dd($allProducts); + $newArrival = Products::where('type', 'new-arrival')->get(); + $hotSale = Products::where('type', 'sale')->get(); - return view('index', compact('allProducts', 'hotSale', 'newArrival')); + return view('index', compact('allProducts', 'hotSale', 'newArrival')); + } + return redirect()->back(); } public function about() @@ -82,18 +85,37 @@ class MainController extends Controller return view('login'); } + // public function loginUser(Request $data) + // { + // $user = User::where('email', $data->input('email'))->first(); + // if ($user && Hash::check($data->input('password'), $user->password)) { // Verifying the hashed password + // session()->put('id', $user->id); + // session()->put('type', $user->type); + // if ($user->type == 'Customer') { + // return redirect('/'); + // }else if($user->type == 'Admin'){ + // return redirect('/admin)} + // } else { + // return redirect('login')->with('error', 'Invalid email or password!'); + // } + // } + public function loginUser(Request $data) { $user = User::where('email', $data->input('email'))->first(); - if ($user && Hash::check($data->input('password'), $user->password)) { // Verifying the hashed password + + if ($user && Hash::check($data->input('password'), $user->password)) { session()->put('id', $user->id); session()->put('type', $user->type); + if ($user->type == 'Customer') { return redirect('/'); + } else if ($user->type == 'Admin') { + return redirect('/admin'); } - } else { - return redirect('login')->with('error', 'Invalid email or password!'); } + + return redirect('login')->with('error', 'Invalid email or password!'); } diff --git a/resources/views/Dashboard/products.blade.php b/resources/views/Dashboard/products.blade.php index 9b169c4..f019745 100644 --- a/resources/views/Dashboard/products.blade.php +++ b/resources/views/Dashboard/products.blade.php @@ -8,8 +8,7 @@

Welcome Aamir

-
All systems are running smoothly! You have - 3 unread alerts! +
All systems are running smoothly!
diff --git a/resources/views/components/adminheader.blade.php b/resources/views/components/adminheader.blade.php index 3f0f131..74269ad 100644 --- a/resources/views/components/adminheader.blade.php +++ b/resources/views/components/adminheader.blade.php @@ -15,7 +15,7 @@ - Skydash Admin + Admin Panel @@ -30,7 +30,7 @@ - +