Cetaklabel/Http/Controllers/CardboardDetailController.php

160 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\CardboardDataTable;
use Modules\Cetaklabel\DataTables\CardboardDetailDataTable;
use Modules\Cetaklabel\DataTables\CardboardOdnerDataTable;
use Modules\Cetaklabel\Entities\CardboardDetail;
class CardboardDetailController 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 Cardboards.
*
* @param \App\DataTables\CardboardDetailDataTable $dataTable
*
* @return mixed
*/
public function index(CardboardOdnerDataTable $dataTable)
{
if (is_null($this->user) || !$this->user->can('document.read')) {
abort(403, 'Sorry !! You are Unauthorized to view any document data !');
}
return $dataTable->render('cetaklabel::app.cardboard-detail.odner');
}
/**
* Store a newly created Cardboard in storage.
*
* @return mixed
*/
public function store(Request $request)
{
if (is_null($this->user) || !$this->user->can('document.create')) {
abort(403, 'Sorry !! You are Unauthorized to create any document data !');
}
// Validate the request...
$validated = $request->validated();
// Store the Cardboard...
if ($validated) {
try {
CardboardDetail::create($validated);
echo json_encode(['status' => 'success', 'message' => 'Cardboard created successfully.']);
} catch (Exception $e) {
echo json_encode(['status' => 'error', 'message' => 'Cardboard created failed.']);
}
return;
}
echo json_encode(['status' => 'error', 'message' => 'Cardboard created failed.']);
}
/**
* Show the form for creating a new Cardboard.
*/
public function create()
{
if (is_null($this->user) || !$this->user->can('document.create')) {
abort(403, 'Sorry !! You are Unauthorized to create any document data !');
}
abort(404);
}
/**
* Display the specified Cardboard.
*
* @param \Modules\Cetaklabel\Entities\CardboardDetail $cardboard_detail
*/
public function show(CardboardDetailDataTable $dataTable)
{
if (is_null($this->user) || !$this->user->can('document.read')) {
abort(403, 'Sorry !! You are Unauthorized to view any document data !');
}
return $dataTable->render('cetaklabel::app.cardboard-detail.index');
}
/**
* Show the form for editing the specified Cardboard.
*
* @param $id
*/
public function edit($id)
{
if (is_null($this->user) || !$this->user->can('document.update')) {
abort(403, 'Sorry !! You are Unauthorized to update any document data !');
}
$cardboard = CardboardDetail::find($id);
echo json_encode($cardboard);
}
/**
* Update the specified Cardboard in storage.
*
* @param \Modules\Cetaklabel\Entities\CardboardDetail $cardboard_detail
*
* @return mixed
*/
public function update(Request $request, CardboardDetail $cardboard_detail)
{
if (is_null($this->user) || !$this->user->can('document.update')) {
abort(403, 'Sorry !! You are Unauthorized to update any document data !');
}
// Validate the request...
$validated = $request->validated();
// Update the Cardboard...
if ($validated) {
try {
$cardboard_detail->update($validated);
echo json_encode(['status' => 'success', 'message' => 'Cardboard updated successfully.']);
} catch (Exception $e) {
echo json_encode(['status' => 'error', 'message' => 'Cardboard updated failed.']);
}
return;
}
echo json_encode(['status' => 'error', 'message' => 'Cardboard updated failed.']);
}
/**
* Remove the specified Cardboard from storage.
*
* @param \Modules\Cetaklabel\Entities\CardboardDetail $cardboard_detail
*
* @return void
*/
public function destroy(CardboardDetail $cardboard_detail)
{
if (is_null($this->user) || !$this->user->can('document.delete')) {
abort(403, 'Sorry !! You are Unauthorized to delete any document data !');
}
$cardboard_detail->delete();
echo json_encode(['status' => 'success', 'message' => 'Cardboard deleted successfully.']);
}
}