¨4.0.1¨
This commit is contained in:
@@ -2,9 +2,15 @@
|
||||
|
||||
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
|
||||
{
|
||||
@@ -15,26 +21,124 @@ class ProductController
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $model = Product::class;
|
||||
protected string $model = Product::class;
|
||||
|
||||
/**
|
||||
* Label of the resource.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $label = 'product::products.product';
|
||||
protected string $label = 'product::products.product';
|
||||
|
||||
/**
|
||||
* View path of the resource.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $viewPath = 'product::admin.products';
|
||||
protected string $viewPath = 'product::admin.products';
|
||||
|
||||
/**
|
||||
* Form requests for the resource.
|
||||
*
|
||||
* @var array|string
|
||||
*/
|
||||
protected $validation = SaveProductRequest::class;
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user