Update source into github
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\CouponRequest;
|
||||
use App\Models\Discount;
|
||||
use App\Repositories\Discount\DiscountRepositoryInterface;
|
||||
use Illuminate\Contracts\Foundation\Application;
|
||||
use Illuminate\Contracts\View\Factory;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class CouponController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
public DiscountRepositoryInterface $discountRepository
|
||||
) {}
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
$title = "List Coupon";
|
||||
|
||||
$coupons = $this->discountRepository->filter($request->merge(['page' => $request->query('page') ?? 1])->all());
|
||||
|
||||
return view("admin.coupon.index", compact('title', 'coupons'));
|
||||
}
|
||||
public function create()
|
||||
{
|
||||
$title = "Create new Coupon";
|
||||
|
||||
return view("admin.coupon.add", compact('title'));
|
||||
}
|
||||
public function store(CouponRequest $request)
|
||||
{
|
||||
|
||||
$request->validated();
|
||||
|
||||
$isExits = $this->discountRepository->findBy('code', $request->code);
|
||||
|
||||
if ($isExits) {
|
||||
return back()->withInput()->withErrors('Coupon code already exists');
|
||||
}
|
||||
if (Carbon::createFromFormat('d/m/Y', $request->start)->timestamp > Carbon::createFromFormat('d/m/Y', $request->end)->timestamp) {
|
||||
return back()->withInput()->withErrors('Coupon code expiration date is invalid');
|
||||
}
|
||||
|
||||
if (Carbon::createFromFormat('d/m/Y', $request->end)->timestamp < Carbon::now()->timestamp) {
|
||||
|
||||
return back()->withInput()->withErrors('Coupon code expiration date is invalid');
|
||||
}
|
||||
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$data = $request->all();
|
||||
$data['start'] = Carbon::createFromFormat('d/m/Y', $request->start)->timestamp;
|
||||
$data['end'] = Carbon::createFromFormat('d/m/Y', $request->end)->timestamp;
|
||||
$this->discountRepository->create($data);
|
||||
DB::commit();
|
||||
} catch (\Exception $error) {
|
||||
DB::rollBack();
|
||||
return back()->withInput()->withErrors($error->getMessage());
|
||||
}
|
||||
|
||||
return redirect()->route('coupon.index')->with('success', 'Added successfully');
|
||||
}
|
||||
public function edit(int $id = 0)
|
||||
{
|
||||
$coupon = $this->discountRepository->find($id);
|
||||
|
||||
if (! $coupon) {
|
||||
return back()->withErrors("Invalid request");
|
||||
}
|
||||
|
||||
$title = "Edit coupon #" . $coupon->code;
|
||||
|
||||
return view("admin.coupon.edit", compact('title', 'coupon'));
|
||||
}
|
||||
|
||||
public function update(int $id = 0, CouponRequest $request)
|
||||
{
|
||||
$coupon = $this->discountRepository->find($id);
|
||||
|
||||
if (! $coupon) {
|
||||
return back()->withErrors("Invalid request");
|
||||
}
|
||||
$request->validated();
|
||||
if ($request->code !== $coupon->code) {
|
||||
|
||||
$isExits = $this->discountRepository->findBy('code', $request->code);
|
||||
if ($isExits) {
|
||||
return back()->withInput()->withErrors('Coupon code already exists');
|
||||
}
|
||||
}
|
||||
|
||||
if (Carbon::createFromFormat('d/m/Y', $request->start)->timestamp > Carbon::createFromFormat('d/m/Y', $request->end)->timestamp) {
|
||||
return back()->withInput()->withErrors('Coupon code expiration date is invalid');
|
||||
}
|
||||
|
||||
if (Carbon::createFromFormat('d/m/Y', $request->end)->timestamp < Carbon::now()->timestamp) {
|
||||
|
||||
return back()->withInput()->withErrors('Coupon code expiration date is invalid');
|
||||
}
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$data = $request->all();
|
||||
$data['start'] = Carbon::createFromFormat('d/m/Y', $request->start)->timestamp;
|
||||
$data['end'] = Carbon::createFromFormat('d/m/Y', $request->end)->timestamp;
|
||||
$this->discountRepository->update($id,$data);
|
||||
DB::commit();
|
||||
} catch (\Exception $error) {
|
||||
DB::rollBack();
|
||||
return back()->withInput()->withErrors($error->getMessage());
|
||||
}
|
||||
|
||||
return redirect()->route('coupon.index')->with('success', 'Updated successfully');
|
||||
}
|
||||
public function destroy(int $id = 0)
|
||||
{
|
||||
$coupon = $this->discountRepository->find($id);
|
||||
|
||||
if (! $coupon) {
|
||||
return back()->withErrors("Invalid request");
|
||||
}
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$this->discountRepository->delete($id);
|
||||
DB::commit();
|
||||
} catch (\Exception $error) {
|
||||
DB::rollBack();
|
||||
return back()->withInput()->withErrors($error->getMessage());
|
||||
}
|
||||
|
||||
return back()->with('success', 'Deleted successfully');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user