Files
getgreen-backend/app/Http/Controllers/Dashboard/Brand/Controller.php
2026-04-16 11:25:40 +05:00

118 lines
3.3 KiB
PHP
Executable File

<?php
namespace App\Http\Controllers\Dashboard\Brand;
use App\Api\ImageResize;
use App\Models\Brand;
use App\Models\Product;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller as ExController;
use App\Http\Requests\Dashboard\Brand\Update as UpdateRequest;
use App\Http\Requests\Dashboard\Brand\Store as StoreRequest;
use App\Jobs\Dashboard\Brand\Store as StoreJob;
use App\Jobs\Dashboard\Brand\Update as UpdateJob;
use Illuminate\Support\Facades\Storage;
class Controller extends ExController
{
protected $brands;
/**
* Controller constructor.
* @param Brend $brand
*/
public function __construct(Brand $brand)
{
$this->brands = $brand;
}
/**
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function index()
{
$this->authorize('view', 'brands');
$brands = $this->brands->orderBy('position')->paginate(20);
return view('dashboard.brands.index', compact('brands'));
}
/**
* @param UpdateRequest $request
* @param Brand $brand
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function update(UpdateRequest $request, Brand $brand)
{
if ($request->isMethod('get')) {
$this->authorize('update', 'brands');
return view('dashboard.brands.update', compact('brand'));
}
if ($request->hasFile('image')) {
Storage::delete($brand->image);
$path = $this->storeImageToS3($request->file('image'));
} else {
$path = $brand->image;
}
$this->dispatchSync(UpdateJob::fromRequest($brand, $request, $path));
$this->info(trans('admin.messages.updated'));
return redirect()->route('dashboard.brands');
}
/**
* @param StoreRequest $request
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function store(StoreRequest $request)
{
if ($request->isMethod('get')) {
$this->authorize('create', 'brands');
return view('dashboard.brands.store');
}
if ($request->hasFile('image')) {
$path = $this->storeImageToS3($request->file('image'));
}
$this->dispatchSync(StoreJob::fromRequest($request, $path));
$this->info(trans('admin.messages.created'));
return redirect()->route('dashboard.brands');
}
/**
* @param Brand $brand
* @return \Illuminate\Http\RedirectResponse
* @throws \Exception
*/
public function delete(Brand $brand)
{
$this->authorize('delete', 'brands');
Storage::delete($brand->image);
Product::where('brand_id', $brand->id)->withTrashed()->update([
'brand_id' => null
]);
$brand->delete();
$this->info(trans('admin.messages.deleted'));
return redirect()->back();
}
private function storeImageToS3($image): string
{
$tempPath = $image->store('temp', 'public');
$resizer = new ImageResize();
return $resizer->resize($tempPath, 322, 'brands', true);
}
}