Updated logo API
This commit is contained in:
@@ -2,12 +2,14 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Aircraft;
|
||||
use App\Models\Airline;
|
||||
use App\Models\Airport;
|
||||
use App\Models\CrewType;
|
||||
use App\Models\FlightClass;
|
||||
use App\Models\FlightReason;
|
||||
use App\Models\SeatType;
|
||||
use App\Models\UserAction;
|
||||
use App\Models\UserFlight;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -72,6 +74,70 @@ class FlightController extends Controller
|
||||
];
|
||||
}
|
||||
|
||||
private function recordChanges(UserFlight $flight): void
|
||||
{
|
||||
$dirty = $flight->getDirty();
|
||||
|
||||
if (empty($dirty)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$actions = [];
|
||||
foreach ($dirty as $field => $newValue) {
|
||||
$original = $flight->getOriginal($field);
|
||||
$actions[] = [
|
||||
'user_id' => $flight->user_id,
|
||||
'user_flight_id' => $flight->id,
|
||||
'message' => $this->formatChange($field, $original, $newValue),
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
];
|
||||
}
|
||||
|
||||
UserAction::insert($actions);
|
||||
}
|
||||
|
||||
private array $labelCache = [];
|
||||
|
||||
private function resolveLabel(string $field, mixed $value): string
|
||||
{
|
||||
if (is_null($value)) {
|
||||
return 'none';
|
||||
}
|
||||
|
||||
$cacheKey = "{$field}:{$value}";
|
||||
|
||||
if (isset($this->labelCache[$cacheKey])) {
|
||||
return $this->labelCache[$cacheKey];
|
||||
}
|
||||
|
||||
$label = match($field) {
|
||||
|
||||
'airline_id' => Airline::find($value)?->display_name ?? $value,
|
||||
'departure_airport_id',
|
||||
'arrival_airport_id' => Airport::find($value)?->display_name ?? $value,
|
||||
'aircraft_id' => Aircraft::find($value)?->display_name_short ?? $value,
|
||||
'seat_type_id' => SeatType::find($value)?->name ?? $value,
|
||||
'flight_class_id' => FlightClass::find($value)?->name ?? $value,
|
||||
'flight_reason_id' => FlightReason::find($value)?->name ?? $value,
|
||||
'crew_type_id' => CrewType::find($value)?->name ?? $value,
|
||||
'departure_date',
|
||||
'arrival_date' => Carbon::parse($value)->format('j F Y \a\t H:iA'),
|
||||
default => (string) $value,
|
||||
};
|
||||
|
||||
return $this->labelCache[$cacheKey] = $label;
|
||||
}
|
||||
|
||||
private function formatChange(string $field, mixed $from, mixed $to): string
|
||||
{
|
||||
$label = str($field)->replace('_id', '')->replace('_', ' ')->title();
|
||||
$fromLabel = $this->resolveLabel($field, $from);
|
||||
$toLabel = $this->resolveLabel($field, $to);
|
||||
|
||||
return "{$label} changed from {$fromLabel} to {$toLabel}";
|
||||
}
|
||||
|
||||
private function flightPayload(array $validated): array
|
||||
{
|
||||
[$departureUtc, $arrivalUtc] = $this->convertedDates($validated);
|
||||
@@ -111,7 +177,9 @@ class FlightController extends Controller
|
||||
|
||||
$validated = $request->validate($this->rules());
|
||||
|
||||
$flight->update($this->flightPayload($validated));
|
||||
$flight->fill($this->flightPayload($validated));
|
||||
$this->recordChanges($flight);
|
||||
$flight->save();
|
||||
|
||||
return redirect()->route('profile.departure-board', [Auth::user()->name, $flight->id]);
|
||||
}
|
||||
@@ -147,7 +215,7 @@ class FlightController extends Controller
|
||||
'crew_type' => $flight->crewType?->toArray() ?? [],
|
||||
'flight_reason' => $flight->flightReason->toArray(),
|
||||
'airline_options' => $flight->airline
|
||||
? [['value' => $flight->airline->id, 'title' => $flight->airline->display_name]]
|
||||
? [['value' => $flight->airline->id, 'title' => $flight->airline->display_name, 'logo_url' => $flight->airline->logo_url]]
|
||||
: [],
|
||||
'from_options' => [['value' => $flight->departureAirport->id, 'title' => $flight->departureAirport->display_name, 'country_code' => strtolower($flight->departureAirport->region->country->code)]],
|
||||
'to_options' => [['value' => $flight->arrivalAirport->id, 'title' => $flight->arrivalAirport->display_name, 'country_code' => strtolower($flight->arrivalAirport->region->country->code)]],
|
||||
|
||||
Reference in New Issue
Block a user