From 9964eb63fb86272ff14f679f0605b821f179eb94 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Tue, 23 Jan 2018 21:40:34 -0600 Subject: [PATCH] Add pirep comments and subfleets fares #118 --- app/Http/Controllers/Api/FleetController.php | 4 +- app/Http/Controllers/Api/PirepController.php | 40 ++++++ app/Http/Resources/PirepComment.php | 35 +++++ app/Http/Resources/Response.php | 24 ++++ app/Http/Resources/User.php | 1 + app/Routes/api.php | 3 + composer.lock | 136 +++++++++---------- tests/AcarsTest.php | 14 +- tests/ApiTest.php | 6 + 9 files changed, 191 insertions(+), 72 deletions(-) create mode 100644 app/Http/Resources/PirepComment.php create mode 100644 app/Http/Resources/Response.php diff --git a/app/Http/Controllers/Api/FleetController.php b/app/Http/Controllers/Api/FleetController.php index be950893..1abaf99b 100644 --- a/app/Http/Controllers/Api/FleetController.php +++ b/app/Http/Controllers/Api/FleetController.php @@ -51,9 +51,9 @@ class FleetController extends RestController $where['id'] = $id; } - $all_aircraft = $this->aircraftRepo->all(); + #$all_aircraft = $this->aircraftRepo->all(); $aircraft = $this->aircraftRepo - ->with(['subfleet']) + ->with(['subfleet', 'subfleet.fares']) ->findWhere($where) ->first(); diff --git a/app/Http/Controllers/Api/PirepController.php b/app/Http/Controllers/Api/PirepController.php index 48e8e97f..c11c58bb 100644 --- a/app/Http/Controllers/Api/PirepController.php +++ b/app/Http/Controllers/Api/PirepController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Api; +use App\Models\PirepComment; use Log; use Auth; use Illuminate\Database\Eloquent\ModelNotFoundException; @@ -20,6 +21,7 @@ use App\Repositories\AcarsRepository; use App\Repositories\PirepRepository; use App\Http\Resources\Pirep as PirepResource; +use App\Http\Resources\PirepComment as PirepCommentResource; use App\Http\Resources\AcarsRoute as AcarsRouteResource; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; @@ -328,6 +330,44 @@ class PirepController extends RestController return $this->message($count . ' logs added', $count); } + /** + * Add a new comment + * @param $id + * @param Request $request + * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection + */ + public function comments_get($id, Request $request) + { + $pirep = $this->pirepRepo->find($id); + return PirepCommentResource::collection($pirep->comments); + } + + /** + * Add a new comment + * @param $id + * @param Request $request + * @return PirepCommentResource + * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException + */ + public function comments_post($id, Request $request) + { + $pirep = $this->pirepRepo->find($id); + if ($pirep->state === PirepState::CANCELLED) { + throw new BadRequestHttpException('PIREP has been cancelled, comments can\'t be posted'); + } + + # validation + $this->validate($request, ['comment' => 'required']); + + # Add it + $comment = new PirepComment($request->post()); + $comment->pirep_id = $id; + $comment->user_id = Auth::user()->id; + $comment->save(); + + return new PirepCommentResource($comment); + } + /** * @param $id * @param Request $request diff --git a/app/Http/Resources/PirepComment.php b/app/Http/Resources/PirepComment.php new file mode 100644 index 00000000..9783e8ad --- /dev/null +++ b/app/Http/Resources/PirepComment.php @@ -0,0 +1,35 @@ +user; + + return [ + 'id' => $this->id, + 'comment' => $this->comment, + 'created_at' => $this->created_at, + 'user' => [ + 'id' => $user->id, + 'pilot_id' => $user->pilot_id, + 'name' => $user->name, + ], + ]; + } +} diff --git a/app/Http/Resources/Response.php b/app/Http/Resources/Response.php new file mode 100644 index 00000000..ac0bbc57 --- /dev/null +++ b/app/Http/Resources/Response.php @@ -0,0 +1,24 @@ + $this->id, + 'pilot_id' => $this->pilot_id, 'name' => $this->name, 'email' => $this->email, 'apikey' => $this->apikey, diff --git a/app/Routes/api.php b/app/Routes/api.php index 05c71c3c..615228b4 100755 --- a/app/Routes/api.php +++ b/app/Routes/api.php @@ -26,6 +26,7 @@ Route::group([], function() Route::get('pireps/{id}/route', 'PirepController@route_get'); Route::get('pireps/{id}/acars/position', 'PirepController@acars_get'); Route::get('pireps/{id}/acars/geojson', 'PirepController@acars_geojson'); + Route::get('pireps/{id}/comments', 'PirepController@comments_get'); Route::get('status', 'StatusController@status'); Route::get('version', 'StatusController@status'); @@ -46,6 +47,8 @@ Route::group(['middleware' => ['api.auth']], function () Route::post('pireps/{id}/acars/log', 'PirepController@acars_log'); Route::post('pireps/{id}/acars/logs', 'PirepController@acars_log'); + Route::post('pireps/{id}/comments', 'PirepController@comments_post'); + Route::post('pireps/{id}/route', 'PirepController@route_post'); Route::delete('pireps/{id}/route', 'PirepController@route_delete'); diff --git a/composer.lock b/composer.lock index d2555db4..9236e706 100644 --- a/composer.lock +++ b/composer.lock @@ -48,7 +48,7 @@ "Arrilot\\Widgets\\": "src/" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -488,7 +488,7 @@ "Doctrine\\DBAL\\": "lib/" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -811,7 +811,7 @@ "Egulias\\EmailValidator\\": "EmailValidator" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -857,7 +857,7 @@ "Parsedown": "" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -902,7 +902,7 @@ "Firebase\\JWT\\": "src" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "BSD-3-Clause" ], @@ -970,7 +970,7 @@ "src/Google/Service/" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "Apache-2.0" ], @@ -1007,7 +1007,7 @@ "Google_Service_": "src" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "Apache-2.0" ], @@ -1052,7 +1052,7 @@ "Google\\Auth\\": "src" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "Apache-2.0" ], @@ -1106,7 +1106,7 @@ "GuzzleHttp\\": "src/" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -1164,7 +1164,7 @@ "src/functions_include.php" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -1219,7 +1219,7 @@ "src/functions_include.php" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -1281,7 +1281,7 @@ "Hashids\\": "src/" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -1398,7 +1398,7 @@ "Irazasyed\\LaravelGAMP\\": "src/" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -1445,7 +1445,7 @@ "Jackiedo\\Timezonelist\\": "src/Jackiedo/Timezonelist" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -1498,7 +1498,7 @@ "stubs/" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -1544,7 +1544,7 @@ "Traitor\\": "src/" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -1603,7 +1603,7 @@ "src/Laracasts/Flash/functions.php" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -1732,7 +1732,7 @@ "Illuminate\\": "src/Illuminate/" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -2068,7 +2068,7 @@ "League\\ISO3166\\": "src" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -2131,7 +2131,7 @@ } ], "description": "Bloom filter implementation", - "time": "2017-11-30 17:51:14" + "time": "2017-11-30T17:51:14+00:00" }, { "name": "monolog/monolog", @@ -2237,7 +2237,7 @@ "Cron\\": "src/Cron/" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -2286,11 +2286,11 @@ "VaCentral\\": "src/" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], - "time": "2017-12-08 04:00:06" + "time": "2017-12-08T04:00:06+00:00" }, { "name": "nesbot/carbon", @@ -2380,7 +2380,7 @@ "PhpParser\\": "lib/PhpParser" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "BSD-3-Clause" ], @@ -2657,7 +2657,7 @@ "phpseclib\\": "phpseclib/" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -2753,7 +2753,7 @@ "PragmaRX\\Version\\Tests\\": "tests/" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -2813,7 +2813,7 @@ "PragmaRX\\Yaml\\Tests\\": "tests/" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -2876,7 +2876,7 @@ "Prettus\\Repository\\": "src/Prettus/Repository/" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -2965,7 +2965,7 @@ "Psr\\Cache\\": "src/" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -3229,7 +3229,7 @@ "Ramsey\\Uuid\\": "src/" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -3389,7 +3389,7 @@ "Laratrust\\": "src/" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -3509,7 +3509,7 @@ "Spatie\\Fractalistic\\": "src" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -3575,7 +3575,7 @@ "src/helpers.php" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -3758,7 +3758,7 @@ "/Tests/" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -3807,7 +3807,7 @@ "/Tests/" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -3867,7 +3867,7 @@ "/Tests/" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -3923,7 +3923,7 @@ "/Tests/" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -3986,7 +3986,7 @@ "/Tests/" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -4035,7 +4035,7 @@ "/Tests/" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -4089,7 +4089,7 @@ "/Tests/" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -4177,7 +4177,7 @@ "/Tests/" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -4226,7 +4226,7 @@ "/Tests/" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -4378,7 +4378,7 @@ "bootstrap.php" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -4438,7 +4438,7 @@ "Resources/stubs" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -4493,7 +4493,7 @@ "/Tests/" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -4550,7 +4550,7 @@ "/Tests/" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -4633,7 +4633,7 @@ "/Tests/" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -4717,7 +4717,7 @@ "/Tests/" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -4785,7 +4785,7 @@ "/Tests/" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -4850,7 +4850,7 @@ "/Tests/" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -4912,7 +4912,7 @@ "/Tests/" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -4958,7 +4958,7 @@ "TheIconic\\Tracking\\GoogleAnalytics\\": "src/" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -5003,7 +5003,7 @@ "TijsVerkoyen\\CssToInlineStyles\\": "src" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "BSD-3-Clause" ], @@ -5044,7 +5044,7 @@ "Tivie\\OS\\": "src/" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "APACHE 2.0" ], @@ -5162,7 +5162,7 @@ "src/vierbergenlars/SemVer/internal.php" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -5267,7 +5267,7 @@ "Webpatser\\Uuid": "src/" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -5383,7 +5383,7 @@ "Zend\\Diactoros\\": "src/" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "BSD-2-Clause" ], @@ -5609,7 +5609,7 @@ "Whoops\\": "src/Whoops/" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -5823,7 +5823,7 @@ "src/DeepCopy/deep_copy.php" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -5936,7 +5936,7 @@ "Orchestra\\Testbench\\": "src/" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -6153,7 +6153,7 @@ ] } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -6249,7 +6249,7 @@ "Prophecy\\": "src/" } }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -6367,7 +6367,7 @@ "src/" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "BSD-3-Clause" ], @@ -6508,7 +6508,7 @@ "src/" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "BSD-3-Clause" ], @@ -6589,7 +6589,7 @@ "src/" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "BSD-3-Clause" ], @@ -6779,7 +6779,7 @@ "src/" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "BSD-3-Clause" ], @@ -7298,7 +7298,7 @@ "/Tests/" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -7442,7 +7442,7 @@ "src/functions.php" ] }, - "notification-url": "http://packagist.org/downloads/", + "notification-url": "https://packagist.org/downloads/", "license": [ "Apache2" ], diff --git a/tests/AcarsTest.php b/tests/AcarsTest.php index 2a04ef4c..da7b5a84 100644 --- a/tests/AcarsTest.php +++ b/tests/AcarsTest.php @@ -48,8 +48,7 @@ class AcarsTest extends TestCase protected function getPirep($pirep_id) { $this->user = factory(App\Models\User::class)->create(); - $resp = $this - ->get('/api/pireps/' . $pirep_id); + $resp = $this ->get('/api/pireps/' . $pirep_id); $resp->assertStatus(200); return $resp->json(); } @@ -163,6 +162,17 @@ class AcarsTest extends TestCase $response = $this->post($uri, ['flight_time' => '130']); $response->assertStatus(200); // invalid flight time + + # Add a comment + $uri = '/api/pireps/'.$pirep_id.'/comments'; + $response = $this->post($uri, ['comment' => 'A comment']); + $response->assertStatus(201); + + $response = $this->get($uri); + $response->assertStatus(200); + $comments = $response->json(); + + $this->assertCount(1, $comments); } /** diff --git a/tests/ApiTest.php b/tests/ApiTest.php index 0a5a6798..681a4539 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -1,6 +1,7 @@ create(); + $fare = factory(App\Models\Fare::class)->create(); + + $fare_svc->setForSubfleet($subfleet, $fare); $aircraft = factory(App\Models\Aircraft::class)->create([ 'subfleet_id' => $subfleet->id ]);