Skip to content

Commit

Permalink
Feature: FlashSale
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesChou committed Jul 9, 2023
1 parent a681d7a commit 20c69d6
Show file tree
Hide file tree
Showing 16 changed files with 548 additions and 1 deletion.
82 changes: 82 additions & 0 deletions app/Http/Controllers/Admin/FlashSaleController.php
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'));
}
}
42 changes: 42 additions & 0 deletions app/Http/Controllers/Api/FlashSaleAll.php
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();
}
}
48 changes: 48 additions & 0 deletions app/Http/Controllers/User/DirectBuy.php
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');
}
}
10 changes: 9 additions & 1 deletion app/Http/Controllers/User/Shop.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,15 +13,22 @@
*/
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')
->take(24)
->get();

return ViewFactory::make('shop.index')->with([
'flashSaleProducts' => $flashSaleProducts,
'newProducts' => $randomProducts,
]);
}
Expand Down
26 changes: 26 additions & 0 deletions app/Models/FlashSale.php
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 database/migrations/2020_11_06_010658_create_flash_sales_table.php
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');
}
}
8 changes: 8 additions & 0 deletions database/seeders/ProductSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Database\Seeders;

use App\Models\FlashSale;
use App\Models\Product;
use App\Models\ProductImage;
use Illuminate\Database\Seeder;
Expand Down Expand Up @@ -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),
]);
}
}
}
}
48 changes: 48 additions & 0 deletions resources/views/admin/flash-sale/create.blade.php
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
Loading

0 comments on commit 20c69d6

Please sign in to comment.