Writeoff/Http/Controllers/BranchController.php

161 lines
5.2 KiB
PHP
Raw Normal View History

2023-10-02 11:27:22 +00:00
<?php
namespace Modules\Writeoff\Http\Controllers;
use App\Http\Controllers\Controller;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Modules\Writeoff\DataTables\BranchDataTable;
use Modules\Writeoff\Entities\Branch;
use Modules\Writeoff\Http\Requests\Branch\StoreBranchRequest;
use Modules\Writeoff\Http\Requests\Branch\UpdateBranchRequest;
class BranchController extends Controller
{
public $user;
public function __construct()
{
$this->middleware(function ($request, $next) {
$this->user = Auth::guard('web')->user();
return $next($request);
});
}
/**
* Display a listing of the Branchs.
*
* @param \Modules\Writeoff\DataTables\BranchDataTable $dataTable
*
* @return mixed
*/
public function index(BranchDataTable $dataTable, Request $request)
{
if (is_null($this->user) || !$this->user->can('master.read')) {
abort(403, 'Sorry !! You are Unauthorized to view any master data !');
}
return $dataTable->render('writeoff::parameter.branches.index');
}
/**
* Store a newly created Branch in storage.
*
* @param \Modules\Writeoff\Http\Requests\Branch\StoreBranchRequest $request
*
* @return mixed
*/
public function store(StoreBranchRequest $request)
{
if (is_null($this->user) || !$this->user->can('master.create')) {
abort(403, 'Sorry !! You are Unauthorized to create any master data !');
}
// Validate the request...
$validated = $request->validated();
// Store the Branch...
if ($validated) {
try {
Branch::create($validated);
echo json_encode(['status' => 'success', 'message' => 'Branch created successfully.']);
} catch (Exception $e) {
echo json_encode(['status' => 'error', 'message' => 'Branch created failed.']);
}
return;
}
echo json_encode(['status' => 'error', 'message' => 'Branch created failed.']);
}
/**
* Show the form for creating a new Branch.
*/
public function create()
{
if (is_null($this->user) || !$this->user->can('master.create')) {
abort(403, 'Sorry !! You are Unauthorized to create any master data !');
}
abort(404);
}
/**
* Display the specified Branch.
*
* @param \Modules\Writeoff\Entities\Branch $directorat
*/
public function show(Branch $directorat)
{
if (is_null($this->user) || !$this->user->can('master.read')) {
abort(403, 'Sorry !! You are Unauthorized to view any master data !');
}
}
/**
* Show the form for editing the specified Branch.
*
* @param $id
*/
public function edit($id)
{
if (is_null($this->user) || !$this->user->can('master.update')) {
abort(403, 'Sorry !! You are Unauthorized to update any master data !');
}
$directorat = Branch::find($id);
echo json_encode($directorat);
}
/**
* Update the specified Branch in storage.
*
* @param \Modules\Writeoff\Http\Requests\Branch\UpdateBranchRequest $request
* @param \Modules\Writeoff\Entities\Branch $directorat
*
* @return mixed
*/
public function update(UpdateBranchRequest $request, Branch $directorat)
{
if (is_null($this->user) || !$this->user->can('master.update')) {
abort(403, 'Sorry !! You are Unauthorized to update any master data !');
}
// Validate the request...
$validated = $request->validated();
// Update the Branch...
if ($validated) {
try {
$directorat->update($validated);
echo json_encode(['status' => 'success', 'message' => 'Branch updated successfully.']);
} catch (Exception $e) {
echo json_encode(['status' => 'error', 'message' => 'Branch updated failed.']);
}
return;
}
echo json_encode(['status' => 'error', 'message' => 'Branch updated failed.']);
}
/**
* Remove the specified Branch from storage.
*
* @param \Modules\Writeoff\Entities\Branch $directorat
*
* @return void
*/
public function destroy(Branch $directorat)
{
if (is_null($this->user) || !$this->user->can('master.delete')) {
abort(403, 'Sorry !! You are Unauthorized to delete any master data !');
}
$directorat->delete();
echo json_encode(['status' => 'success', 'message' => 'Branch deleted successfully.']);
}
}