Add api routes to get/add/remove bids for a user #172
This commit is contained in:
20
app/Exceptions/BidExists.php
Normal file
20
app/Exceptions/BidExists.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
|
||||
class BidExists extends HttpException
|
||||
{
|
||||
public function __construct(string $message = null, \Exception $previous = null, int $code = 0, array $headers = [])
|
||||
{
|
||||
parent::__construct(
|
||||
409,
|
||||
'A bid already exists for this flight',
|
||||
$previous, $headers, $code
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,11 @@ use App\Http\Resources\User as UserResource;
|
||||
use App\Models\Enums\PirepState;
|
||||
use App\Models\UserBid;
|
||||
use App\Repositories\Criteria\WhereCriteria;
|
||||
use App\Repositories\FlightRepository;
|
||||
use App\Repositories\PirepRepository;
|
||||
use App\Repositories\SubfleetRepository;
|
||||
use App\Repositories\UserRepository;
|
||||
use App\Services\FlightService;
|
||||
use App\Services\UserService;
|
||||
use Auth;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -21,24 +23,32 @@ use Prettus\Repository\Exceptions\RepositoryException;
|
||||
|
||||
class UserController extends RestController
|
||||
{
|
||||
protected $pirepRepo,
|
||||
protected $flightRepo,
|
||||
$flightSvc,
|
||||
$pirepRepo,
|
||||
$subfleetRepo,
|
||||
$userRepo,
|
||||
$userSvc;
|
||||
|
||||
/**
|
||||
* UserController constructor.
|
||||
* @param FlightRepository $flightRepo
|
||||
* @param FlightService $flightSvc
|
||||
* @param PirepRepository $pirepRepo
|
||||
* @param SubfleetRepository $subfleetRepo
|
||||
* @param UserRepository $userRepo
|
||||
* @param UserService $userSvc
|
||||
*/
|
||||
public function __construct(
|
||||
FlightRepository $flightRepo,
|
||||
FlightService $flightSvc,
|
||||
PirepRepository $pirepRepo,
|
||||
SubfleetRepository $subfleetRepo,
|
||||
UserRepository $userRepo,
|
||||
UserService $userSvc
|
||||
) {
|
||||
$this->flightRepo = $flightRepo;
|
||||
$this->flightSvc = $flightSvc;
|
||||
$this->pirepRepo = $pirepRepo;
|
||||
$this->subfleetRepo = $subfleetRepo;
|
||||
$this->userRepo = $userRepo;
|
||||
@@ -80,13 +90,31 @@ class UserController extends RestController
|
||||
|
||||
/**
|
||||
* Return all of the bids for the passed-in user
|
||||
* @param $id
|
||||
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
|
||||
* @param Request $request
|
||||
* @return mixed
|
||||
* @throws \App\Exceptions\BidExists
|
||||
* @throws \App\Services\Exception
|
||||
*/
|
||||
public function bids($id)
|
||||
public function bids(Request $request)
|
||||
{
|
||||
$flights = UserBid::where(['user_id' => $id])->get()
|
||||
->pluck('flight');
|
||||
$user = $this->userRepo->find($this->getUserId($request));
|
||||
|
||||
# Add a bid
|
||||
if ($request->isMethod('PUT')) {
|
||||
$flight_id = $request->input('flight_id');
|
||||
$flight = $this->flightRepo->find($flight_id);
|
||||
$this->flightSvc->addBid($flight, $user);
|
||||
}
|
||||
|
||||
elseif ($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);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ use App\Repositories\AirlineRepository;
|
||||
use App\Repositories\AirportRepository;
|
||||
use App\Repositories\Criteria\WhereCriteria;
|
||||
use App\Repositories\FlightRepository;
|
||||
use App\Services\FlightService;
|
||||
use App\Services\GeoService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
@@ -26,17 +27,20 @@ class FlightController extends Controller
|
||||
* @param AirlineRepository $airlineRepo
|
||||
* @param AirportRepository $airportRepo
|
||||
* @param FlightRepository $flightRepo
|
||||
* @param FlightService $flightSvc
|
||||
* @param GeoService $geoSvc
|
||||
*/
|
||||
public function __construct(
|
||||
AirlineRepository $airlineRepo,
|
||||
AirportRepository $airportRepo,
|
||||
FlightRepository $flightRepo,
|
||||
FlightService $flightSvc,
|
||||
GeoService $geoSvc
|
||||
) {
|
||||
$this->airlineRepo = $airlineRepo;
|
||||
$this->airportRepo = $airportRepo;
|
||||
$this->flightRepo = $flightRepo;
|
||||
$this->flightSvc = $flightSvc;
|
||||
$this->geoSvc = $geoSvc;
|
||||
}
|
||||
|
||||
|
||||
@@ -65,9 +65,15 @@ Route::group(['middleware' => ['api.auth']], function ()
|
||||
Route::get('user/fleet', 'UserController@fleet');
|
||||
Route::get('user/pireps', 'UserController@pireps');
|
||||
|
||||
Route::get('user/bids', 'UserController@bids');
|
||||
Route::put('user/bids', 'UserController@bids');
|
||||
Route::delete('user/bids', 'UserController@bids');
|
||||
|
||||
Route::get('users/{id}', 'UserController@get');
|
||||
Route::get('users/{id}/bids', 'UserController@bids');
|
||||
Route::get('users/{id}/fleet', 'UserController@fleet');
|
||||
Route::get('users/{id}/pireps', 'UserController@pireps');
|
||||
|
||||
Route::get('users/{id}/bids', 'UserController@bids');
|
||||
Route::put('users/{id}/bids', 'UserController@bids');
|
||||
|
||||
});
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Exceptions\BidExists;
|
||||
use App\Models\Flight;
|
||||
use App\Models\User;
|
||||
use App\Models\UserBid;
|
||||
@@ -129,13 +130,14 @@ class FlightService extends BaseService
|
||||
* @param Flight $flight
|
||||
* @param User $user
|
||||
* @return UserBid|null
|
||||
* @throws \App\Exceptions\BidExists
|
||||
*/
|
||||
public function addBid(Flight $flight, User $user)
|
||||
{
|
||||
# If it's already been bid on, then it can't be bid on again
|
||||
if($flight->has_bid && setting('bids.disable_flight_on_bid')) {
|
||||
Log::info($flight->id . ' already has a bid, skipping');
|
||||
return null;
|
||||
throw new BidExists();
|
||||
}
|
||||
|
||||
# See if we're allowed to have multiple bids or not
|
||||
@@ -170,7 +172,6 @@ class FlightService extends BaseService
|
||||
* Remove a bid from a given flight
|
||||
* @param Flight $flight
|
||||
* @param User $user
|
||||
* @throws Exception
|
||||
*/
|
||||
public function removeBid(Flight $flight, User $user)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user