diff --git a/app/Http/Controllers/Admin/CarouselController.php b/app/Http/Controllers/Admin/CarouselController.php new file mode 100644 index 00000000..27c1307b --- /dev/null +++ b/app/Http/Controllers/Admin/CarouselController.php @@ -0,0 +1,114 @@ +paginate(15); + return view('admin.carousel.index', compact('carousels')); + } + + public function create() + { + return view('admin.carousel.create'); + } + + public function store(Request $request) + { + $request->validate([ + 'caption' => 'required|min:3', + 'file' => 'required|image|max:5000' + ]); + + if ($this->saveCarousel($request)) { + Alert::toast('Carousel created!', 'success'); + } else { + Alert::toast('Something went wrong!'); + } + return redirect(route('carousel.index')); + } + + public function edit(Carousel $carousel) + { + return view('admin.carousel.edit', compact('carousel')); + } + + public function update(Request $request, Carousel $carousel) + { + $request->validate([ + 'caption' => 'required|min:3', + 'file' => 'sometimes|image|max:5000' + ]); + + if ($this->updateCarousel($request, $carousel)) { + Alert::toast('Carousel Updated!', 'success'); + } else { + Alert::toast('Something went wrong!'); + } + return redirect(route('carousel.index')); + } + + + public function destroy(Carousel $carousel) + { + File::delete(public_path($carousel->img)); + $carousel->delete(); + + Alert::toast('Carousel deleted!', 'success'); + return redirect(route('carousel.index')); + } + + private function updateCarousel($request, $carousel) + { + if ($request->hasFile('file')) { + $basename = Str::random(); + $original = $basename . '.' . $request->file->getClientOriginalExtension(); + + + //delete old image + File::delete([ + public_path($carousel->img), + ]); + + if ( + $carousel->update([ + 'caption' => $request->caption, + 'img' => '/images/banner/' . $original, + ]) + ) { + $request->file->move(public_path('/images/banner'), $original); + return true; + } + } else { + $carousel->caption = $request->caption; + $carousel->save(); + return true; + } + } + + private function saveCarousel($request) + { + //setting file upload + $basename = Str::random(); + $original = $basename . '.' . $request->file->getClientOriginalExtension(); + + if ( + Carousel::create([ + 'caption' => $request->caption, + 'img' => '/images/banner/' . $original, + ]) + ) { + $request->file->move(public_path('/images/banner'), $original); + return true; + } + } +} diff --git a/app/Http/Controllers/User/Shop.php b/app/Http/Controllers/User/Shop.php index 4f1c476b..f28ebfdf 100644 --- a/app/Http/Controllers/User/Shop.php +++ b/app/Http/Controllers/User/Shop.php @@ -3,6 +3,8 @@ namespace App\Http\Controllers\User; use App\Http\Controllers\Controller; +use App\Models\Carousel; +use App\Models\FlashSale; use App\Models\Product; use Illuminate\Contracts\View\View; use Illuminate\Support\Facades\View as ViewFactory; @@ -12,8 +14,13 @@ */ class Shop extends Controller { - public function __invoke(Product $product): View + public function __invoke(Carousel $carousel, Product $product): View { + $carousels = $carousel->newQuery() + ->latest() + ->take(3) + ->get(); + $randomProducts = $product->newQuery() ->inRandomOrder() ->with('productImage') @@ -21,6 +28,7 @@ public function __invoke(Product $product): View ->get(); return ViewFactory::make('shop.index')->with([ + 'carousels' => $carousels, 'newProducts' => $randomProducts, ]); } diff --git a/app/Models/Carousel.php b/app/Models/Carousel.php new file mode 100644 index 00000000..e5b5f2be --- /dev/null +++ b/app/Models/Carousel.php @@ -0,0 +1,21 @@ +id(); + $table->string('img'); + $table->string('caption'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('carousels'); + } +} diff --git a/database/seeders/CarouselSeeder.php b/database/seeders/CarouselSeeder.php new file mode 100644 index 00000000..bec06b2e --- /dev/null +++ b/database/seeders/CarouselSeeder.php @@ -0,0 +1,39 @@ + 'This is Caption1', + 'img' => '/images/banner/1.webp' + ], + [ + 'caption' => 'This is Caption2', + 'img' => '/images/banner/2.webp' + ], + [ + 'caption' => 'This is Caption3', + 'img' => '/images/banner/3.webp' + ] + ]; + + foreach ($carousels as $item) { + Carousel::create([ + 'caption' => $item['caption'], + 'img' => $item['img'] + ]); + } + } +} diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 235535cd..13b78b7d 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -17,6 +17,7 @@ public function run() RoleSeeder::class, UserSeeder::class, CategorySeeder::class, + CarouselSeeder::class, ProductSeeder::class, ]); } diff --git a/resources/views/admin/carousel/create.blade.php b/resources/views/admin/carousel/create.blade.php new file mode 100644 index 00000000..a2fca9f8 --- /dev/null +++ b/resources/views/admin/carousel/create.blade.php @@ -0,0 +1,51 @@ +@extends('layouts.admin') + +@section('content') +
+ +
+
+

Creating a banner

+ Go back +
+
+ +
+
+
+
+ @if($errors->any()) +
+ {{ implode('', $errors->all(':message')) }} +
+ @endif +
+ @csrf +
+ + + @error('caption') + + {{ $message }} + + @enderror +
+ +
+ + + @error('file') + + {{ $message }} + + @enderror +
+ + +
+
+
+
+
+
+@endsection \ No newline at end of file diff --git a/resources/views/admin/carousel/edit.blade.php b/resources/views/admin/carousel/edit.blade.php new file mode 100644 index 00000000..d1241cd4 --- /dev/null +++ b/resources/views/admin/carousel/edit.blade.php @@ -0,0 +1,52 @@ +@extends('layouts.admin') + +@section('content') +
+ +
+
+

Updating a banner

+ Go back +
+
+ +
+
+
+
+ @if($errors->any()) +
+ {{ implode('', $errors->all(':message')) }} +
+ @endif +
+ @csrf @method('PUT') +
+ + + @error('caption') + + {{ $message }} + + @enderror +
+ +
+ carousel image + + + @error('file') + + {{ $message }} + + @enderror +
+ + +
+
+
+
+
+
+@endsection \ No newline at end of file diff --git a/resources/views/admin/carousel/index.blade.php b/resources/views/admin/carousel/index.blade.php new file mode 100644 index 00000000..716ef7a0 --- /dev/null +++ b/resources/views/admin/carousel/index.blade.php @@ -0,0 +1,61 @@ +@extends('layouts.admin') + +@section('content') +
+ +
+
+

Front page carousel/banner management

+ Add a carousel +
+ +
+ +
+
+
+
+ + + + + + + + + + + + @if($carousels->count()) + @foreach($carousels as $carousel) + + + + + + + + @endforeach + @else + + + + + + + @endif + +
#CaptionImageCreated AtActions
{{$carousel->id}}{{$carousel->caption}}{{$carousel->created_at->diffForHumans()}} + Edit +
+ @csrf @method('delete') + +
+
No carousel found. Create one now
+
+ +
+
+
+
+@endsection diff --git a/resources/views/inc/admin/sidebar.blade.php b/resources/views/inc/admin/sidebar.blade.php index d56c6186..cb73602b 100644 --- a/resources/views/inc/admin/sidebar.blade.php +++ b/resources/views/inc/admin/sidebar.blade.php @@ -82,6 +82,9 @@ Manage Categories + + Manage Carousel + diff --git a/resources/views/shop/index.blade.php b/resources/views/shop/index.blade.php index 72f08a6b..40156afb 100644 --- a/resources/views/shop/index.blade.php +++ b/resources/views/shop/index.blade.php @@ -3,6 +3,32 @@ @section('page-title', 'Buy products online with great discount') @section('content') + {{-- carousel --}} + + {{-- categories-section --}}

Categories

diff --git a/routes/admin.php b/routes/admin.php index c1d82c32..9281bb65 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -2,6 +2,7 @@ use App\Http\Controllers\Admin\AdminController; use App\Http\Controllers\Admin\AdminLoginController; +use App\Http\Controllers\Admin\CarouselController; use App\Http\Controllers\Admin\CategoryController; use App\Http\Controllers\Admin\DeliveredController; use App\Http\Controllers\Admin\InvoiceController; @@ -41,6 +42,8 @@ Route::get('/user-management/get-all-users', [UserManagementController::class, 'getAllUsers']) ->name('userManagement.getAllUsers'); + Route::resource('/carousel', CarouselController::class); + Route::get('/category', [CategoryController::class, 'index']) ->name('category.index'); Route::get('/category/create', [CategoryController::class, 'create']) diff --git a/tests/Feature/Http/Controllers/CarouselControllerTest.php b/tests/Feature/Http/Controllers/CarouselControllerTest.php new file mode 100644 index 00000000..c491ca0c --- /dev/null +++ b/tests/Feature/Http/Controllers/CarouselControllerTest.php @@ -0,0 +1,34 @@ +get('/carousel'); + + $response->assertOk(); + } + + #[Test] + public function usersAreForbiddenToVisitCarouselPage() + { + Artisan::call('db:seed'); + Auth::loginUsingId(2); //user according to the seeder + + $response = $this->get('/carousel'); + $response->assertForbidden(); + } +}