Skip to content

Commit

Permalink
Add /flight/:id/route resource, returns all route info that's in the …
Browse files Browse the repository at this point in the history
…navaid database closes #183
  • Loading branch information
nabeelio committed Feb 20, 2018
1 parent fdf18cf commit 8393ae2
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 22 deletions.
2 changes: 1 addition & 1 deletion app/Database/factories/FlightFactory.php
Expand Up @@ -25,7 +25,7 @@
return factory(App\Models\Airport::class)->create()->id;
},
'distance' => $faker->numberBetween(0, 3000),
'route' => $faker->randomElement(['', $faker->text(5)]),
'route' => null,
'dpt_time' => $faker->time(),
'arr_time' => $faker->time(),
'flight_time' => $faker->numberBetween(60, 360),
Expand Down
2 changes: 1 addition & 1 deletion app/Database/factories/NavdataFactory.php
Expand Up @@ -6,7 +6,7 @@
$factory->define(App\Models\Navdata::class, function (Faker $faker) {
return [
'id' => str_replace(' ', '', str_replace('.', '', $faker->unique()->text(5))),
'name' => $faker->unique()->text(10),
'name' => str_replace('.', '', $faker->unique()->word),
'type' => $faker->randomElement([NavaidType::VOR, NavaidType::NDB]),
'lat' => $faker->latitude,
'lon' => $faker->longitude,
Expand Down
3 changes: 0 additions & 3 deletions app/Database/factories/PirepFactory.php
Expand Up @@ -8,8 +8,6 @@
*/
$factory->define(App\Models\Pirep::class, function (Faker $faker) {

static $raw_data;

return [
'id' => null,
'airline_id' => function () {
Expand Down Expand Up @@ -48,7 +46,6 @@
'source_name' => 'Test Factory',
'state' => PirepState::PENDING,
'status' => PirepStatus::SCHEDULED,
'raw_data' => $raw_data ?: $raw_data = json_encode(['key' => 'value']),
'created_at' => Carbon::now()->toDateTimeString(),
'updated_at' => function(array $pirep) {
return $pirep['created_at'];
Expand Down
17 changes: 16 additions & 1 deletion app/Http/Controllers/Api/FlightController.php
Expand Up @@ -2,7 +2,6 @@

namespace App\Http\Controllers\Api;

use App\Http\Resources\FlightCollection;
use App\Repositories\Criteria\WhereCriteria;
use App\Services\FlightService;
use Auth;
Expand All @@ -12,7 +11,9 @@

use App\Services\UserService;
use App\Repositories\FlightRepository;

use App\Http\Resources\Flight as FlightResource;
use App\Http\Resources\Navdata as NavdataResource;

/**
* Class FlightController
Expand Down Expand Up @@ -97,4 +98,18 @@ public function search(Request $request)

return FlightResource::collection($flights);
}

/**
* Get a flight's route
* @param $id
* @param Request $request
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
*/
public function route($id, Request $request)
{
$flight = $this->flightRepo->find($id);
$route = $this->flightSvc->getRoute($flight);

return NavdataResource::collection($route);
}
}
24 changes: 24 additions & 0 deletions app/Http/Resources/Navdata.php
@@ -0,0 +1,24 @@
<?php

namespace App\Http\Resources;

use App\Models\Enums\NavaidType;
use Illuminate\Http\Resources\Json\Resource;

class Navdata extends Resource
{
public function toArray($request)
{
$point = parent::toArray($request);

// Some details about the navaid type
$type = [
'type' => $point['type'],
'name' => NavaidType::label($point['type']),
];

$point['type'] = $type;

return $point;
}
}
3 changes: 0 additions & 3 deletions app/Models/GeoJson.php
Expand Up @@ -11,9 +11,6 @@
* Return different points/features in GeoJSON format
* https://tools.ietf.org/html/rfc7946
*
* Once a PIREP is accepted, save this returned structure as a
* JSON-encoded string into the raw_data field of the PIREP row
*
* @package App\Models
*/
class GeoJson
Expand Down
1 change: 1 addition & 0 deletions app/Routes/api.php
Expand Up @@ -31,6 +31,7 @@
Route::get('flights', 'FlightController@index');
Route::get('flights/search', 'FlightController@search');
Route::get('flights/{id}', 'FlightController@get');
Route::get('flights/{id}/route', 'FlightController@route');

Route::get('pireps/{pirep_id}', 'PirepController@get');
Route::put('pireps/{pirep_id}', 'PirepController@update');
Expand Down
52 changes: 40 additions & 12 deletions app/Services/FlightService.php
@@ -1,29 +1,31 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: nshahzad
* Date: 12/12/17
* Time: 2:48 PM
*/

namespace App\Services;

use App\Repositories\FlightRepository;
use function foo\func;
use Illuminate\Support\Collection;
use App\Models\Navdata;
use Log;

use App\Models\Flight;
use App\Models\User;
use App\Models\UserBid;
use App\Repositories\FlightRepository;
use App\Repositories\NavdataRepository;

/**
* Class FlightService
* @package App\Services
*/
class FlightService extends BaseService
{
protected $flightRepo, $userSvc;
protected $flightRepo, $navDataRepo, $userSvc;

public function __construct(FlightRepository $flightRepo, UserService $userSvc)
{
public function __construct(
FlightRepository $flightRepo,
NavdataRepository $navdataRepo,
UserService $userSvc
) {
$this->flightRepo = $flightRepo;
$this->navDataRepo = $navdataRepo;
$this->userSvc = $userSvc;
}

Expand Down Expand Up @@ -98,6 +100,32 @@ public function deleteFlight(Flight $flight)
$flight->delete();
}

/**
* Return all of the navaid points as a collection
* @param Flight $flight
* @return \Illuminate\Support\Collection
*/
public function getRoute(Flight $flight)
{
if(!$flight->route) {
return collect();
}

$route_points = array_map(function($point) {
return strtoupper($point);
}, explode(' ', $flight->route));

$route = $this->navDataRepo->findWhereIn('id', $route_points);

// Put it back into the original order the route is in
$return_points = [];
foreach($route_points as $rp) {
$return_points[] = $route->where('id', $rp)->first();
}

return collect($return_points);
}

/**
* Allow a user to bid on a flight. Check settings and all that good stuff
* @param Flight $flight
Expand Down
3 changes: 2 additions & 1 deletion tests/AcarsTest.php
Expand Up @@ -377,7 +377,8 @@ public function testAcarsRoutePost()
foreach($route as $position) {
$post_route[] = [
'order' => $order,
'name' => $position->name,
'id' => $position->id,
'name' => $position->id,
'lat' => $position->lat,
'lon' => $position->lon,
];
Expand Down
31 changes: 31 additions & 0 deletions tests/FlightTest.php
Expand Up @@ -68,6 +68,37 @@ public function testSearchFlight()
$req->assertStatus(200);
}

/**
* Get the flight's route
*/
public function testFlightRoute()
{
$this->user = factory(App\Models\User::class)->create();
$flight = $this->addFlight($this->user);

$route_count = random_int(4, 6);
$route = factory(App\Models\Navdata::class, $route_count)->create();
$route_text = implode(' ', $route->pluck('id')->toArray());

$flight->route = $route_text;
$flight->save();

$res = $this->get('/api/flights/'.$flight->id.'/route');
$res->assertStatus(200);
$body = $res->json();

$this->assertCount($route_count, $body['data']);

$first_point = $body['data'][0];
$this->assertEquals($first_point['id'], $route[0]->id);
$this->assertEquals($first_point['name'], $route[0]->name);
$this->assertEquals($first_point['type']['type'], $route[0]->type);
$this->assertEquals(
$first_point['type']['name'],
\App\Models\Enums\NavaidType::label($route[0]->type)
);
}

/**
* Find all of the flights
*/
Expand Down

0 comments on commit 8393ae2

Please sign in to comment.