File: /var/www/vhost/disk-apps/dev-beg.teky.com.co/app/Http/Controllers/StoragesController.php
<?php
namespace App\Http\Controllers;
use App\Zone;
use App\Storage as StorageModule;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class StoragesController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index($zone_id)
{
abort_unless(\Gate::allows('storage_access'), 403);
$zone = Zone::where('id', $zone_id)->first();
$storages = StorageModule::where('zone_id', $zone_id)->with('zone')->orderBy('name', 'asc')->get();
return view('storages.list', compact('storages', 'zone_id', 'zone'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create($zone_id)
{
abort_unless(\Gate::allows('storage_create'), 403);
$zone = Zone::where('id', $zone_id)->first();
return view('storages.create', compact('zone_id', 'zone'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
abort_unless(\Gate::allows('storage_create'), 403);
try {
if (!StorageModule::where('name', $request->input('name'))->first()) {
StorageModule::create(array_slice($request->all(), 1));
return response(array("status" => true, "type" => "success", "title" => "", "message" => __('created_successfully'), "data" => null));
} else {
return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('already_exists', ['name' => $request->input('name')]), "data" => null));
}
} catch (\Throwable $th) {
return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('error_creating'), "data" => null));
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
abort_unless(\Gate::allows('storage_show'), 403);
return redirect()->back();
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($zone_id,$storage_id)
{
abort_unless(\Gate::allows('storage_edit'), 403);
$storage = StorageModule::findOrFail($storage_id);
$zone = Zone::where('id', $zone_id)->first();
return view('storages.edit', compact('zone_id', 'storage', 'zone'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request,$zone_id,$storage_id)
{
abort_unless(\Gate::allows('storage_edit'), 403);
try {
if (!StorageModule::where([['id', '!=', $storage_id], ['name', $request->input('name')]])->first()) {
StorageModule::where('id', $storage_id)->update(array_slice($request->all(), 2));
return response(array("status" => true, "type" => "success", "title" => "", "message" => __('updated_successfully'), "data" => null));
} else {
return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('already_exists', ['name' => $request->input('name')]), "data" => null));
}
} catch (\Throwable $th) {
return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('error_updating'), "data" => null));
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($zone_id,$storage_id)
{
abort_unless(\Gate::allows('storage_destroy'), 403);
try {
StorageModule::where('id', $storage_id)->delete();
return response(array("status" => true, "type" => "success", "title" => "", "message" => __('deleted_successfully'), "data" => null));
} catch (\Illuminate\Database\QueryException $e) {
return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('delete_relation_data'), "data" => null));
}
}
}