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

139 lines
3.3 KiB
PHP

<?php
namespace Modules\Client\app\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Modules\Client\app\Http\Requests\CreateClientRequest;
use Modules\Client\app\Repositories\ClientRepository;
class ClientController extends Controller
{
protected $clientRepository;
public function __construct()
{
$this->clientRepository = new ClientRepository();
}
/**
* 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') : [];
$clients = $this->clientRepository->allClients($perPage, $filter);
return view('client::index', compact('clients'));
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
return view('client::create');
}
/**
* Store a newly created resource in storage.
*/
public function store(CreateClientRequest $request): RedirectResponse
{
try {
$validated = $request->validated();
$this->clientRepository->storeClient($validated);
toastr()->success('Client created successfully.');
return redirect()->route('cms.clients.index');
} catch (\Throwable $th) {
report($th);
toastr()->error('Something went wrong.');
return back();
}
}
/**
* Show the specified resource.
*/
public function show($id)
{
return view('client::show');
}
/**
* Show the form for editing the specified resource.
*/
public function edit($uuid)
{
$client = $this->clientRepository->findClientByUuid($uuid);
if (! $client) {
toastr()->error('Client not found.');
return back();
}
return view('client::edit', compact('client'));
}
/**
* Update the specified resource in storage.
*/
public function update(CreateClientRequest $request, $uuid): RedirectResponse
{
try {
$validated = $request->validated();
$client = $this->clientRepository->updateClient($validated, $uuid);
if (! $client) {
toastr()->error('Client not found !');
return back();
}
toastr()->success('Client updated successfully.');
return redirect()->route('cms.clients.index');
} catch (\Throwable $th) {
report($th);
toastr()->error('Something went wrong.');
return back();
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy($uuid)
{
try {
$client = $this->clientRepository->deleteClient($uuid);
if (! $client) {
toastr()->error('Client not found.');
return back();
}
DB::commit();
toastr()->success('Client deleted successfully.');
return redirect()->route('cms.clients.index');
} catch (\Throwable $th) {
report($th);
toastr()->error('Something went wrong.');
return back();
}
}
}