add separate flight_fields table and move over to flight_field_values

This commit is contained in:
Nabeel Shahzad
2018-03-20 13:06:06 -05:00
parent a9454c319a
commit 485c6e86bb
17 changed files with 147 additions and 45 deletions

View File

@@ -14,7 +14,7 @@ class CreateFlightTables extends Migration
public function up()
{
Schema::create('flights', function (Blueprint $table) {
$table->string('id', \App\Models\Flight::ID_MAX_LENGTH);
$table->string('id', \App\Interfaces\Model::ID_MAX_LENGTH);
$table->unsignedInteger('airline_id');
$table->string('flight_number', 10);
$table->string('route_code', 5)->nullable();
@@ -42,7 +42,7 @@ class CreateFlightTables extends Migration
});
Schema::create('flight_fare', function (Blueprint $table) {
$table->string('flight_id', \App\Models\Flight::ID_MAX_LENGTH);
$table->string('flight_id', \App\Interfaces\Model::ID_MAX_LENGTH);
$table->unsignedInteger('fare_id');
$table->string('price', 10)->nullable();
$table->string('cost', 10)->nullable();
@@ -52,9 +52,21 @@ class CreateFlightTables extends Migration
$table->primary(['flight_id', 'fare_id']);
});
/**
* Hold a master list of fields
*/
Schema::create('flight_fields', function (Blueprint $table) {
$table->increments('id');
$table->string('flight_id', \App\Models\Flight::ID_MAX_LENGTH);
$table->string('name', 50);
$table->string('slug', 50)->nullable();
});
/**
* The values for the actual fields
*/
Schema::create('flight_field_values', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('flight_id', \App\Interfaces\Model::ID_MAX_LENGTH);
$table->string('name', 50);
$table->text('value');
$table->timestamps();

View File

@@ -16,7 +16,7 @@ class CreatePirepTables extends Migration
public function up()
{
Schema::create('pireps', function (Blueprint $table) {
$table->string('id', \App\Models\Pirep::ID_MAX_LENGTH);
$table->string('id', \App\Interfaces\Model::ID_MAX_LENGTH);
$table->unsignedInteger('user_id');
$table->unsignedInteger('airline_id');
$table->unsignedInteger('aircraft_id')->nullable();
@@ -52,7 +52,7 @@ class CreatePirepTables extends Migration
Schema::create('pirep_comments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('pirep_id', \App\Models\Pirep::ID_MAX_LENGTH);
$table->string('pirep_id', \App\Interfaces\Model::ID_MAX_LENGTH);
$table->unsignedInteger('user_id');
$table->text('comment');
$table->timestamps();
@@ -60,7 +60,7 @@ class CreatePirepTables extends Migration
Schema::create('pirep_fares', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('pirep_id', \App\Models\Pirep::ID_MAX_LENGTH);
$table->string('pirep_id', \App\Interfaces\Model::ID_MAX_LENGTH);
$table->unsignedInteger('fare_id');
$table->unsignedInteger('count')->nullable()->default(0);
@@ -68,7 +68,7 @@ class CreatePirepTables extends Migration
});
Schema::create('pirep_fields', function (Blueprint $table) {
$table->bigIncrements('id');
$table->increments('id');
$table->string('name', 50);
$table->string('slug', 50)->nullable();
$table->boolean('required')->nullable()->default(false);
@@ -76,7 +76,7 @@ class CreatePirepTables extends Migration
Schema::create('pirep_field_values', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('pirep_id', \App\Models\Pirep::ID_MAX_LENGTH);
$table->string('pirep_id', \App\Interfaces\Model::ID_MAX_LENGTH);
$table->string('name', 50);
$table->string('slug', 50)->nullable();
$table->string('value')->nullable();

View File

@@ -16,7 +16,7 @@ class CreateBidsTable extends Migration
Schema::create('bids', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->string('flight_id', \App\Models\Flight::ID_MAX_LENGTH);
$table->string('flight_id', \App\Interfaces\Model::ID_MAX_LENGTH);
$table->timestamps();
$table->index('user_id');

View File

@@ -15,7 +15,7 @@ class CreateAcarsTables extends Migration
{
Schema::create('acars', function (Blueprint $table) {
$table->string('id', 12);
$table->string('pirep_id', \App\Models\Pirep::ID_MAX_LENGTH);
$table->string('pirep_id', \App\Interfaces\Model::ID_MAX_LENGTH);
$table->unsignedTinyInteger('type');
$table->unsignedInteger('nav_type')->nullable();
$table->unsignedInteger('order')->default(0);

View File

@@ -346,6 +346,12 @@ flights:
updated_at: NOW
flight_fields:
- name: Departure Gate
slug: departure_gate
- name: Arrival Gate
slug: arrival_gate
flight_field_values:
- id: 1
flight_id: flightid_1
name: cost index

View File

@@ -7,7 +7,8 @@ use App\Http\Requests\UpdateFlightRequest;
use App\Interfaces\Controller;
use App\Models\Enums\FlightType;
use App\Models\Flight;
use App\Models\FlightFields;
use App\Models\FlightField;
use App\Models\FlightFieldValue;
use App\Repositories\AirlineRepository;
use App\Repositories\AirportRepository;
use App\Repositories\FareRepository;
@@ -18,6 +19,7 @@ use App\Services\FlightService;
use App\Support\Units\Time;
use Flash;
use Illuminate\Http\Request;
use Log;
use Response;
/**
@@ -62,6 +64,30 @@ class FlightController extends Controller
$this->subfleetRepo = $subfleetRepo;
}
/**
* Save any custom fields found
* @param Flight $flight
* @param Request $request
*/
protected function saveCustomFields(Flight $flight, Request $request): void
{
$custom_fields = [];
$flight_fields = FlightField::all();
foreach ($flight_fields as $field) {
if (!$request->filled($field->slug)) {
continue;
}
$custom_fields[] = [
'name' => $field->name,
'value' => $request->input($field->slug)
];
}
Log::info('PIREP Custom Fields', $custom_fields);
$this->flightSvc->updateCustomFields($flight->id, $custom_fields);
}
/**
* @param $flight
* @return array
@@ -295,7 +321,7 @@ class FlightController extends Controller
* @param Request $request
* @return mixed
*/
public function fields(Request $request)
public function field_values(Request $request)
{
$id = $request->id;
@@ -308,19 +334,19 @@ class FlightController extends Controller
// add custom field to flight
if ($request->isMethod('post')) {
$field = new FlightFields;
$field = new FlightFieldValue;
$field->flight_id = $id;
$field->name = $request->name;
$field->value = $request->value;
$field->save();
} elseif ($request->isMethod('put')) {
$field = FlightFields::where('id', $request->field_id)->first();
$field = FlightFieldValue::where('id', $request->field_id)->first();
$field->value = $request->value;
$field->save();
// update the field value
} // remove custom field from flight
elseif ($request->isMethod('delete')) {
FlightFields::destroy($request->field_id);
FlightFieldValue::destroy($request->field_id);
}
return $this->return_fields_view($flight);

View File

@@ -114,7 +114,7 @@ class PirepController extends Controller
* @param Pirep $pirep
* @param Request $request
*/
protected function saveCustomFields(Pirep $pirep, Request $request)
protected function saveCustomFields(Pirep $pirep, Request $request): void
{
$custom_fields = [];
$pirep_fields = $this->pirepFieldRepo->all();
@@ -265,7 +265,6 @@ class PirepController extends Controller
if (empty($pirep)) {
Flash::error('Pirep not found');
return redirect(route('admin.pireps.index'));
}
@@ -358,7 +357,6 @@ class PirepController extends Controller
$this->saveFares($pirep, $request);
Flash::success('Pirep updated successfully.');
return redirect(route('admin.pireps.index'));
}
@@ -373,14 +371,12 @@ class PirepController extends Controller
if (empty($pirep)) {
Flash::error('Pirep not found');
return redirect(route('admin.pireps.index'));
}
$this->pirepRepo->delete($id);
Flash::success('Pirep deleted successfully.');
return redirect(route('admin.pireps.index'));
}

View File

@@ -8,5 +8,5 @@ namespace App\Interfaces;
*/
abstract class Model extends \Illuminate\Database\Eloquent\Model
{
public const ID_MAX_LENGTH = 12;
}

View File

@@ -10,17 +10,17 @@ use PhpUnitsOfMeasure\Exception\NonNumericValue;
use PhpUnitsOfMeasure\Exception\NonStringUnitName;
/**
* @property Airline airline
* @property mixed flight_number
* @property mixed route_code
* @property mixed route_leg
* @property Collection fields
* @property string id
* @property Airline airline
* @property mixed flight_number
* @property mixed route_code
* @property mixed route_leg
* @property Collection field_values
*/
class Flight extends Model
{
use HashIdTrait;
public const ID_MAX_LENGTH = 12;
public $table = 'flights';
public $incrementing = false;
@@ -130,7 +130,7 @@ class Flight extends Model
*/
public function field($field_name): string
{
$field = $this->fields->where('name', $field_name)->first();
$field = $this->field_values->where('name', $field_name)->first();
if($field) {
return $field['value'];
}
@@ -168,9 +168,9 @@ class Flight extends Model
->withPivot('price', 'cost', 'capacity');
}
public function fields()
public function field_values()
{
return $this->hasMany(FlightFields::class, 'flight_id');
return $this->hasMany(FlightFieldValue::class, 'flight_id');
}
public function subfleets()

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Models;
use App\Interfaces\Model;
/**
* Class FlightField
* @property string name
* @property string slug
* @package App\Models
*/
class FlightField extends Model
{
public $table = 'flight_fields';
public $timestamps = false;
public $fillable = [
'name',
'slug',
'required',
];
protected $casts = [
'required' => 'boolean',
];
public static $rules = [
'name' => 'required',
];
/**
* When setting the name attribute, also set the slug
* @param $name
*/
public function setNameAttribute($name): void
{
$this->attributes['name'] = $name;
$this->attributes['slug'] = str_slug($name);
}
}

View File

@@ -5,12 +5,12 @@ namespace App\Models;
use App\Interfaces\Model;
/**
* Class FlightFields
* Class FlightFieldValue
* @package App\Models
*/
class FlightFields extends Model
class FlightFieldValue extends Model
{
public $table = 'flight_fields';
public $table = 'flight_field_values';
public $fillable = [
'flight_id',

View File

@@ -34,8 +34,6 @@ class Pirep extends Model
{
use HashIdTrait;
public const ID_MAX_LENGTH = 12;
public $table = 'pireps';
public $incrementing = false;

View File

@@ -2,6 +2,7 @@
namespace App\Models\Traits;
use App\Interfaces\Model;
use Hashids\Hashids;
trait HashIdTrait
@@ -10,11 +11,10 @@ trait HashIdTrait
* @return string
* @throws \Hashids\HashidsException
*/
protected static function createNewHashId(): string
final protected static function createNewHashId(): string
{
$hashids = new Hashids('', 12);
$hashids = new Hashids('', Model::ID_MAX_LENGTH);
$mt = str_replace('.', '', microtime(true));
return $hashids->encode($mt);
}
@@ -22,7 +22,7 @@ trait HashIdTrait
* Register callbacks
* @throws \Hashids\HashidsException
*/
protected static function bootHashIdTrait()
final protected static function bootHashIdTrait(): void
{
static::creating(function ($model) {
if (empty($model->id)) {

View File

@@ -32,7 +32,7 @@ Route::group([
# flights and aircraft associations
Route::resource('flights', 'FlightController');
Route::match(['get', 'post', 'put', 'delete'], 'flights/{id}/fares', 'FlightController@fares');
Route::match(['get', 'post', 'put', 'delete'], 'flights/{id}/fields', 'FlightController@fields');
Route::match(['get', 'post', 'put', 'delete'], 'flights/{id}/fields', 'FlightController@field_values');
Route::match(['get', 'post', 'put', 'delete'], 'flights/{id}/subfleets', 'FlightController@subfleets');
# pirep related routes

View File

@@ -6,6 +6,7 @@ use App\Exceptions\BidExists;
use App\Interfaces\Service;
use App\Models\Bid;
use App\Models\Flight;
use App\Models\FlightFieldValue;
use App\Models\User;
use App\Repositories\FlightRepository;
use App\Repositories\NavdataRepository;
@@ -104,13 +105,33 @@ class FlightService extends Service
* @param Flight $flight
* @throws \Exception
*/
public function deleteFlight(Flight $flight)
public function deleteFlight(Flight $flight): void
{
$where = ['flight_id' => $flight->id];
Bid::where($where)->delete();
$flight->delete();
}
/**
* Update any custom PIREP fields
* @param Flight $flight_id
* @param array $field_values
*/
public function updateCustomFields(Flight $flight_id, array $field_values): void
{
foreach ($field_values as $fv) {
FlightFieldValue::updateOrCreate(
[
'flight_id' => $flight_id,
'name' => $fv['name'],
],
[
'value' => $fv['value']
]
);
}
}
/**
* Return all of the navaid points as a collection
* @param Flight $flight

View File

@@ -1,15 +1,15 @@
<div id="flight_fields_wrapper">
<h3>custom fields</h3><br />
<table class="table table-responsive" id="flight-fields-table">
@if(count($flight->fields))
@if(count($flight->field_values))
<thead>
<th>Name</th>
<th style="text-align: center;">Value</th>
<th style="text-align: center;">Actions</th>
<th>Value</th>
<th></th>
</thead>
@endif
<tbody>
@foreach($flight->fields as $field)
@foreach($flight->field_values as $field)
<tr>
<td>{{ $field->name }}</td>
<td>

View File

@@ -62,11 +62,13 @@ $(document).ready(function () {
$(document).on('submit', 'form.pjax_flight_fields', function (event) {
event.preventDefault();
$.pjax.submit(event, '#flight_fields_wrapper', {push: false});
setEditable();
});
$(document).on('submit', 'form.pjax_subfleet_form', function (event) {
event.preventDefault();
$.pjax.submit(event, '#subfleet_flight_wrapper', {push: false});
setEditable();
});
$(document).on('submit', 'form.pjax_fares_form', function (event) {