set fare classes on aircraft w/ overrides (no admin ui yet)

This commit is contained in:
Nabeel Shahzad
2017-06-10 18:27:19 -05:00
parent dee3503ad6
commit bf910e4549
27 changed files with 859 additions and 88 deletions

View File

@@ -0,0 +1,146 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\CreateFareRequest;
use App\Http\Requests\UpdateFareRequest;
use App\Repositories\FareRepository;
use App\Http\Controllers\AppBaseController;
use Illuminate\Http\Request;
use Flash;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;
class FareController extends AppBaseController
{
/** @var FareRepository */
private $fareRepository;
public function __construct(FareRepository $fareRepo)
{
$this->fareRepository = $fareRepo;
}
/**
* Display a listing of the Fare.
*
* @param Request $request
*
* @return Response
*/
public function index(Request $request)
{
$this->fareRepository->pushCriteria(new RequestCriteria($request));
$fares = $this->fareRepository->all();
return view('admin.fares.index')
->with('fares', $fares);
}
/**
* Show the form for creating a new Fare.
*
* @return Response
*/
public function create()
{
return view('admin.fares.create');
}
/**
* Store a newly created Fare in storage.
*
* @param CreateFareRequest $request
*
* @return Response
*/
public function store(CreateFareRequest $request)
{
$input = $request->all();
$fare = $this->fareRepository->create($input);
Flash::success('Fare saved successfully.');
return redirect(route('admin.fares.index'));
}
/**
* Display the specified Fare.
*
* @param int $id
*
* @return Response
*/
public function show($id)
{
$fare = $this->fareRepository->findWithoutFail($id);
if (empty($fare)) {
Flash::error('Fare not found');
return redirect(route('admin.fares.index'));
}
return view('admin.fares.show')->with('fare', $fare);
}
/**
* Show the form for editing the specified Fare.
*
* @param int $id
*
* @return Response
*/
public function edit($id)
{
$fare = $this->fareRepository->findWithoutFail($id);
if (empty($fare)) {
Flash::error('Fare not found');
return redirect(route('admin.fares.index'));
}
return view('admin.fares.edit')->with('fare', $fare);
}
/**
* Update the specified Fare in storage.
*
* @param int $id
* @param UpdateFareRequest $request
*
* @return Response
*/
public function update($id, UpdateFareRequest $request)
{
$fare = $this->fareRepository->findWithoutFail($id);
if (empty($fare)) {
Flash::error('Fare not found');
return redirect(route('admin.fares.index'));
}
$fare = $this->fareRepository->update($request->all(), $id);
Flash::success('Fare updated successfully.');
return redirect(route('admin.fares.index'));
}
/**
* Remove the specified Fare from storage.
*
* @param int $id
*
* @return Response
*/
public function destroy($id)
{
$fare = $this->fareRepository->findWithoutFail($id);
if (empty($fare)) {
Flash::error('Fare not found');
return redirect(route('admin.fares.index'));
}
$this->fareRepository->delete($id);
Flash::success('Fare deleted successfully.');
return redirect(route('admin.fares.index'));
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use App\Models\Fare;
class CreateFareRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return Fare::$rules;
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use App\Models\Fare;
class UpdateFareRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return Fare::$rules;
}
}

View File

@@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Class Aircraft
*
* @package App\Models
* @version June 9, 2017, 1:06 am UTC
*/
@@ -15,42 +16,46 @@ class Aircraft extends Model
use SoftDeletes;
public $table = 'aircraft';
protected $dates = ['deleted_at'];
public $fillable = [
'aircraft_class_id',
'icao',
'name',
'full_name',
'registration',
'active'
];
public $fillable
= [
'aircraft_class_id',
'icao',
'name',
'full_name',
'registration',
'active',
];
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'icao' => 'string',
'name' => 'string',
'full_name' => 'string',
'registration' => 'string',
'active' => 'boolean',
];
protected $casts
= [
'icao' => 'string',
'name' => 'string',
'full_name' => 'string',
'registration' => 'string',
'active' => 'boolean',
];
/**
* Validation rules
*
* @var array
*/
public static $rules = [
'icao' => 'required|max:4',
'name' => 'required',
'full_name' => 'required',
'registration' => 'required',
'active' => 'default:1'
];
public static $rules
= [
'icao' => 'required|max:4',
'name' => 'required',
'full_name' => 'required',
'registration' => 'required',
'active' => 'default:1',
];
/**
* foreign key
@@ -63,8 +68,12 @@ class Aircraft extends Model
);
}
public function fares() {
# aircraft_fare == table name
return $this->belongsToMany('App\Models\Fare', 'aircraft_fare');
public function fares()
{
$r = $this->belongsToMany(
'App\Models\Fare',
'aircraft_fare'
)->withPivot('price', 'cost', 'capacity');
return $r;
}
}

66
app/Models/Fare.php Normal file
View File

@@ -0,0 +1,66 @@
<?php
namespace App\Models;
use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Class Fare
*
* @package App\Models
* @version June 10, 2017, 4:03 am UTC
*/
class Fare extends Model
{
use SoftDeletes;
public $table = 'fares';
protected $dates = ['deleted_at'];
public $fillable
= [
'code',
'name',
'price',
'cost',
'notes',
'active',
];
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts
= [
'code' => 'string',
'name' => 'string',
'price' => 'float',
'cost' => 'float',
'notes' => 'string',
'active' => 'boolean',
];
/**
* Validation rules
*
* @var array
*/
public static $rules
= [
'code' => 'required',
'name' => 'required',
'cost' => 'default:0.0',
];
public function aircraft() {
return $this->belongsToMany(
'App\Models\Aircraft',
'aircraft_fare'
)->withPivot('price', 'cost', 'capacity');
}
}

View File

@@ -4,6 +4,8 @@ namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Services\AircraftService;
class AppServiceProvider extends ServiceProvider
{
/**
@@ -23,6 +25,13 @@ class AppServiceProvider extends ServiceProvider
*/
public function register()
{
//
// bind all the app services...
$this->app->bind('App\Services\AircraftService', function($app) {
return new \App\Services\AircraftService();
});
$this->app->bind('App\Services\AircraftFareService', function($app) {
return new \App\Services\AircraftFareService();
});
}
}

View File

@@ -3,7 +3,6 @@
namespace App\Repositories;
use App\Models\AircraftClass;
use InfyOm\Generator\Common\BaseRepository;
class AircraftClassRepository extends BaseRepository
{

View File

@@ -3,20 +3,20 @@
namespace App\Repositories;
use App\Models\Aircraft;
use InfyOm\Generator\Common\BaseRepository;
class AircraftRepository extends BaseRepository
{
/**
* @var array
*/
protected $fieldSearchable = [
'icao',
'name',
'full_name',
'registration',
'active',
];
protected $fieldSearchable
= [
'icao',
'name',
'full_name',
'registration',
'active',
];
/**
* Configure the Model
@@ -25,4 +25,9 @@ class AircraftRepository extends BaseRepository
{
return Aircraft::class;
}
public function findByICAO($icao)
{
return $this->findByField('icao', $icao)->first();
}
}

View File

@@ -3,7 +3,6 @@
namespace App\Repositories;
use App\Models\Airlines;
use InfyOm\Generator\Common\BaseRepository;
class AirlinesRepository extends BaseRepository
{

View File

@@ -0,0 +1,21 @@
<?php
namespace App\Repositories;
use Illuminate\Validation\Validator;
abstract class BaseRepository extends \InfyOm\Generator\Common\BaseRepository {
public function validate($values) {
$validator = Validator::make(
$values,
$this->model()->rules
);
if($validator->fails()) {
return $validator->messages();
}
return true;
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace App\Repositories;
use App\Models\Fare;
class FareRepository extends BaseRepository
{
/**
* @var array
*/
protected $fieldSearchable = [
'code',
'name',
'price',
'cost',
'notes',
'active'
];
/**
* Configure the Model
**/
public function model()
{
return Fare::class;
}
public function findByCode($code) {
return $this->findByField('code', $code)->first();
}
}

View File

@@ -1,30 +0,0 @@
<?php
namespace App\Services;
use App\Repositories\FareRepository;
use App\Repositories\AircraftRepository;
class AircraftFareService {
protected $aircraft, $fare;
/**
* return a PIREP model
* @param $aircraft AircraftRepository
* @param $fare FareRepository
*/
public function __construct(AircraftRepository $aircraft, FareRepository $fare) {
$this->fare = $fare;
$this->aircraft = $aircraft;
}
public function link(int $aircraft_id, int $fare_id) {
}
public function unlink(int $aircraft_id, int $fare_id) {
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Services;
use App\Models\Aircraft;
use App\Models\AircraftClass;
use Dompdf\Exception;
class AircraftService extends BaseService
{
public function create(
array $attributes,
AircraftClass $class = null
) {
$repo = app('App\Repositories\AircraftRepository');
try {
$model = $repo->create($attributes);
} catch (Exception $e) {
return false;
}
if ($class != null) {
$model->class()->associate($class);
$model->save();
}
return $model;
}
}

View File

@@ -0,0 +1,7 @@
<?php
namespace App\Services;
class BaseService {
}

View File

@@ -0,0 +1,70 @@
<?php
namespace App\Services;
use App\Models\Aircraft;
use App\Models\Fare;
class FareService extends BaseService {
/**
* Attach a fare to an aircraft
*
* @param Aircraft $aircraft
* @param Fare $fare
* @param array set the price/cost/capacity
*
* @return Aircraft
*/
public function set_for_aircraft(
Aircraft &$aircraft,
Fare &$fare,
array $override=[]
) {
$aircraft->fares()->syncWithoutDetaching([$fare->id]);
# modify any pivot values?
if(count($override) > 0) {
$aircraft->fares()->updateExistingPivot($fare->id, $override);
}
$aircraft->save();
$aircraft = $aircraft->fresh();
return $aircraft;
}
/**
* return all the fares for an aircraft. check the pivot
* table to see if the price/cost/capacity has been overridden
* and return the correct amounts.
* @param Aircraft $aircraft
* @return Fare[]
*/
public function get_for_aircraft(Aircraft &$aircraft)
{
$fares = [];
foreach($aircraft->fares as $fare) {
if(!is_null($fare->pivot->price)) {
$fare->price = $fare->pivot->price;
}
if(!is_null($fare->pivot->cost)) {
$fare->cost = $fare->pivot->cost;
}
if(!is_null($fare->pivot->capacity)) {
$fare->capacity = $fare->pivot->capacity;
}
array_push($fares, $fare);
}
return $fares;
}
public function delete_from_aircraft(Aircraft &$aircraft, Fare &$fare)
{
$aircraft->fares()->detach($fare->id);
$aircraft = $aircraft->fresh();
return $aircraft;
}
}

View File

@@ -5,7 +5,7 @@ namespace App\Services;
use App\Repositories\AircraftRepository;
class PIREPService {
class PIREPService extends BaseService {
protected $aircraft;