diff --git a/app/Http/Controllers/Admin/FlashSaleController.php b/app/Http/Controllers/Admin/FlashSaleController.php new file mode 100644 index 00000000..adc38809 --- /dev/null +++ b/app/Http/Controllers/Admin/FlashSaleController.php @@ -0,0 +1,82 @@ +middleware('role:admin'); + } + public function index() + { + return view('admin.flash-sale.index'); + } + public function create() + { + return view('admin.flash-sale.create'); + } + + public function store(Request $request) + { + $request->validate([ + 'product_code' => 'required', + 'flash_price' => 'required', + ]); + + $product = Product::where('product_code', $request->product_code)->first(); + if (!$product) { + Alert::toast('Product not found!', 'error'); + return redirect(route('flashSale.create')); + } + //flash price should be less than the actual price + if ($request->flash_price > $product->price) { + $request->flash_price = $product->price; + } + + $flashSale = new FlashSale(); + $flashSale->product_id = $product->id; + $flashSale->flash_price = $request->flash_price; + $flashSale->save(); + + Alert::toast('Added', 'success'); + return redirect(route('flashSale.index')); + } + + public function edit(FlashSale $id) + { + //here the id become an instance of flashsale + $product = Product::where('id', $id->product_id)->with('productImage')->first(); + return view('admin.flash-sale.edit')->with([ + 'flashSale' => $id, //here the id is an instance of flashsale + 'product' => $product, + ]); + } + + public function update(Request $request, $id) + { + $request->validate([ + 'flash_price' => 'required' + ]); + + $flashSale = FlashSale::findOrFail($id); + $flashSale->flash_price = $request->flash_price; + $flashSale->update(); + Alert::toast('Updated', 'success'); + return redirect(route('flashSale.index')); + } + + public function destroy(Request $request) + { + $flashSale = FlashSale::find($request->id); + $flashSale->delete(); + Alert::toast('Deleted!', 'success'); + return redirect(route('flashSale.index')); + } +} diff --git a/app/Http/Controllers/Api/FlashSaleAll.php b/app/Http/Controllers/Api/FlashSaleAll.php new file mode 100644 index 00000000..a98df8a6 --- /dev/null +++ b/app/Http/Controllers/Api/FlashSaleAll.php @@ -0,0 +1,42 @@ +latest()->get(); + + return DataTables::of($flashProducts) + ->addColumn('select', function ($row) { + return ' + '; + }) + ->addColumn('created_at', function ($row) { + return date('d/m/Y h:i A', strtotime($row->created_at)); + }) + ->addColumn('product_title', function ($row) { + return '' . $row->product->title . ''; + }) + ->addColumn('product_code', function ($row) { + return $row->product->product_code; + }) + ->addColumn('product_price', function ($row) { + return number_format($row->product->price); + }) + ->addColumn('flash_price', function ($row) { + return number_format($row->flash_price); + }) + ->addColumn('action', function ($row) { + return ' Edit'; + }) + ->rawColumns(['select', 'product_title', 'product_code', 'product_price', 'action']) + ->make(); + } +} diff --git a/app/Http/Controllers/User/DirectBuy.php b/app/Http/Controllers/User/DirectBuy.php new file mode 100644 index 00000000..b6329b47 --- /dev/null +++ b/app/Http/Controllers/User/DirectBuy.php @@ -0,0 +1,48 @@ +middleware('auth'); + } + + public function __invoke(DirectBuyRequest $request, FlashSale $flashSale): RedirectResponse + { + /** @var FlashSale $flashProduct */ + $flashProduct = $flashSale->newQuery() + ->where('id', $request->id) + ->with('product') + ->first(); + + $order = new Order(); + $order->user_id = Auth::user()->getAuthIdentifier(); + $order->product_id = $flashProduct->product_id; + $order->shipping_cost = 100; + $order->order_number = rand(200, 299) . '' . Carbon::now()->timestamp; + $order->price = $flashProduct->flash_price; + + if ($order->save()) { + Alert::toast('Order Placed!', 'success'); + } else { + Alert::toast('Checkout fail' . 'error'); + } + + return Redirect::route('myOrder.index'); + } +} diff --git a/app/Http/Controllers/User/Shop.php b/app/Http/Controllers/User/Shop.php index 4f1c476b..bf5f699e 100644 --- a/app/Http/Controllers/User/Shop.php +++ b/app/Http/Controllers/User/Shop.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\User; use App\Http\Controllers\Controller; +use App\Models\FlashSale; use App\Models\Product; use Illuminate\Contracts\View\View; use Illuminate\Support\Facades\View as ViewFactory; @@ -12,8 +13,14 @@ */ class Shop extends Controller { - public function __invoke(Product $product): View + public function __invoke(FlashSale $flashSale, Product $product): View { + $flashSaleProducts = $flashSale->newQuery() + ->inRandomOrder() + ->with('product.productImage') + ->take(6) + ->get(); + $randomProducts = $product->newQuery() ->inRandomOrder() ->with('productImage') @@ -21,6 +28,7 @@ public function __invoke(Product $product): View ->get(); return ViewFactory::make('shop.index')->with([ + 'flashSaleProducts' => $flashSaleProducts, 'newProducts' => $randomProducts, ]); } diff --git a/app/Models/FlashSale.php b/app/Models/FlashSale.php new file mode 100644 index 00000000..3859cf65 --- /dev/null +++ b/app/Models/FlashSale.php @@ -0,0 +1,26 @@ +belongsTo(Product::class); + } +} diff --git a/database/migrations/2020_11_06_010658_create_flash_sales_table.php b/database/migrations/2020_11_06_010658_create_flash_sales_table.php new file mode 100644 index 00000000..f2994246 --- /dev/null +++ b/database/migrations/2020_11_06_010658_create_flash_sales_table.php @@ -0,0 +1,33 @@ +id(); + $table->unsignedBigInteger('product_id')->unique(); + $table->string('flash_price'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('flash_sales'); + } +} diff --git a/database/seeders/ProductSeeder.php b/database/seeders/ProductSeeder.php index 32ec2ad9..9752581e 100644 --- a/database/seeders/ProductSeeder.php +++ b/database/seeders/ProductSeeder.php @@ -2,6 +2,7 @@ namespace Database\Seeders; +use App\Models\FlashSale; use App\Models\Product; use App\Models\ProductImage; use Illuminate\Database\Seeder; @@ -307,6 +308,13 @@ public function run() 'original' => 'images/products/' . $product['img'] . '.jpg', 'thumbnail' => 'images/products/' . $product['img'] . '.jpg', ]); + + if ($newProduct->id < 11) { + $flashSale = FlashSale::create([ + 'product_id' => $newProduct->id, + 'flash_price' => $newProduct->price - (0.1 * $newProduct->price), + ]); + } } } } diff --git a/resources/views/admin/flash-sale/create.blade.php b/resources/views/admin/flash-sale/create.blade.php new file mode 100644 index 00000000..7a883475 --- /dev/null +++ b/resources/views/admin/flash-sale/create.blade.php @@ -0,0 +1,48 @@ +@extends('layouts.admin') + +@section('content') +
Price : Rs.{{number_format($product->price)}}
+Sale Price : Rs.{{number_format($product->sale_price)}}
+ @else +Price : Rs.{{number_format($product->price)}}
+ @endif +{{substr($item->product->title,0,25)}}..
+ +Rs. {{number_format($item->flash_price)}}
+