From e9b30fbe3073222afb155370afe0d0f415b8b3c9 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Sun, 29 Mar 2020 13:33:14 -0400 Subject: [PATCH] Make sure user and acars data is sent from API --- app/Http/Controllers/Api/PirepController.php | 10 +++++++--- app/Http/Controllers/Api/UserController.php | 2 +- app/Http/Resources/Pirep.php | 6 ++++-- app/Http/Resources/User.php | 15 +++----------- app/Models/Pirep.php | 1 + app/Models/User.php | 10 ++++++++++ app/Repositories/AcarsRepository.php | 5 +---- tests/AcarsTest.php | 21 ++++++++++++++++++-- 8 files changed, 46 insertions(+), 24 deletions(-) diff --git a/app/Http/Controllers/Api/PirepController.php b/app/Http/Controllers/Api/PirepController.php index 7b96c239..cd9ff944 100644 --- a/app/Http/Controllers/Api/PirepController.php +++ b/app/Http/Controllers/Api/PirepController.php @@ -162,13 +162,17 @@ class PirepController extends Controller } /** - * @param $pirep_id + * @param string $id The PIREP ID * * @return PirepResource */ - public function get($pirep_id) + public function get($id) { - return new PirepResource($this->pirepRepo->find($pirep_id)); + $pirep = $this->pirepRepo + ->with(['acars', 'arr_airport', 'dpt_airport', 'comments', 'flight', 'simbrief', 'user']) + ->find($id); + + return new PirepResource($pirep); } /** diff --git a/app/Http/Controllers/Api/UserController.php b/app/Http/Controllers/Api/UserController.php index ae64262f..60fc0b15 100644 --- a/app/Http/Controllers/Api/UserController.php +++ b/app/Http/Controllers/Api/UserController.php @@ -90,7 +90,7 @@ class UserController extends Controller public function get($id) { $user = $this->userRepo - ->with(['airline', 'rank']) + ->with(['airline', 'bids', 'rank']) ->find($id); return new UserResource($user); diff --git a/app/Http/Resources/Pirep.php b/app/Http/Resources/Pirep.php index ec8b9b12..18d23aa2 100644 --- a/app/Http/Resources/Pirep.php +++ b/app/Http/Resources/Pirep.php @@ -27,6 +27,7 @@ class Pirep extends Resource { $res = parent::toArray($request); $res['ident'] = $this->ident; + $res['status_text'] = PirepStatus::label($this->status); // Set these to the response units if (!array_key_exists('distance', $res)) { @@ -62,8 +63,6 @@ class Pirep extends Resource $res['block_off_time'] = $this->block_off_time->toIso8601ZuluString(); } - $res['status_text'] = PirepStatus::label($this->status); - $res['airline'] = new Airline($this->airline); $res['dpt_airport'] = new Airport($this->dpt_airport); $res['arr_airport'] = new Airport($this->arr_airport); @@ -73,10 +72,13 @@ class Pirep extends Resource $res['user'] = [ 'id' => $this->user->id, 'name' => $this->user->name, + 'avatar' => $this->user->resolveAvatarUrl(), 'home_airport_id' => $this->user->home_airport_id, 'curr_airport_id' => $this->user->curr_airport_id, ]; + $res['flight'] = Flight::make($this->whenLoaded('flight')); + // format to kvp $res['fields'] = new PirepFieldCollection($this->fields); diff --git a/app/Http/Resources/User.php b/app/Http/Resources/User.php index 65254e68..147dec2d 100644 --- a/app/Http/Resources/User.php +++ b/app/Http/Resources/User.php @@ -17,6 +17,7 @@ class User extends Resource 'ident' => $this->ident, 'name' => $this->name, 'email' => $this->email, + 'avatar' => $this->resolveAvatarUrl(), 'rank_id' => $this->rank_id, 'home_airport' => $this->home_airport_id, 'curr_airport' => $this->curr_airport_id, @@ -27,19 +28,9 @@ class User extends Resource 'state' => $this->state, ]; - $res['airline'] = Airline::make($this->airline); + $res['airline'] = Airline::make($this->whenLoaded('airline')); $res['bids'] = UserBid::collection($this->whenLoaded('bids')); - $res['rank'] = Rank::make($this->rank); - - /* - * Determine which avatar to send/use - */ - $res['avatar'] = $this->avatar; - if (empty($res['avatar'])) { - $res['avatar'] = $this->gravatar(); - } else { - $res['avatar'] = $res['avatar']->url; - } + $res['rank'] = Rank::make($this->whenLoaded('rank')); return $res; } diff --git a/app/Models/Pirep.php b/app/Models/Pirep.php index 7035095b..9409d799 100644 --- a/app/Models/Pirep.php +++ b/app/Models/Pirep.php @@ -14,6 +14,7 @@ use Illuminate\Support\Collection; /** * @property string id + * @property string ident * @property string flight_number * @property string route_code * @property string route_leg diff --git a/app/Models/User.php b/app/Models/User.php index 7f22ce3b..3a2933ba 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -170,6 +170,16 @@ class User extends Authenticatable return $uri; } + public function resolveAvatarUrl() + { + $avatar = $this->getAvatarAttribute(); + if (empty($avatar)) { + return $this->gravatar(); + } else { + return $avatar->url; + } + } + /** * Foreign Keys */ diff --git a/app/Repositories/AcarsRepository.php b/app/Repositories/AcarsRepository.php index ac14e2db..7b4df0e8 100644 --- a/app/Repositories/AcarsRepository.php +++ b/app/Repositories/AcarsRepository.php @@ -9,9 +9,6 @@ use App\Models\Enums\PirepState; use App\Models\Pirep; use Carbon\Carbon; -/** - * Class AcarsRepository - */ class AcarsRepository extends Repository { /** @@ -59,7 +56,7 @@ class AcarsRepository extends Repository */ public function getPositions($live_time = 0) { - $q = Pirep::with(['airline', 'position', 'aircraft']) + $q = Pirep::with(['aircraft', 'airline', 'arr_airport', 'dpt_airport', 'position', 'user']) ->where(['state' => PirepState::IN_PROGRESS]); if ($live_time !== null && $live_time > 0) { diff --git a/tests/AcarsTest.php b/tests/AcarsTest.php index 9eec1ab4..a81fb70e 100644 --- a/tests/AcarsTest.php +++ b/tests/AcarsTest.php @@ -416,7 +416,8 @@ class AcarsTest extends TestCase */ $uri = '/api/pireps/'.$pirep_id.'/update'; $this->post($uri, [ - 'fields' => [ + 'flight_time' => 60, + 'fields' => [ 'custom_field' => 'custom_value_changed', ], ]); @@ -476,7 +477,23 @@ class AcarsTest extends TestCase $body = $response->json('data'); $this->assertEquals('G26', $body['Departure Gate']); - // File the PIREP now + /* + * Get the live flights and make sure all the fields we want are there + */ + $uri = '/api/acars'; + $response = $this->get($uri); + + $response->assertStatus(200); + $body = $response->json('data'); + + $this->assertEquals($pirep->id, $body['id']); + $this->assertNotEmpty($body['user']['name']); + $this->assertNotEmpty($body['user']['avatar']); + + /* + * File the PIREP + */ + $uri = '/api/pireps/'.$pirep_id.'/file'; $response = $this->post($uri, []); $response->assertStatus(400); // missing field