diff --git a/app/Enums/FlightType.php b/app/Enums/FlightType.php new file mode 100644 index 0000000..e374f17 --- /dev/null +++ b/app/Enums/FlightType.php @@ -0,0 +1,10 @@ +with([ + 'departureAirport.city', + 'arrivalAirport.city', + 'airline', + ]) + ->when($request->filled('search'), function (Builder $query) use ($request) { + + $search = $request->input('search'); + + $query->where(function (Builder $query) use ($search) { + + $query->where('title', 'LIKE', "%{$search}%") + ->orWhere('summary', 'LIKE', "%{$search}%") + ->orWhereHas('departureAirport.city', function (Builder $query) use ($search) { + $query->where('name', 'LIKE', "%{$search}%"); + }) + ->orWhereHas('arrivalAirport.city', function (Builder $query) use ($search) { + $query->where('name', 'LIKE', "%{$search}%"); + }) + ->orWhereHas('airline', function (Builder $query) use ($search) { + $query->where('name', 'LIKE', "%{$search}%"); + }); + + }); + }) + ->when($request->filled('flight_type'), function (Builder $query) use ($request) { + + $query->where( + 'flight_type', + $request->input('flight_type') + ); + + }) + ->when($request->filled('status'), function (Builder $query) use ($request) { + + $query->where( + 'status', + $request->input('status') + ); + + }) + ->when($request->filled('sort'), function (Builder $query) use ($request) { + + switch ($request->input('sort')) { + + case 'oldest': + $query->orderBy('created_at'); + break; + + case 'price_asc': + $query->orderBy('price'); + break; + + case 'price_desc': + $query->orderByDesc('price'); + break; + + case 'departure_asc': + $query->orderBy('departure_time'); + break; + + case 'departure_desc': + $query->orderByDesc('departure_time'); + break; + + case 'newest': + default: + $query->orderByDesc('created_at'); + break; + } + }, function (Builder $query) { + + $query->orderByDesc('created_at'); + + }) + ->paginate(15) + ->withQueryString(); + + return view('admin.flights.index', compact('flights')); + } + + public function create() + { + $airlines = Airline::query() + ->orderBy('name') + ->get(); + + $airports = Airport::query() + ->with('city') + ->orderBy('id') + ->get(); + + return view('admin.flights.create', compact( + 'airlines', + 'airports' + )); + } + + + public function edit(Flight $flight) + { + $airlines = Airline::query() + ->orderBy('name') + ->get(); + + $airports = Airport::query() + ->with('city') + ->orderBy('id') + ->get(); + + $flight->load('file'); + + $createdLog = ActivityLog::query() + ->where('model_type', Flight::class) + ->where('model_id', $flight->id) + ->where('action', 'created') + ->with('admin') + ->first(); + + $lastUpdatedLog = ActivityLog::query() + ->where('model_type', Flight::class) + ->where('model_id', $flight->id) + ->where('action', 'updated') + ->with('admin') + ->latest('created_at') + ->first(); + + return view('admin.flights.edit', compact( + 'flight', + 'airlines', + 'airports', + 'createdLog', + 'lastUpdatedLog' + )); + } + + public function store(FlightStoreRequest $request) + { + DB::beginTransaction(); + + try { + + // 1. ذخیره عکس + $image = $request->file('image'); + + $imageName = time() + . '_' + . rand(11111, 99999) + . '.' + . $image->extension(); + + $path = $image->storeAs( + 'flight_images', + $imageName, + 'public' + ); + + // 2. ایجاد رکورد فایل + $file = File::create([ + 'name' => $imageName, + 'size' => $image->getSize(), + 'original_name' => $image->getClientOriginalName(), + 'path' => $path, + 'extension' => $image->extension(), + ]); + + // 3. ایجاد پرواز با file_id + $flight = Flight::create([ + 'file_id' => $file->id, + 'arrival_aiport_id' => $request->arrival_aiport_id, + 'departure_airport_id' => $request->departure_airport_id, + 'airline_id' => $request->airline_id, + 'title' => $request->title, + 'summary' => $request->input('summary') ?? '', + 'flight_type' => $request->flight_type, + 'departure_time' => $request->departure_time, + 'price' => $request->price, + 'capacity' => 0, + 'views' => 0, + 'status' => $request->status, + ]); + + DB::commit(); + + } catch (Exception $exception) { + + DB::rollBack(); + + Log::error($exception); + + return back() + ->withErrors([ + 'general' => $exception->getMessage(), + ]) + ->withInput(); + } + + return redirect() + ->route('admin.flights.index') + ->with('success', 'پرواز با موفقیت ایجاد شد.'); + } + + public function update(FlightUpdateRequest $request, Flight $flight) + { + DB::beginTransaction(); + + try { + + $oldFile = $flight->file; + + // اطلاعات پرواز + $flight->update([ + 'arrival_aiport_id' => $request->arrival_aiport_id, + 'departure_airport_id' => $request->departure_airport_id, + 'airline_id' => $request->airline_id, + 'title' => $request->title, + 'summary' => $request->input('summary') ?? '', + 'flight_type' => $request->flight_type, + 'departure_time' => $request->departure_time, + 'price' => $request->price, + 'capacity' => 0, + 'status' => $request->status, + ]); + + + // اگر عکس جدید انتخاب شده + if ($request->hasFile('image')) { + + $image = $request->file('image'); + + // ذخیره عکس جدید + $path = $image->store('flights', 'public'); + + // ایجاد رکورد فایل جدید + $newFile = File::create([ + 'name' => pathinfo( + $image->hashName(), + PATHINFO_FILENAME + ), + 'extension' => $image->getClientOriginalExtension(), + 'size' => $image->getSize(), + 'original_name' => $image->getClientOriginalName(), + 'path' => $path, + ]); + + // اتصال پرواز به فایل جدید + $flight->update([ + 'file_id' => $newFile->id, + ]); + + + // حذف فقط فایل فیزیکی قبلی + // رکورد files قبلی را حذف نمی‌کنیم + // چون FK روی flights.file_id وجود دارد. + if ($oldFile) { + + if (Storage::disk('public')->exists($oldFile->path)) { + + Storage::disk('public')->delete( + $oldFile->path + ); + } + } + } + + + DB::commit(); + + } catch (\Exception $exception) { + + DB::rollBack(); + + Log::error($exception); + + return back() + ->withErrors([ + 'general' => 'خطایی در ویرایش پرواز رخ داده است.' + ]) + ->withInput(); + } + + + return redirect() + ->route('admin.flights.index') + ->with('success', 'پرواز با موفقیت ویرایش شد.'); + } + public function destroy(Flight $flight) + { + if ($flight->bookings()->exists()) { + + return back()->withErrors([ + 'error' => 'این پرواز دارای رزرو است و قابل حذف نیست.' + ]); + } + + $flight->delete(); + + return redirect() + ->route('admin.flights.index') + ->with('success', 'پرواز با موفقیت حذف شد.'); + } + + public function logs(Flight $flight) + { + $logs = ActivityLog::query() + ->where('model_type', Flight::class) + ->where('model_id', $flight->id) + ->with('admin') + ->orderByDesc('created_at') + ->paginate(20); + + return view('admin.flights.logs', compact('flight', 'logs')); + } +} diff --git a/app/Http/Requests/Admin/FlightStoreRequest.php b/app/Http/Requests/Admin/FlightStoreRequest.php new file mode 100644 index 0000000..fe2fb9f --- /dev/null +++ b/app/Http/Requests/Admin/FlightStoreRequest.php @@ -0,0 +1,99 @@ +|string> + */ + public function rules(): array + { + + return [ + 'arrival_aiport_id' => [ + 'required', + 'integer', + 'exists:airports,id', + ], + + 'departure_airport_id' => [ + 'required', + 'integer', + 'exists:airports,id', + 'different:arrival_aiport_id', + ], + + 'airline_id' => [ + 'required', + 'integer', + 'exists:airlines,id', + ], + + + 'title' => [ + 'required', + 'string', + 'max:255', + ], + + 'summary' => [ + 'nullable', + 'string', + ], + + 'flight_type' => [ + 'required', + 'integer', + Rule::in([ + FlightType::DEPARTURE->value, + FlightType::RETURN->value, + FlightType::ROUNDTRIP->value, + ]), + ], + + 'departure_time' => [ + 'required', + 'date', + ], + + 'price' => [ + 'required', + 'numeric', + 'min:0', + ], + + + 'image' => [ + 'required', + 'image', + 'mimes:jpg,jpeg,png,webp', + 'max:2048', + ], + + 'status' => [ + 'required', + 'integer', + Rule::in([ + FlightStatus::DISABLE->value, + FlightStatus::ENABLE->value, + ]), + ], + ]; + } +} diff --git a/app/Http/Requests/Admin/FlightUpdateRequest.php b/app/Http/Requests/Admin/FlightUpdateRequest.php new file mode 100644 index 0000000..875c12d --- /dev/null +++ b/app/Http/Requests/Admin/FlightUpdateRequest.php @@ -0,0 +1,98 @@ +|string> + */ + public function rules(): array + { + return [ + 'arrival_aiport_id' => [ + 'required', + 'integer', + 'exists:airports,id', + ], + + 'departure_airport_id' => [ + 'required', + 'integer', + 'exists:airports,id', + 'different:arrival_aiport_id', + ], + + 'airline_id' => [ + 'required', + 'integer', + 'exists:airlines,id', + ], + 'image' => [ + 'nullable', + 'image', + 'mimes:jpg,jpeg,png,webp', + 'max:2048', + ], + + + 'title' => [ + 'required', + 'string', + 'max:255', + ], + + 'summary' => [ + 'nullable', + 'string', + ], + + 'flight_type' => [ + 'required', + 'integer', + Rule::in([ + FlightType::DEPARTURE->value, + FlightType::RETURN->value, + FlightType::ROUNDTRIP->value, + ]), + ], + + 'departure_time' => [ + 'required', + 'date', + ], + + 'price' => [ + 'required', + 'numeric', + 'min:0', + ], + + + 'status' => [ + 'required', + 'integer', + Rule::in([ + FlightStatus::DISABLE->value, + FlightStatus::ENABLE->value, + ]), + ], + ]; + } +}