Files
aroginhealthcare/Modules/Service/app/Http/Controllers/ServiceController.php
2025-08-17 16:23:14 +05:45

140 lines
3.5 KiB
PHP

<?php
namespace Modules\Service\app\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Modules\Service\app\Http\Requests\CreateServiceRequest;
use Modules\Service\app\Repositories\ServiceRepository;
class ServiceController extends Controller
{
protected $serviceRepository;
public function __construct()
{
$this->serviceRepository = new ServiceRepository;
}
/**
* Display a listing of the resource.
*/
public function index(Request $request)
{
$perPage = $request->has('per-page') ? $request->input('per-page') : null;
$filter = $request->has('filter') ? $request->input('filter') : [];
$services = $this->serviceRepository->allServices($perPage, $filter);
return view('service::index', compact('services'));
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
return view('service::create');
}
/**
* Store a newly created resource in storage.
*/
public function store(CreateServiceRequest $request): RedirectResponse
{
try {
$validated = $request->validated();
$this->serviceRepository->storeService($validated);
toastr()->success('Service created successfully.');
return redirect()->route('cms.services.index');
} catch (\Throwable $th) {
report($th);
toastr()->error('Something went wrong.');
return back();
}
}
/**
* Show the specified resource.
*/
public function show($id)
{
return view('service::show');
}
/**
* Show the form for editing the specified resource.
*/
public function edit($uuid)
{
$service = $this->serviceRepository->findServiceByUuid($uuid);
if (!$service) {
toastr()->error('Service not found.');
return back();
}
return view('service::edit', compact('service'));
}
/**
* Update the specified resource in storage.
*/
public function update(CreateServiceRequest $request, $uuid): RedirectResponse
{
try {
$validated = $request->validated();
$service = $this->serviceRepository->updateService($validated, $uuid);
if (!$service) {
toastr()->error('Service not found !');
return null;
}
toastr()->success('Service updated successfully.');
return redirect()->route('cms.services.index');
} catch (\Throwable $th) {
DB::rollback();
report($th);
toastr()->error('Something went wrong.');
return back();
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy($uuid)
{
DB::beginTransaction();
try {
$service = $this->serviceRepository->deleteService($uuid);
if (!$service) {
toastr()->error('Service not found.');
return back();
}
DB::commit();
toastr()->success('Service deleted successfully.');
return redirect()->route('cms.services.index');
} catch (\Throwable $th) {
DB::rollback();
report($th);
toastr()->error('Something went wrong.');
return back();
}
}
}