Return the flight object with the bid

This commit is contained in:
Nabeel Shahzad 2018-02-27 13:11:48 -06:00
parent 4c22a098c1
commit a38b4e063b
3 changed files with 25 additions and 9 deletions

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers\Api;
use App\Http\Resources\Bid as BidResource;
use App\Http\Resources\Flight as FlightResource;
use App\Http\Resources\Pirep as PirepResource;
use App\Http\Resources\Subfleet as SubfleetResource;
@ -103,20 +104,19 @@ class UserController extends RestController
if ($request->isMethod('PUT')) {
$flight_id = $request->input('flight_id');
$flight = $this->flightRepo->find($flight_id);
$this->flightSvc->addBid($flight, $user);
$bid = $this->flightSvc->addBid($flight, $user);
return new BidResource($bid);
}
elseif ($request->isMethod('DELETE')) {
if ($request->isMethod('DELETE')) {
$flight_id = $request->input('flight_id');
$flight = $this->flightRepo->find($flight_id);
$this->flightSvc->removeBid($flight, $user);
}
# Return the flights they currently have bids on
$flights = UserBid::where(['user_id' => $user->id])
->get()->pluck('flight');
return FlightResource::collection($flights);
$bids = UserBid::where(['user_id' => $user->id])->get();
return BidResource::collection($bids);
}
/**

View File

@ -0,0 +1,16 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class Bid extends Resource
{
public function toArray($request)
{
$bid = parent::toArray($request);
$bid['flight'] = new Flight($this->flight);
return $bid;
}
}

View File

@ -235,10 +235,10 @@ class FlightTest extends TestCase
$uri = '/api/user/bids';
$data = ['flight_id' => $flight->id];
$body = $this->put($uri, $data)->json('data');
$body = $this->put($uri, $data);
$body = $body->json('data');
$this->assertCount(1, $body);
$this->assertEquals($body[0]['id'], $flight->id);
$this->assertEquals($body['flight_id'], $flight->id);
# Now try to have the second user bid on it
# Should return a 409 error