FleetCart/Modules/Product/Http/Controllers/Admin/ProductController.php
2023-12-03 14:07:47 +00:00

145 lines
3.5 KiB
PHP

<?php
namespace Modules\Product\Http\Controllers\Admin;
use Illuminate\Http\Response;
use Illuminate\Http\JsonResponse;
use Illuminate\Contracts\View\View;
use Modules\Product\Entities\Product;
use Illuminate\Contracts\View\Factory;
use Illuminate\Foundation\Application;
use Modules\Admin\Traits\HasCrudActions;
use Modules\Product\Http\Requests\SaveProductRequest;
use Modules\Product\Transformers\ProductEditResource;
class ProductController
{
use HasCrudActions;
/**
* Model for the resource.
*
* @var string
*/
protected string $model = Product::class;
/**
* Label of the resource.
*
* @var string
*/
protected string $label = 'product::products.product';
/**
* View path of the resource.
*
* @var string
*/
protected string $viewPath = 'product::admin.products';
/**
* Form requests for the resource.
*
* @var array|string
*/
protected string|array $validation = SaveProductRequest::class;
/**
* Store a newly created resource in storage.
*
* @return Response|JsonResponse
*/
public function store()
{
$this->disableSearchSyncing();
$entity = $this->getModel()->create(
$this->getRequest('store')->all()
);
$this->searchable($entity);
$message = trans('admin::messages.resource_created', ['resource' => $this->getLabel()]);
if (request()->query('exit_flash')) {
session()->flash('exit_flash', $message);
}
if (request()->wantsJson()) {
return response()->json(
[
'success' => true,
'message' => $message,
'product_id' => $entity->id,
], 200
);
}
return redirect()->route("{$this->getRoutePrefix()}.index")
->withSuccess($message);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
*
* @return Factory|View|Application
*/
public function edit($id): Factory|View|Application
{
$entity = $this->getEntity($id);
$productEditResource = new ProductEditResource($entity);
return view("{$this->viewPath}.edit",
[
'product' => $entity,
'product_resource' => $productEditResource->response()->content(),
]
);
}
/**
* Update the specified resource in storage.
*
* @param int $id
*/
public function update($id)
{
$entity = $this->getEntity($id);
$this->disableSearchSyncing();
$entity->update(
$this->getRequest('update')->all()
);
$entity->withoutEvents(function () use ($entity) {
$entity->touch();
});
$productEditResource = new ProductEditResource($entity);
$this->searchable($entity);
$message = trans('admin::messages.resource_updated', ['resource' => $this->getLabel()]);
if (request()->query('exit_flash')) {
session()->flash('exit_flash', $message);
}
if (request()->wantsJson()) {
return response()->json(
[
'success' => true,
'message' => $message,
'product_resource' => $productEditResource,
], 200
);
}
}
}