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

140 lines
4.0 KiB
PHP

<?php
namespace Modules\Destination\app\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Modules\Destination\app\Repositories\DestinationRepository;
use Modules\Destination\app\Http\Requests\CreateDestinationRequest;
use Modules\Destination\app\Http\Requests\UpdateDestinationRequest;
class DestinationController extends Controller
{
protected $destinationRepository;
public function __construct()
{
$this->destinationRepository = new DestinationRepository;
}
/**
* 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') : [];
$destinations = $this->destinationRepository->allDestinations($perPage, $filter);
return view('destination::index', compact('destinations'));
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$data['countries'] = $this->destinationRepository->getCountries() ?? [];
$data['activities'] = $this->destinationRepository->getActivities() ?? [];
return view('destination::create', $data);
}
/**
* Store a newly created resource in storage.
*/
public function store(CreateDestinationRequest $request)
{
try {
$validated = $request->validated();
$result = $this->destinationRepository->storeDestination($validated);
if ($result == null) {
toastr()->error('Destination not found.');
return back();
}
toastr()->success('Destination created successfully.');
return redirect()->route('cms.destinations.index');
} catch (\Throwable $th) {
report($th);
toastr()->error('Something went wrong.');
return back();
}
}
/**
* Show the specified resource.
*/
public function show($id)
{
return view('destination::show');
}
/**
* Show the form for editing the specified resource.
*/
public function edit($uuid)
{
$data['countries'] = $this->destinationRepository->getCountries() ?? [];
$data['activities'] = $this->destinationRepository->getActivities() ?? [];
$data['destination'] = $this->destinationRepository->destinationWithCountriesAndActivitiesByUuid($uuid);
if (!$data['destination']) {
toastr()->error('Destination not found.');
return back();
}
return view('destination::edit', $data);
}
/**
* Update the specified resource in storage.
*/
public function update(UpdateDestinationRequest $request, $uuid)
{
try {
$validated = $request->validated();
$destination = $this->destinationRepository->updateDestination($validated, $uuid);
if (!$destination) {
toastr()->error('Destination not found!');
return back();
}
toastr()->success('Destination updated successfully.');
return redirect()->route('cms.destinations.index');
} catch (\Throwable $th) {
report($th);
toastr()->error('Something went wrong.');
return back();
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy($uuid)
{
try {
$destination = $this->destinationRepository->deleteDestination($uuid);
if (!$destination) {
toastr()->error('Destination not found.');
return back();
}
toastr()->success('Destination deleted successfully.');
return redirect()->route('cms.destinations.index');
} catch (\Throwable $th) {
report($th);
toastr()->error('Something went wrong.');
return back();
}
}
}