forked from nishangupta/Mart
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
548 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Admin; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\FlashSale; | ||
use App\Models\Product; | ||
use Illuminate\Http\Request; | ||
use RealRashid\SweetAlert\Facades\Alert; | ||
|
||
class FlashSaleController extends Controller | ||
{ | ||
public function __construct() | ||
{ | ||
$this->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')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api; | ||
|
||
use App\Models\FlashSale; | ||
use Illuminate\Http\JsonResponse; | ||
use Yajra\DataTables\DataTables; | ||
|
||
class FlashSaleAll | ||
{ | ||
public function __invoke(): JsonResponse | ||
{ | ||
$flashProducts = FlashSale::with('product')->latest()->get(); | ||
|
||
return DataTables::of($flashProducts) | ||
->addColumn('select', function ($row) { | ||
return '<input type="checkbox" name="ids[]" class="selectbox" value="' . $row->id . '"> | ||
'; | ||
}) | ||
->addColumn('created_at', function ($row) { | ||
return date('d/m/Y h:i A', strtotime($row->created_at)); | ||
}) | ||
->addColumn('product_title', function ($row) { | ||
return '<a href="catalog?filter[title]=' . $row->product->title . '">' . $row->product->title . '</a>'; | ||
}) | ||
->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 '<a target="_blank" class="btn btn-info btn-sm btn-block" href="/flash-sale/' | ||
. $row->id . '/edit" ><i class="fas fa-eye"></i> Edit</a>'; | ||
}) | ||
->rawColumns(['select', 'product_title', 'product_code', 'product_price', 'action']) | ||
->make(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\User; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Http\Requests\User\DirectBuyRequest; | ||
use App\Models\FlashSale; | ||
use App\Models\Order; | ||
use Carbon\Carbon; | ||
use Illuminate\Http\RedirectResponse; | ||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Support\Facades\Redirect; | ||
use RealRashid\SweetAlert\Facades\Alert; | ||
|
||
/** | ||
* @see \Tests\Feature\Http\Controllers\OrderControllerTest | ||
*/ | ||
class DirectBuy extends Controller | ||
{ | ||
public function __construct() | ||
{ | ||
$this->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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
|
||
/** | ||
* @property int id | ||
* @property int product_id | ||
* @property string flash_price | ||
* @property Carbon created_at | ||
* @property Carbon updated_at | ||
* @property Product product | ||
*/ | ||
class FlashSale extends Model | ||
{ | ||
use HasFactory; | ||
|
||
public function product(): BelongsTo | ||
{ | ||
return $this->belongsTo(Product::class); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
database/migrations/2020_11_06_010658_create_flash_sales_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateFlashSalesTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('flash_sales', function (Blueprint $table) { | ||
$table->id(); | ||
$table->unsignedBigInteger('product_id')->unique(); | ||
$table->string('flash_price'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('flash_sales'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
@extends('layouts.admin') | ||
|
||
@section('content') | ||
<div class="container-fluid"> | ||
|
||
<div class="card shadow mb-4"> | ||
<div class="card-header py-3"> | ||
<h6 class="m-0 font-weight-bold text-primary">Add products to flash sale</h6> | ||
</div> | ||
<div class="card-body"> | ||
<div class="row"> | ||
<div class="col-sm-12 col-md-8 pb-3"> | ||
|
||
@if($errors->any()) | ||
<div class="alert alert-danger"> | ||
{{ implode('', $errors->all(':message')) }} | ||
</div> | ||
@endif | ||
|
||
<form action="{{route('flashSale.store')}}" method="POST"> | ||
@csrf | ||
<div class="row"> | ||
<div class="col-sm-12 col-md-8"> | ||
<div class="form-group"> | ||
<label for="">Product code</label> <small class="text-info">[6 digit code of each product] <a href="{{route('product.index')}}">Find here</a> </small> | ||
<input type="text" placeholder="Product code" name="product_code" class="form-control mt-2" required> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<label for="">Flash sale price</label> | ||
<input type="text" placeholder="Price" name="flash_price" class="form-control" required> | ||
</div> | ||
|
||
<div class="d-flex justify-content-between"> | ||
<button type="submit" class="btn btn-primary my-3">Add to flash sale</button> | ||
</div> | ||
</div> | ||
</div> | ||
</form> | ||
|
||
</div> | ||
|
||
</div> | ||
</div> | ||
</div> | ||
|
||
</div> | ||
@endsection |
Oops, something went wrong.