Updated logo API
This commit is contained in:
@@ -2,19 +2,46 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class UserAction extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'user_flight_id',
|
||||
'message',
|
||||
'type',
|
||||
'data'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'user_flight_id' => 'integer',
|
||||
'user_id' => 'integer',
|
||||
'message' => 'string',
|
||||
'data' => 'array',
|
||||
];
|
||||
|
||||
protected $appends = [
|
||||
'display_type',
|
||||
];
|
||||
|
||||
protected function displayType(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn () => match ($this->type) {
|
||||
'flight_booked' => 'Flight Booked',
|
||||
'flight_cancelled' => 'Flight Cancelled',
|
||||
'flight_updated' => 'Flight Updated',
|
||||
'flight_imported' => 'Flight Imported',
|
||||
'flight_logged' => 'Flight Logged',
|
||||
'flight_deleted' => 'Flight Deleted',
|
||||
default => 'Unknown Action'
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'user_id');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
@@ -34,6 +36,17 @@ class UserFlight extends Model
|
||||
'auto_update' => 'boolean',
|
||||
];
|
||||
|
||||
protected $appends = [
|
||||
'departure_date_display',
|
||||
'departure_time_display',
|
||||
'arrival_date_display',
|
||||
'arrival_time_display',
|
||||
'arrival_day_difference',
|
||||
'duration',
|
||||
'duration_display',
|
||||
'distance',
|
||||
];
|
||||
|
||||
public function calculateGreatCircleDistance(): float{
|
||||
$earthRadiusKm = 6371;
|
||||
[$depLat, $depLong] = [$this->departureAirport->latitude_deg, $this->departureAirport->longitude_deg];
|
||||
@@ -52,6 +65,89 @@ class UserFlight extends Model
|
||||
return $earthRadiusKm * $c;
|
||||
}
|
||||
|
||||
protected function departureDateDisplay(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn() => $this->departure_date
|
||||
->toMutable()
|
||||
->setTimezone($this->departureAirport->timezone)
|
||||
->format('j M Y')
|
||||
);
|
||||
}
|
||||
|
||||
protected function departureTimeDisplay(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn() => $this->departure_date
|
||||
->toMutable()
|
||||
->setTimezone($this->departureAirport->timezone)
|
||||
->format('g:iA')
|
||||
);
|
||||
}
|
||||
|
||||
protected function arrivalDateDisplay(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn() => $this->arrival_date
|
||||
?->copy()
|
||||
->setTimezone($this->arrivalAirport->timezone)
|
||||
->format('j M Y')
|
||||
);
|
||||
}
|
||||
|
||||
protected function arrivalTimeDisplay(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn() => $this->arrival_date
|
||||
?->copy()
|
||||
->setTimezone($this->arrivalAirport->timezone)
|
||||
->format('g:iA')
|
||||
);
|
||||
}
|
||||
|
||||
protected function arrivalDayDifference(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: function () {
|
||||
if (!$this->arrival_date) return 0;
|
||||
|
||||
$departureLocal = $this->departure_date->copy()->setTimezone($this->departureAirport->timezone);
|
||||
$arrivalLocal = $this->arrival_date->copy()->setTimezone($this->arrivalAirport->timezone);
|
||||
|
||||
return (int) abs(
|
||||
Carbon::parse($arrivalLocal->toDateString())
|
||||
->diffInDays(Carbon::parse($departureLocal->toDateString()))
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
protected function duration(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn() => $this->departure_date->diffInMinutes($this->arrival_date)
|
||||
);
|
||||
}
|
||||
|
||||
protected function durationDisplay(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: function () {
|
||||
$hours = intdiv($this->duration, 60);
|
||||
$minutes = $this->duration % 60;
|
||||
|
||||
return $hours . 'h ' . str_pad($minutes, 2, '0', STR_PAD_LEFT) . 'm';
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
protected function distance(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn() => $this->calculateGreatCircleDistance()
|
||||
);
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
|
||||
Reference in New Issue
Block a user