175 lines
5.7 KiB
PHP
175 lines
5.7 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace Modules\Buyer\Http\Controllers;
|
||
|
|
||
|
use Exception;
|
||
|
use Illuminate\Contracts\Support\Renderable;
|
||
|
use Illuminate\Http\Request;
|
||
|
use Illuminate\Routing\Controller;
|
||
|
use Illuminate\Support\Facades\Auth;
|
||
|
use Modules\Buyer\DataTables\BuyerDataTable;
|
||
|
use Modules\Buyer\Entities\Buyer;
|
||
|
use Modules\Buyer\Http\Requests\StoreBuyerRequest;
|
||
|
use Modules\Buyer\Http\Requests\UpdateBuyerRequest;
|
||
|
|
||
|
class BuyerController extends Controller
|
||
|
{
|
||
|
protected $user;
|
||
|
protected $model = Buyer::class;
|
||
|
protected $module;
|
||
|
|
||
|
public function __construct()
|
||
|
{
|
||
|
$this->middleware(function ($request, $next) {
|
||
|
$this->user = Auth::guard('web')->user();
|
||
|
return $next($request);
|
||
|
});
|
||
|
|
||
|
$module = file_get_contents(dirname(__FILE__, 3) . '/module.json');
|
||
|
$this->module = json_decode($module);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Display a listing of the resource.
|
||
|
*
|
||
|
* @return Renderable
|
||
|
*/
|
||
|
public function index(BuyerDataTable $dataTable)
|
||
|
{
|
||
|
if (is_null($this->user) || !$this->user->can($this->module->alias . '.read')) {
|
||
|
abort(403, 'Sorry !! You are Unauthorized to view any ' . $this->module->alias . ' !');
|
||
|
}
|
||
|
|
||
|
return $dataTable->render($this->module->alias . '::index');
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Store a newly created resource in storage.
|
||
|
*
|
||
|
* @param Request $request
|
||
|
*
|
||
|
* @return Renderable
|
||
|
*/
|
||
|
public function store(StoreBuyerRequest $request)
|
||
|
{
|
||
|
if (is_null($this->user) || !$this->user->can($this->module->alias . '.create')) {
|
||
|
abort(403, 'Sorry !! You are Unauthorized to create any ' . $this->module->alias . ' !');
|
||
|
}
|
||
|
|
||
|
//Validate the request
|
||
|
$validated = $request->validated();
|
||
|
|
||
|
// Store the Buyer...
|
||
|
if ($validated) {
|
||
|
try {
|
||
|
$this->model::create($validated);
|
||
|
echo json_encode(['status' => 'success', 'message' => $this->module->name . ' created successfully.']);
|
||
|
} catch (Exception $e) {
|
||
|
echo json_encode(['status' => 'error', 'message' => $this->module->name . ' created failed.']);
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
echo json_encode(['status' => 'error', 'message' => $this->module->name . ' created failed.']);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Show the form for creating a new resource.
|
||
|
*
|
||
|
* @return Renderable
|
||
|
*/
|
||
|
public function create()
|
||
|
{
|
||
|
if (is_null($this->user) || !$this->user->can($this->module->alias . '.create')) {
|
||
|
abort(403, 'Sorry !! You are Unauthorized to create any ' . $this->module->alias . ' !');
|
||
|
}
|
||
|
|
||
|
abort(404);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Show the specified resource.
|
||
|
*
|
||
|
* @param int $id
|
||
|
*
|
||
|
* @return Renderable
|
||
|
*/
|
||
|
public function show($id)
|
||
|
{
|
||
|
if (is_null($this->user) || !$this->user->can($this->module->alias . '.read')) {
|
||
|
abort(403, 'Sorry !! You are Unauthorized to view any ' . $this->module->alias . ' !');
|
||
|
}
|
||
|
|
||
|
abort(404);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Show the form for editing the specified resource.
|
||
|
*
|
||
|
* @param int $id
|
||
|
*
|
||
|
* @return Renderable
|
||
|
*/
|
||
|
public function edit($id)
|
||
|
{
|
||
|
if (is_null($this->user) || !$this->user->can($this->module->alias . '.update')) {
|
||
|
abort(403, 'Sorry !! You are Unauthorized to update any ' . $this->module->alias . ' !');
|
||
|
}
|
||
|
|
||
|
$data = $this->model::find($id);
|
||
|
echo json_encode($data);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Update the specified resource in storage.
|
||
|
*
|
||
|
* @param Request $request
|
||
|
* @param int $id
|
||
|
*
|
||
|
* @return Renderable
|
||
|
*/
|
||
|
public function update(UpdateBuyerRequest $request, Buyer $buyer)
|
||
|
{
|
||
|
if (is_null($this->user) || !$this->user->can($this->module->alias . '.update')) {
|
||
|
abort(403, 'Sorry !! You are Unauthorized to update any ' . $this->module->alias . ' !');
|
||
|
}
|
||
|
|
||
|
//Validate the request
|
||
|
$validated = $request->validated();
|
||
|
|
||
|
// Update the Buyer...
|
||
|
if ($validated) {
|
||
|
try {
|
||
|
$buyer->update($validated);
|
||
|
echo json_encode(['status' => 'success', 'message' => $this->module->name . ' updated successfully.']);
|
||
|
} catch (Exception $e) {
|
||
|
echo json_encode(['status' => 'error', 'message' => $this->module->name . ' updated failed.']);
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
echo json_encode(['status' => 'error', 'message' => $this->module->name . ' updated failed.']);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Remove the specified resource from storage.
|
||
|
*
|
||
|
* @param int $id
|
||
|
*
|
||
|
* @return Renderable
|
||
|
*/
|
||
|
public function destroy(Buyer $buyer)
|
||
|
{
|
||
|
if (is_null($this->user) || !$this->user->can($this->module->alias . '.delete')) {
|
||
|
abort(403, 'Sorry !! You are Unauthorized to delete any ' . $this->module->alias . ' !');
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
$buyer->delete();
|
||
|
echo json_encode(['status' => 'success', 'message' => $this->module->name . ' deleted successfully.']);
|
||
|
} catch (Exception $e) {
|
||
|
echo json_encode(['status' => 'error', 'message' => $this->module->name . ' deleted failed.']);
|
||
|
}
|
||
|
}
|
||
|
}
|