120 lines
3.6 KiB
PHP
120 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\Admin\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Modules\Admin\Models\Municipality;
|
|
use Modules\Admin\Repositories\DistrictInterface;
|
|
use Modules\Admin\Repositories\MunicipalityInterface;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class MunicipalityController extends Controller
|
|
{
|
|
private $municipality;
|
|
private $district;
|
|
|
|
public function __construct(MunicipalityInterface $municipality, DistrictInterface $district)
|
|
{
|
|
$this->municipality = $municipality;
|
|
$this->district = $district;
|
|
}
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$data['title'] = 'Municipality List';
|
|
|
|
if ($request->ajax()) {
|
|
$model = Municipality::query();
|
|
|
|
return DataTables::eloquent($model)
|
|
->setRowId('{{$id}}')
|
|
->setRowClass('{{"text-center align-middle"}}')
|
|
->addColumn('district', function (Municipality $municipality) {
|
|
return $municipality->district?->name;
|
|
})
|
|
->addColumn('action', 'admin::municipalities.datatables.action-btn')
|
|
->rawColumns(['district', 'action'])
|
|
->toJson();
|
|
}
|
|
return view('admin::municipalities.index', $data);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
$data['title'] = 'Create Municipality';
|
|
$data['editable'] = false;
|
|
$data['districtLists'] = $this->district->pluck();
|
|
return view('admin::municipalities.create', $data);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request): RedirectResponse
|
|
{
|
|
try {
|
|
|
|
$this->municipality->create($request->all());
|
|
return redirect()->route('municipality.index')->with('sucess', 'Municipality has been created!');
|
|
|
|
} catch (\Throwable $th) {
|
|
return redirect()->back()->withError($th->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show the specified resource.
|
|
*/
|
|
public function show($id)
|
|
{
|
|
return view('admin::municipalities.show');
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
$data['title'] = 'Edit Municipality';
|
|
$data['editable'] = true;
|
|
$data['municipality'] = $this->municipality->getMunicipalityById($id);
|
|
$data['districtLists'] = $this->district->pluck();
|
|
return view('admin::municipalities.edit', $data);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, $id): RedirectResponse
|
|
{
|
|
try {
|
|
$this->municipality->update($id, $request->except(['_method', '_token']));
|
|
return redirect()->route('municipality.index')->with('success', 'Municipality has been updated!');
|
|
|
|
} catch (\Throwable $th) {
|
|
return redirect()->back()->withError($th->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
try {
|
|
$this->municipality->delete($id);
|
|
return response()->json(['status' => 200, 'message' => 'Municipality has been deleted!'], 200);
|
|
} catch (\Throwable $th) {
|
|
return response()->json(['status' => 500, 'message' => 'Municipality to delete!', 'error' => $th->getMessage()], 500);
|
|
}
|
|
}
|
|
}
|