2017-12-13 06:58:27 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
2018-02-28 03:11:48 +08:00
|
|
|
use App\Http\Resources\Bid as BidResource;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Http\Resources\Flight as FlightResource;
|
|
|
|
use App\Http\Resources\Pirep as PirepResource;
|
|
|
|
use App\Http\Resources\Subfleet as SubfleetResource;
|
|
|
|
use App\Http\Resources\User as UserResource;
|
2018-02-20 00:35:49 +08:00
|
|
|
use App\Models\Enums\PirepState;
|
2018-02-28 03:25:32 +08:00
|
|
|
use App\Models\Bid;
|
2018-02-20 00:35:49 +08:00
|
|
|
use App\Repositories\Criteria\WhereCriteria;
|
2018-02-23 00:44:15 +08:00
|
|
|
use App\Repositories\FlightRepository;
|
2018-02-20 00:35:49 +08:00
|
|
|
use App\Repositories\PirepRepository;
|
2018-01-20 01:46:30 +08:00
|
|
|
use App\Repositories\SubfleetRepository;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Repositories\UserRepository;
|
2018-02-23 00:44:15 +08:00
|
|
|
use App\Services\FlightService;
|
2018-01-11 08:40:01 +08:00
|
|
|
use App\Services\UserService;
|
2018-01-03 04:37:52 +08:00
|
|
|
use Auth;
|
2017-12-13 06:58:27 +08:00
|
|
|
use Illuminate\Http\Request;
|
2018-02-20 00:35:49 +08:00
|
|
|
use Prettus\Repository\Criteria\RequestCriteria;
|
|
|
|
use Prettus\Repository\Exceptions\RepositoryException;
|
2017-12-13 06:58:27 +08:00
|
|
|
|
|
|
|
|
2018-01-06 00:45:34 +08:00
|
|
|
class UserController extends RestController
|
2017-12-13 06:58:27 +08:00
|
|
|
{
|
2018-02-23 00:44:15 +08:00
|
|
|
protected $flightRepo,
|
|
|
|
$flightSvc,
|
|
|
|
$pirepRepo,
|
2018-02-20 00:35:49 +08:00
|
|
|
$subfleetRepo,
|
2018-01-20 01:46:30 +08:00
|
|
|
$userRepo,
|
|
|
|
$userSvc;
|
2017-12-13 06:58:27 +08:00
|
|
|
|
2018-02-21 12:33:09 +08:00
|
|
|
/**
|
|
|
|
* UserController constructor.
|
2018-02-23 00:44:15 +08:00
|
|
|
* @param FlightRepository $flightRepo
|
|
|
|
* @param FlightService $flightSvc
|
2018-02-21 12:33:09 +08:00
|
|
|
* @param PirepRepository $pirepRepo
|
|
|
|
* @param SubfleetRepository $subfleetRepo
|
|
|
|
* @param UserRepository $userRepo
|
|
|
|
* @param UserService $userSvc
|
|
|
|
*/
|
2018-01-11 08:40:01 +08:00
|
|
|
public function __construct(
|
2018-02-23 00:44:15 +08:00
|
|
|
FlightRepository $flightRepo,
|
|
|
|
FlightService $flightSvc,
|
2018-02-20 00:35:49 +08:00
|
|
|
PirepRepository $pirepRepo,
|
2018-01-20 01:46:30 +08:00
|
|
|
SubfleetRepository $subfleetRepo,
|
2018-01-11 08:40:01 +08:00
|
|
|
UserRepository $userRepo,
|
|
|
|
UserService $userSvc
|
|
|
|
) {
|
2018-02-23 00:44:15 +08:00
|
|
|
$this->flightRepo = $flightRepo;
|
|
|
|
$this->flightSvc = $flightSvc;
|
2018-02-20 00:35:49 +08:00
|
|
|
$this->pirepRepo = $pirepRepo;
|
2018-01-20 01:46:30 +08:00
|
|
|
$this->subfleetRepo = $subfleetRepo;
|
2017-12-13 06:58:27 +08:00
|
|
|
$this->userRepo = $userRepo;
|
2018-01-11 08:40:01 +08:00
|
|
|
$this->userSvc = $userSvc;
|
2017-12-13 06:58:27 +08:00
|
|
|
}
|
|
|
|
|
2018-02-20 00:35:49 +08:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @return int|mixed
|
|
|
|
*/
|
|
|
|
protected function getUserId(Request $request)
|
|
|
|
{
|
|
|
|
if ($request->id === null) {
|
|
|
|
return Auth::user()->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $request->id;
|
|
|
|
}
|
|
|
|
|
2017-12-13 06:58:27 +08:00
|
|
|
/**
|
|
|
|
* Return the profile for the currently auth'd user
|
2018-02-21 12:33:09 +08:00
|
|
|
* @param Request $request
|
|
|
|
* @return UserResource
|
2017-12-13 06:58:27 +08:00
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
2018-01-03 04:37:52 +08:00
|
|
|
return new UserResource(Auth::user());
|
2017-12-13 06:58:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the profile for the passed-in user
|
2018-02-21 12:33:09 +08:00
|
|
|
* @param $id
|
|
|
|
* @return UserResource
|
2017-12-13 06:58:27 +08:00
|
|
|
*/
|
|
|
|
public function get($id)
|
|
|
|
{
|
|
|
|
return new UserResource($this->userRepo->find($id));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all of the bids for the passed-in user
|
2018-02-23 00:44:15 +08:00
|
|
|
* @param Request $request
|
|
|
|
* @return mixed
|
2018-02-28 03:14:58 +08:00
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
2018-02-23 00:44:15 +08:00
|
|
|
* @throws \App\Exceptions\BidExists
|
|
|
|
* @throws \App\Services\Exception
|
2017-12-13 06:58:27 +08:00
|
|
|
*/
|
2018-02-23 00:44:15 +08:00
|
|
|
public function bids(Request $request)
|
2017-12-13 06:58:27 +08:00
|
|
|
{
|
2018-02-23 00:44:15 +08:00
|
|
|
$user = $this->userRepo->find($this->getUserId($request));
|
|
|
|
|
|
|
|
# Add a bid
|
2018-03-13 14:26:27 +08:00
|
|
|
if ($request->isMethod('PUT') || $request->isMethod('POST')) {
|
2018-02-23 00:44:15 +08:00
|
|
|
$flight_id = $request->input('flight_id');
|
|
|
|
$flight = $this->flightRepo->find($flight_id);
|
2018-02-28 03:11:48 +08:00
|
|
|
$bid = $this->flightSvc->addBid($flight, $user);
|
|
|
|
return new BidResource($bid);
|
2018-02-23 00:44:15 +08:00
|
|
|
}
|
|
|
|
|
2018-02-28 03:11:48 +08:00
|
|
|
if ($request->isMethod('DELETE')) {
|
2018-02-28 03:14:58 +08:00
|
|
|
if($request->filled('bid_id')) {
|
2018-02-28 03:25:32 +08:00
|
|
|
$bid = Bid::findOrFail($request->input('bid_id'));
|
2018-02-28 03:14:58 +08:00
|
|
|
$flight_id = $bid->flight_id;
|
|
|
|
} else {
|
|
|
|
$flight_id = $request->input('flight_id');
|
|
|
|
}
|
|
|
|
|
2018-02-23 00:44:15 +08:00
|
|
|
$flight = $this->flightRepo->find($flight_id);
|
|
|
|
$this->flightSvc->removeBid($flight, $user);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return the flights they currently have bids on
|
2018-02-28 03:25:32 +08:00
|
|
|
$bids = Bid::where(['user_id' => $user->id])->get();
|
2018-02-28 03:11:48 +08:00
|
|
|
return BidResource::collection($bids);
|
2017-12-13 06:58:27 +08:00
|
|
|
}
|
|
|
|
|
2018-01-11 08:40:01 +08:00
|
|
|
/**
|
|
|
|
* Return the fleet that this user is allowed to
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
|
|
|
|
*/
|
|
|
|
public function fleet(Request $request)
|
|
|
|
{
|
2018-02-20 00:35:49 +08:00
|
|
|
$user = $this->userRepo->find($this->getUserId($request));
|
2018-01-11 08:40:01 +08:00
|
|
|
$subfleets = $this->userSvc->getAllowableSubfleets($user);
|
|
|
|
|
|
|
|
return SubfleetResource::collection($subfleets);
|
|
|
|
}
|
|
|
|
|
2018-02-20 00:35:49 +08:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
|
|
|
|
* @throws RepositoryException
|
|
|
|
*/
|
|
|
|
public function pireps(Request $request)
|
|
|
|
{
|
|
|
|
$this->pirepRepo->pushCriteria(new RequestCriteria($request));
|
|
|
|
|
|
|
|
$where = [
|
|
|
|
'user_id' => $this->getUserId($request),
|
|
|
|
];
|
|
|
|
|
|
|
|
if(filled($request->query('state'))) {
|
|
|
|
$where['state'] = $request->query('state');
|
|
|
|
} else {
|
|
|
|
$where[] = ['state', '!=', PirepState::CANCELLED];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->pirepRepo->pushCriteria(new WhereCriteria($request, $where));
|
|
|
|
|
|
|
|
$pireps = $this->pirepRepo
|
|
|
|
->orderBy('created_at', 'desc')
|
|
|
|
->paginate();
|
|
|
|
|
|
|
|
return PirepResource::collection($pireps);
|
|
|
|
}
|
2017-12-13 06:58:27 +08:00
|
|
|
}
|