firstcommit
This commit is contained in:
0
Modules/Destination/app/Http/Controllers/.gitkeep
Normal file
0
Modules/Destination/app/Http/Controllers/.gitkeep
Normal file
@@ -0,0 +1,139 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
}
|
0
Modules/Destination/app/Http/Middleware/.gitkeep
Normal file
0
Modules/Destination/app/Http/Middleware/.gitkeep
Normal file
0
Modules/Destination/app/Http/Requests/.gitkeep
Normal file
0
Modules/Destination/app/Http/Requests/.gitkeep
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Destination\app\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Contracts\Validation\Validator;
|
||||
|
||||
class CreateDestinationRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'title' => 'required|string',
|
||||
'country_id' => 'required|exists:countries,id',
|
||||
'ordering' => 'required|integer|unique:destinations,ordering',
|
||||
'rating' => 'required|integer|between:1,5',
|
||||
'activities' => 'required',
|
||||
'image' => 'sometimes|nullable',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function failedValidation(Validator $validator)
|
||||
{
|
||||
$message = $validator->errors()->first();
|
||||
toastr()->error($message);
|
||||
parent::failedValidation($validator);
|
||||
}
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Destination\app\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Modules\Destination\app\Models\Destination;
|
||||
use Illuminate\Contracts\Validation\Validator;
|
||||
|
||||
class UpdateDestinationRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$destination = Destination::where('uuid', $this->route('uuid'))->first();
|
||||
|
||||
return [
|
||||
'title' => 'required|string',
|
||||
'country_id' => 'required|exists:countries,id',
|
||||
'ordering' => 'required|integer|unique:destinations,ordering,' . ($destination ? $destination->id : 'NULL') . ',id',
|
||||
'activities' => 'required',
|
||||
'rating' => 'required|integer|between:1,5',
|
||||
'image' => 'sometimes|nullable',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
protected function failedValidation(Validator $validator)
|
||||
{
|
||||
$message = $validator->errors()->first();
|
||||
toastr()->error($message);
|
||||
parent::failedValidation($validator);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user