Cetaklabel/Http/Controllers/DirectoratController.php

161 lines
5.3 KiB
PHP

<?php
namespace Modules\Cetaklabel\Http\Controllers;
use App\Http\Controllers\Controller;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Modules\Cetaklabel\DataTables\DirectoratDataTable;
use Modules\Cetaklabel\Entities\Directorat;
use Modules\Cetaklabel\Http\Requests\Directorat\StoreDirectoratRequest;
use Modules\Cetaklabel\Http\Requests\Directorat\UpdateDirectoratRequest;
class DirectoratController 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 Directorats.
*
* @param \App\DataTables\DirectoratDataTable $dataTable
*
* @return mixed
*/
public function index(DirectoratDataTable $dataTable, Request $request)
{
if (is_null($this->user) || !$this->user->can('masters.read')) {
abort(403, 'Sorry !! You are Unauthorized to view any master data !');
}
return $dataTable->render('cetaklabel::masters.directorat.index');
}
/**
* Store a newly created Directorat in storage.
*
* @param \App\Http\Requests\StoreDirectoratRequest $request
*
* @return mixed
*/
public function store(StoreDirectoratRequest $request)
{
if (is_null($this->user) || !$this->user->can('masters.create')) {
abort(403, 'Sorry !! You are Unauthorized to create any master data !');
}
// Validate the request...
$validated = $request->validated();
// Store the Directorat...
if ($validated) {
try {
Directorat::create($validated);
echo json_encode(['status' => 'success', 'message' => 'Directorat created successfully.']);
} catch (Exception $e) {
echo json_encode(['status' => 'error', 'message' => 'Directorat created failed.']);
}
return;
}
echo json_encode(['status' => 'error', 'message' => 'Directorat created failed.']);
}
/**
* Show the form for creating a new Directorat.
*/
public function create()
{
if (is_null($this->user) || !$this->user->can('masters.create')) {
abort(403, 'Sorry !! You are Unauthorized to create any master data !');
}
abort(404);
}
/**
* Display the specified Directorat.
*
* @param \Modules\Cetaklabel\Entities\Directorat $directorat
*/
public function show(Directorat $directorat)
{
if (is_null($this->user) || !$this->user->can('masters.read')) {
abort(403, 'Sorry !! You are Unauthorized to view any master data !');
}
}
/**
* Show the form for editing the specified Directorat.
*
* @param $id
*/
public function edit($id)
{
if (is_null($this->user) || !$this->user->can('masters.update')) {
abort(403, 'Sorry !! You are Unauthorized to update any master data !');
}
$directorat = Directorat::find($id);
echo json_encode($directorat);
}
/**
* Update the specified Directorat in storage.
*
* @param \App\Http\Requests\UpdateDirectoratRequest $request
* @param \Modules\Cetaklabel\Entities\Directorat $directorat
*
* @return mixed
*/
public function update(UpdateDirectoratRequest $request, Directorat $directorat)
{
if (is_null($this->user) || !$this->user->can('masters.update')) {
abort(403, 'Sorry !! You are Unauthorized to update any master data !');
}
// Validate the request...
$validated = $request->validated();
// Update the Directorat...
if ($validated) {
try {
$directorat->update($validated);
echo json_encode(['status' => 'success', 'message' => 'Directorat updated successfully.']);
} catch (Exception $e) {
echo json_encode(['status' => 'error', 'message' => 'Directorat updated failed.']);
}
return;
}
echo json_encode(['status' => 'error', 'message' => 'Directorat updated failed.']);
}
/**
* Remove the specified Directorat from storage.
*
* @param \Modules\Cetaklabel\Entities\Directorat $directorat
*
* @return void
*/
public function destroy(Directorat $directorat)
{
if (is_null($this->user) || !$this->user->can('masters.delete')) {
abort(403, 'Sorry !! You are Unauthorized to delete any master data !');
}
$directorat->delete();
echo json_encode(['status' => 'success', 'message' => 'Directorat deleted successfully.']);
}
}