delete the user bids when a flight is deleted #103

This commit is contained in:
Nabeel Shahzad 2018-01-01 09:32:04 -06:00
parent 904f64939f
commit c65da0a0af
5 changed files with 89 additions and 19 deletions

View File

@ -9,6 +9,7 @@ use App\Repositories\AirlineRepository;
use App\Repositories\AirportRepository;
use App\Repositories\FlightRepository;
use App\Repositories\SubfleetRepository;
use App\Services\FlightService;
use Illuminate\Http\Request;
use Flash;
use Response;
@ -18,17 +19,20 @@ class FlightController extends BaseController
private $airlineRepo,
$airportRepo,
$flightRepo,
$flightSvc,
$subfleetRepo;
public function __construct(
AirlineRepository $airlineRepo,
AirportRepository $airportRepo,
FlightRepository $flightRepo,
FlightService $flightSvc,
SubfleetRepository $subfleetRepo
) {
$this->airlineRepo = $airlineRepo;
$this->airportRepo = $airportRepo;
$this->flightRepo = $flightRepo;
$this->flightSvc = $flightSvc;
$this->subfleetRepo = $subfleetRepo;
}
@ -161,6 +165,7 @@ class FlightController extends BaseController
/**
* @param $id
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @throws \Exception
*/
public function destroy($id)
{
@ -171,7 +176,7 @@ class FlightController extends BaseController
return redirect(route('admin.flights.index'));
}
$this->flightRepo->delete($id);
$this->flightSvc->deleteFlight($flight);
Flash::success('Flight deleted successfully.');
return redirect(route('admin.flights.index'));

View File

@ -7,6 +7,7 @@ use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use App\Repositories\UserRepository;
use App\Models\User;
use App\Models\UserBid;
use App\Http\Resources\Flight as FlightResource;

View File

@ -16,6 +16,18 @@ use App\Models\UserBid;
class FlightService extends BaseService
{
/**
* Delete a flight, and all the user bids, etc associated with it
* @param Flight $flight
* @throws \Exception
*/
public function deleteFlight(Flight $flight)
{
$where = ['flight_id' => $flight->id];
UserBid::where($where)->delete();
$flight->delete();
}
/**
* Allow a user to bid on a flight. Check settings and all that good stuff
* @param Flight $flight

View File

@ -1,6 +1,7 @@
<?php
#use Swagger\Serializer;
use App\Models\User;
/**
* Test API calls and authentication, etc
@ -18,6 +19,7 @@ class ApiTest extends TestCase
*/
public function testApiAuthentication()
{
$user = factory(User::class)->create();
$airport = factory(App\Models\Airport::class)->create();
$uri = '/api/airports/' . $airport->icao;
@ -37,15 +39,15 @@ class ApiTest extends TestCase
->assertStatus(200)
->assertJson(['icao' => $airport->icao], true);
$this->withHeaders(['x-api-key' => 'testadminapikey'])->get($uri)
$this->withHeaders(['x-api-key' => $user->api_key])->get($uri)
->assertStatus(200)
->assertJson(['icao' => $airport->icao], true);
$this->withHeaders(['x-API-key' => 'testadminapikey'])->get($uri)
$this->withHeaders(['x-API-key' => $user->api_key])->get($uri)
->assertStatus(200)
->assertJson(['icao' => $airport->icao], true);
$this->withHeaders(['X-API-KEY' => 'testadminapikey'])->get($uri)
$this->withHeaders(['X-API-KEY' => $user->api_key])->get($uri)
->assertStatus(200)
->assertJson(['icao' => $airport->icao], true);
}
@ -55,19 +57,12 @@ class ApiTest extends TestCase
*/
public function testAirportRequest()
{
$airport = factory(App\Models\Airport::class)->create();
$response = $this->withHeaders($this->apiHeaders())->get('/api/airports/' . $airport->icao);
$response->assertStatus(200);
$response->assertJson(['icao' => $airport->icao], true);
/*$body = $response->json();
$serializer = new Serializer();
$swagger = $serializer->deserialize(\json_encode($body));
echo $swagger;*/
$this->withHeaders($this->apiHeaders())->get('/api/airports/UNK')
->assertStatus(404);
}

View File

@ -3,6 +3,7 @@
use App\Services\FlightService;
use App\Models\Flight;
use App\Models\User;
use App\Models\UserBid;
class FlightTest extends TestCase
{
@ -59,11 +60,15 @@ class FlightTest extends TestCase
*/
public function testBids()
{
$user = User::find(1);
$user = factory(User::class)->create();
$headers = [
'x-api-key' => $user->api_key,
];
$flight = $this->addFlight();
$bid = $this->flightSvc->addBid($flight, $user);
$this->assertEquals(1, $bid->user_id);
$this->assertEquals($user->id, $bid->user_id);
$this->assertEquals($flight->id, $bid->flight_id);
$this->assertTrue($flight->has_bid);
@ -71,15 +76,23 @@ class FlightTest extends TestCase
$flight = Flight::find($flight->id);
$this->assertTrue($flight->has_bid);
# Check the table and make sure thee entry is there
$user_bid = UserBid::where(['flight_id'=>$flight->id, 'user_id'=>$user->id])->get();
$this->assertNotNull($user_bid);
$user->refresh();
$this->assertEquals(1, $user->bids->count());
# Query the API and see that the user has the bids
# And pull the flight details for the user/bids
$req = $this->get('/api/user', self::$auth_headers);
$req = $this->get('/api/user', $headers);
$req->assertStatus(200);
$body = $req->json();
$this->assertEquals(1, sizeof($body['bids']));
$this->assertEquals($flight->id, $body['bids'][0]['flight_id']);
$req = $this->get('/api/users/1/bids', self::$auth_headers);
$req = $this->get('/api/users/'.$user->id.'/bids', $headers);
$body = $req->json();
$req->assertStatus(200);
@ -92,17 +105,18 @@ class FlightTest extends TestCase
$flight = Flight::find($flight->id);
$this->assertFalse($flight->has_bid);
$user = User::find(1);
$user->refresh();
$bids = $user->bids()->get();
$this->assertTrue($bids->isEmpty());
$req = $this->get('/api/user', self::$auth_headers);
$req = $this->get('/api/user', $headers);
$req->assertStatus(200);
$body = $req->json();
$this->assertEquals($user->id, $body['id']);
$this->assertEquals(0, sizeof($body['bids']));
$req = $this->get('/api/users/1/bids', self::$auth_headers);
$req = $this->get('/api/users/'.$user->id.'/bids', $headers);
$req->assertStatus(200);
$body = $req->json();
@ -116,7 +130,7 @@ class FlightTest extends TestCase
{
setting('bids.disable_flight_on_bid', true);
$user1 = User::find(1);
$user1 = factory(User::class)->create();;
$user2 = factory(User::class)->create();
$flight = $this->addFlight();
@ -127,4 +141,47 @@ class FlightTest extends TestCase
$bidRepeat = $this->flightSvc->addBid($flight, $user2);
$this->assertNull($bidRepeat);
}
/**
* Delete a flight and make sure all the bids are gone
*/
public function testDeleteFlight()
{
$user = factory(User::class)->create();
$headers = [
'x-api-key' => $user->api_key,
];
$flight = $this->addFlight();
$bid = $this->flightSvc->addBid($flight, $user);
$this->assertEquals($user->id, $bid->user_id);
$this->assertEquals($flight->id, $bid->flight_id);
$this->assertTrue($flight->has_bid);
$this->flightSvc->deleteFlight($flight);
$empty_flight = Flight::find($flight->id);
$this->assertNull($empty_flight);
# Make sure no bids exist
$user_bids = UserBid::where('flight_id', $flight->id)->get();
#$this->assertEquals(0, $user_bid->count());
# Query the API and see that the user has the bids
# And pull the flight details for the user/bids
$req = $this->get('/api/user', $headers);
$req->assertStatus(200);
$body = $req->json();
$this->assertEquals($user->id, $body['id']);
$this->assertEquals(0, sizeof($body['bids']));
$req = $this->get('/api/users/'.$user->id.'/bids', $headers);
$req->assertStatus(200);
$body = $req->json();
$this->assertEquals(0, sizeof($body));
}
}