fix:change flightd sections
This commit is contained in:
@@ -0,0 +1,338 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Admin\FlightStoreRequest;
|
||||
use App\Http\Requests\Admin\FlightUpdateRequest;
|
||||
use App\Models\ActivityLog;
|
||||
use App\Models\Airline;
|
||||
use App\Models\Airport;
|
||||
use App\Models\File;
|
||||
use App\Models\Flight;
|
||||
use Exception;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class FlightController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$flights = Flight::query()
|
||||
->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'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Admin;
|
||||
use App\Enums\FlightStatus;
|
||||
use App\Enums\FlightType;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class FlightStoreRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|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,
|
||||
]),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Admin;
|
||||
|
||||
use App\Enums\FlightStatus;
|
||||
use App\Enums\FlightType;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class FlightUpdateRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|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,
|
||||
]),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user