126 lines
3.7 KiB
PHP
126 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace Modules\Product\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Modules\Client\Interfaces\ClientInterface;
|
|
use Modules\Product\Interfaces\ProductInterface;
|
|
use Modules\Product\Models\Product;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class ProductController extends Controller
|
|
{
|
|
private $product;
|
|
private $client;
|
|
|
|
public function __construct(ProductInterface $product, ClientInterface $client)
|
|
{
|
|
$this->product = $product;
|
|
$this->client = $client;
|
|
}
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$data['title'] = 'Product';
|
|
$data['status'] = Product::STATUS;
|
|
$data['clients'] = $this->client->pluck();
|
|
if ($request->ajax()) {
|
|
$model = Product::query();
|
|
|
|
return DataTables::eloquent($model)
|
|
->addIndexColumn()
|
|
->setRowClass('{{"text-center align-middle"}}')
|
|
->addColumn('client', function (Product $product) {
|
|
return $product->client?->name;
|
|
})
|
|
->addColumn('status', '{!! $status_name !!}')
|
|
->addColumn('action', 'product::products.datatables.action-btn')
|
|
->rawColumns(['client', 'action', 'status','desc'])
|
|
->make(true);
|
|
// ->toJson();
|
|
}
|
|
return view('product::products.index', $data);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
$data['title'] = 'Create Product';
|
|
$data['editable'] = false;
|
|
|
|
return view('product::products.create', $data);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request): RedirectResponse
|
|
{
|
|
try {
|
|
|
|
$this->product->create($request->all());
|
|
return redirect()->route('product.index')->with('sucess', 'Product has been created!');
|
|
|
|
} catch (\Throwable $th) {
|
|
return redirect()->back()->withError($th->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show the specified resource.
|
|
*/
|
|
public function show($id)
|
|
{
|
|
$data['title'] = 'Product List';
|
|
$data['product'] = $this->product->getProductById($id);
|
|
return view('product::products.show', $data);
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
$data['title'] = 'Edit Product';
|
|
$data['editable'] = true;
|
|
$data['status'] = Product::STATUS;
|
|
$data['product'] = $this->product->getProductById($id);
|
|
$data['clients'] = $this->client->pluck();
|
|
return view('product::products.index', $data);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, $id): RedirectResponse
|
|
{
|
|
try {
|
|
$this->product->update($id, $request->except(['_token', '_method']));
|
|
return redirect()->route('product.index')->with('success', 'Product has been updated!');
|
|
|
|
} catch (\Throwable $th) {
|
|
return redirect()->back()->withError($th->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
try {
|
|
$this->product->delete($id);
|
|
return response()->json(['status' => 200, 'message' => 'Product has been deleted!'], 200);
|
|
} catch (\Throwable $th) {
|
|
return response()->json(['status' => 500, 'message' => 'Product to delete!', 'error' => $th->getMessage()], 500);
|
|
}
|
|
}
|
|
}
|