feat(admin): implement full flight management for panel admin
This commit is contained in:
@@ -4,13 +4,20 @@ namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Admin\Flight\FlightStoreRequest;
|
||||
use App\Http\Requests\Admin\Flight\FlightUpdateRequest;
|
||||
use App\Models\Airline;
|
||||
use App\Models\Airport;
|
||||
use App\Models\City;
|
||||
use App\Models\Country;
|
||||
use App\Models\File;
|
||||
use App\Models\Flight;
|
||||
use App\Models\FlightImage;
|
||||
use Hekmatinasser\Verta\Verta;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class FlightController extends Controller
|
||||
{
|
||||
@@ -62,6 +69,7 @@ class FlightController extends Controller
|
||||
));
|
||||
|
||||
}
|
||||
|
||||
public function citiesByCountry(Country $country): JsonResponse
|
||||
{
|
||||
$cities = City::query()
|
||||
@@ -74,6 +82,7 @@ class FlightController extends Controller
|
||||
|
||||
return response()->json($cities);
|
||||
}
|
||||
|
||||
public function airportsByCity(City $city): JsonResponse
|
||||
{
|
||||
$airports = Airport::query()
|
||||
@@ -89,7 +98,78 @@ class FlightController extends Controller
|
||||
|
||||
public function store(FlightStoreRequest $request)
|
||||
{
|
||||
$data = $request->validated();
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
try {
|
||||
$departureTime = Verta::parse(
|
||||
$data['departure_time']
|
||||
)->datetime();
|
||||
|
||||
$arrivalTime = Verta::parse(
|
||||
$data['arrival_time']
|
||||
)->datetime();
|
||||
|
||||
$flight = Flight::create([
|
||||
'airline_id' => $data['airline_id'],
|
||||
'origin_airport_id' => $data['origin_airport_id'],
|
||||
'destination_airport_id' => $data['destination_airport_id'],
|
||||
|
||||
'flight_number' => $data['flight_number'],
|
||||
'trip_type' => $data['trip_type'],
|
||||
'flight_type' => $data['flight_type'],
|
||||
'flight_scope' => $data['flight_scope'],
|
||||
|
||||
'departure_time' => $departureTime,
|
||||
'arrival_time' => $arrivalTime,
|
||||
|
||||
'price' => $data['price'],
|
||||
'capacity' => $data['capacity'],
|
||||
'status' => $data['status'],
|
||||
]);
|
||||
|
||||
$isDefault = true;
|
||||
|
||||
if (!empty($data['images'])) {
|
||||
foreach ($data['images'] as $image) {
|
||||
$imageName = $flight->id . '_' . time() . rand(11111, 99999) . '.' . $image->extension();
|
||||
|
||||
$path = $image->storeAs('flight_images', $imageName, 'public');
|
||||
|
||||
$file = File::create([
|
||||
'name' => $imageName,
|
||||
'size' => $image->getSize(),
|
||||
'original_name' => $image->getClientOriginalName(),
|
||||
'path' => $path,
|
||||
'extension' => $image->extension(),
|
||||
]);
|
||||
|
||||
FlightImage::create([
|
||||
'flight_id' => $flight->id,
|
||||
'file_id' => $file->id,
|
||||
'is_default' => $isDefault,
|
||||
]);
|
||||
|
||||
$isDefault = false;
|
||||
}
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
} catch (\Exception $exception) {
|
||||
DB::rollBack();
|
||||
|
||||
Log::error($exception);
|
||||
|
||||
return back()
|
||||
->withInput()
|
||||
->with('error', 'ثبت پرواز با خطا مواجه شد.');
|
||||
}
|
||||
|
||||
return redirect()
|
||||
->route('admin.flights.index')
|
||||
->with('success', 'پرواز با موفقیت ثبت شد.');
|
||||
}
|
||||
|
||||
public function edit(Flight $flight)
|
||||
@@ -117,21 +197,195 @@ class FlightController extends Controller
|
||||
));
|
||||
}
|
||||
|
||||
public function update()
|
||||
public function destroyImage(Flight $flight, FlightImage $image)
|
||||
{
|
||||
$image = $flight->images()
|
||||
->with('file')
|
||||
->findOrFail($image->id);
|
||||
|
||||
$file = $image->file;
|
||||
|
||||
DB::transaction(function () use ($flight, $image) {
|
||||
$image->delete();
|
||||
|
||||
$hasDefaultImage = $flight->images()
|
||||
->where('is_default', true)
|
||||
->exists();
|
||||
|
||||
if (! $hasDefaultImage) {
|
||||
$newDefaultImage = $flight->images()
|
||||
->orderBy('sort_order')
|
||||
->orderBy('id')
|
||||
->first();
|
||||
|
||||
$newDefaultImage?->update([
|
||||
'is_default' => true,
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
deleteFile($file);
|
||||
|
||||
return redirect()
|
||||
->route('admin.flights.edit', $flight)
|
||||
->with('success', 'تصویر پرواز با موفقیت حذف شد.');
|
||||
}
|
||||
|
||||
public function destroy()
|
||||
public function update(FlightUpdateRequest $request, Flight $flight)
|
||||
{
|
||||
$data = $request->validated();
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
try {
|
||||
$departureTime = Verta::parse(
|
||||
$data['departure_time']
|
||||
)->datetime();
|
||||
|
||||
$arrivalTime = Verta::parse(
|
||||
$data['arrival_time']
|
||||
)->datetime();
|
||||
|
||||
$flight->update([
|
||||
'airline_id' => $data['airline_id'],
|
||||
'origin_airport_id' => $data['origin_airport_id'],
|
||||
'destination_airport_id' => $data['destination_airport_id'],
|
||||
|
||||
'flight_number' => $data['flight_number'],
|
||||
'trip_type' => $data['trip_type'],
|
||||
'flight_type' => $data['flight_type'],
|
||||
'flight_scope' => $data['flight_scope'],
|
||||
|
||||
'departure_time' => $departureTime,
|
||||
'arrival_time' => $arrivalTime,
|
||||
|
||||
'price' => $data['price'],
|
||||
'capacity' => $data['capacity'],
|
||||
'status' => $data['status'],
|
||||
]);
|
||||
if (!empty($data['images'])) {
|
||||
$hasDefault = $flight->images()
|
||||
->where('is_default', true)
|
||||
->exists();
|
||||
|
||||
$nextSort = (int)$flight->images()
|
||||
->max('sort_order');
|
||||
|
||||
foreach ($data['images'] as $index => $image) {
|
||||
$imageName = $flight->id . '_' . time() . rand(11111, 99999) . '.' . $image->extension();
|
||||
|
||||
$path = $image->storeAs(
|
||||
'flight_images',
|
||||
$imageName,
|
||||
'public'
|
||||
);
|
||||
|
||||
$file = File::create([
|
||||
'name' => $imageName,
|
||||
'size' => $image->getSize(),
|
||||
'original_name' => $image->getClientOriginalName(),
|
||||
'path' => $path,
|
||||
'extension' => $image->extension(),
|
||||
]);
|
||||
|
||||
FlightImage::create([
|
||||
'flight_id' => $flight->id,
|
||||
'file_id' => $file->id,
|
||||
'sort_order' => $nextSort + $index + 1,
|
||||
'is_default' => !$hasDefault && $index === 0,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
} catch (\Exception $exception) {
|
||||
|
||||
DB::rollBack();
|
||||
|
||||
Log::error($exception);
|
||||
|
||||
return back()
|
||||
->withInput()
|
||||
->with('error', 'ویرایش پرواز با خطا مواجه شد.');
|
||||
}
|
||||
|
||||
return redirect()
|
||||
->route('admin.flights.index')
|
||||
->with('success', 'پرواز با موفقیت ویرایش شد.');
|
||||
}
|
||||
public function restore()
|
||||
{
|
||||
|
||||
|
||||
public function destroy(Flight $flight)
|
||||
{
|
||||
$flight->delete();
|
||||
|
||||
return redirect()
|
||||
->route('admin.flights.index')
|
||||
->with('success', 'پرواز با موفقیت به سطل زباله منتقل شد.');
|
||||
}
|
||||
public function forceDelete()
|
||||
{
|
||||
|
||||
public function trash()
|
||||
{
|
||||
$title = 'پروازهای حذف شده';
|
||||
|
||||
$flights = Flight::onlyTrashed()
|
||||
->with([
|
||||
'airline',
|
||||
'origin_airport.city',
|
||||
'airport.city',
|
||||
'coverImage.file',
|
||||
])
|
||||
->orderByDesc('deleted_at')
|
||||
->paginate(4);
|
||||
|
||||
return view('admin.flights.trash', compact('title', 'flights'));
|
||||
}
|
||||
|
||||
public function restore($id)
|
||||
{
|
||||
$flight = Flight::onlyTrashed()
|
||||
->findOrFail($id);
|
||||
|
||||
$flight->restore();
|
||||
|
||||
return redirect()
|
||||
->route('admin.flights.trash.index')
|
||||
->with('success', 'پرواز با موفقیت بازگردانی شد.');
|
||||
}
|
||||
|
||||
public function forceDelete($id)
|
||||
{
|
||||
$flight = Flight::onlyTrashed()
|
||||
->with('images.file')
|
||||
->findOrFail($id);
|
||||
|
||||
try {
|
||||
DB::transaction(function () use ($flight) {
|
||||
foreach ($flight->images as $image) {
|
||||
$file = $image->file;
|
||||
|
||||
if ($file) {
|
||||
Storage::disk('public')->delete($file->path);
|
||||
|
||||
$file->delete();
|
||||
}
|
||||
|
||||
$image->delete();
|
||||
}
|
||||
|
||||
$flight->forceDelete();
|
||||
});
|
||||
|
||||
return redirect()
|
||||
->route('admin.flights.trash.index')
|
||||
->with('success', 'پرواز و تمام اطلاعات مرتبط با موفقیت حذف دائمی شدند.');
|
||||
|
||||
} catch (\Throwable $exception) {
|
||||
report($exception);
|
||||
|
||||
return back()
|
||||
->with('error', 'حذف دائمی پرواز انجام نشد.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user