2017-12-13 06:58:27 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
2019-07-16 03:51:35 +08:00
|
|
|
use App\Contracts\Controller;
|
2021-06-18 07:42:56 +08:00
|
|
|
use App\Exceptions\Unauthorized;
|
2021-04-23 22:33:13 +08:00
|
|
|
use App\Exceptions\UserNotFound;
|
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\Pirep as PirepResource;
|
|
|
|
use App\Http\Resources\Subfleet as SubfleetResource;
|
|
|
|
use App\Http\Resources\User as UserResource;
|
2018-02-28 03:25:32 +08:00
|
|
|
use App\Models\Bid;
|
2018-03-20 09:50:40 +08:00
|
|
|
use App\Models\Enums\PirepState;
|
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-02-21 12:33:09 +08:00
|
|
|
use App\Repositories\UserRepository;
|
2019-11-06 00:44:31 +08:00
|
|
|
use App\Services\BidService;
|
2018-02-23 00:44:15 +08:00
|
|
|
use App\Services\FlightService;
|
2018-01-11 08:40:01 +08:00
|
|
|
use App\Services\UserService;
|
2017-12-13 06:58:27 +08:00
|
|
|
use Illuminate\Http\Request;
|
2019-11-06 00:44:31 +08:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
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-03-20 09:50:40 +08:00
|
|
|
class UserController extends Controller
|
2017-12-13 06:58:27 +08:00
|
|
|
{
|
2019-11-06 00:44:31 +08:00
|
|
|
private $bidSvc;
|
2018-08-27 00:40:04 +08:00
|
|
|
private $flightRepo;
|
|
|
|
private $flightSvc;
|
|
|
|
private $pirepRepo;
|
|
|
|
private $userRepo;
|
|
|
|
private $userSvc;
|
2017-12-13 06:58:27 +08:00
|
|
|
|
2018-02-21 12:33:09 +08:00
|
|
|
/**
|
2019-11-06 00:44:31 +08:00
|
|
|
* @param BidService $bidSvc
|
2018-08-27 02:51:47 +08:00
|
|
|
* @param FlightRepository $flightRepo
|
|
|
|
* @param FlightService $flightSvc
|
|
|
|
* @param PirepRepository $pirepRepo
|
|
|
|
* @param UserRepository $userRepo
|
|
|
|
* @param UserService $userSvc
|
2018-02-21 12:33:09 +08:00
|
|
|
*/
|
2018-01-11 08:40:01 +08:00
|
|
|
public function __construct(
|
2019-11-06 00:44:31 +08:00
|
|
|
BidService $bidSvc,
|
2018-02-23 00:44:15 +08:00
|
|
|
FlightRepository $flightRepo,
|
|
|
|
FlightService $flightSvc,
|
2018-02-20 00:35:49 +08:00
|
|
|
PirepRepository $pirepRepo,
|
2018-01-11 08:40:01 +08:00
|
|
|
UserRepository $userRepo,
|
|
|
|
UserService $userSvc
|
|
|
|
) {
|
2019-11-06 00:44:31 +08:00
|
|
|
$this->bidSvc = $bidSvc;
|
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;
|
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
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-20 00:35:49 +08:00
|
|
|
* @return int|mixed
|
|
|
|
*/
|
|
|
|
protected function getUserId(Request $request)
|
|
|
|
{
|
2020-10-25 03:11:08 +08:00
|
|
|
$id = $request->get('id');
|
|
|
|
if ($id === null || $id === 'me') {
|
2018-02-20 00:35:49 +08:00
|
|
|
return Auth::user()->id;
|
|
|
|
}
|
|
|
|
|
2019-11-06 00:44:31 +08:00
|
|
|
return $request->get('id');
|
2018-02-20 00:35:49 +08:00
|
|
|
}
|
|
|
|
|
2017-12-13 06:58:27 +08:00
|
|
|
/**
|
|
|
|
* Return the profile for the currently auth'd user
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @return UserResource
|
2017-12-13 06:58:27 +08:00
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
2020-03-25 04:17:28 +08:00
|
|
|
return $this->get(Auth::user()->id);
|
2017-12-13 06:58:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the profile for the passed-in user
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @param $id
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @return UserResource
|
2017-12-13 06:58:27 +08:00
|
|
|
*/
|
|
|
|
public function get($id)
|
|
|
|
{
|
2020-10-01 00:58:45 +08:00
|
|
|
$user = $this->userSvc->getUser($id);
|
2021-04-23 22:33:13 +08:00
|
|
|
if ($user === null) {
|
|
|
|
throw new UserNotFound();
|
|
|
|
}
|
|
|
|
|
2020-03-25 04:17:28 +08:00
|
|
|
return new UserResource($user);
|
2017-12-13 06:58:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all of the bids for the passed-in user
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-23 00:44:15 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-28 03:14:58 +08:00
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
2019-08-21 20:17:44 +08:00
|
|
|
* @throws \App\Exceptions\BidExistsForFlight
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return mixed
|
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
|
|
|
{
|
2021-03-07 02:49:42 +08:00
|
|
|
$user_id = $this->getUserId($request);
|
|
|
|
$user = $this->userSvc->getUser($user_id);
|
2021-04-23 22:33:13 +08:00
|
|
|
if ($user === null) {
|
|
|
|
throw new UserNotFound();
|
|
|
|
}
|
2018-02-23 00:44:15 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// 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);
|
2019-11-06 00:44:31 +08:00
|
|
|
$bid = $this->bidSvc->addBid($flight, $user);
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-02-28 03:11:48 +08:00
|
|
|
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-03-20 09:50:40 +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);
|
2019-11-06 00:44:31 +08:00
|
|
|
$this->bidSvc->removeBid($flight, $user);
|
2018-02-23 00:44:15 +08:00
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Return the flights they currently have bids on
|
2019-11-06 00:44:31 +08:00
|
|
|
$bids = $this->bidSvc->findBidsForUser($user);
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-02-28 03:11:48 +08:00
|
|
|
return BidResource::collection($bids);
|
2017-12-13 06:58:27 +08:00
|
|
|
}
|
|
|
|
|
2021-06-18 07:42:56 +08:00
|
|
|
/**
|
|
|
|
* Get a particular bid for a user
|
|
|
|
*
|
|
|
|
* @param $bid_id
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
*
|
|
|
|
* @return \App\Http\Resources\Bid
|
|
|
|
*/
|
|
|
|
public function get_bid($bid_id, Request $request)
|
|
|
|
{
|
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
$user = Auth::user();
|
|
|
|
|
|
|
|
// Return the current bid
|
|
|
|
$bid = $this->bidSvc->getBid($user, $bid_id);
|
|
|
|
if ($bid->user_id !== $user->id) {
|
|
|
|
throw new Unauthorized(new \Exception('Bid not not belong to authenticated user'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return new BidResource($bid);
|
|
|
|
}
|
|
|
|
|
2018-01-11 08:40:01 +08:00
|
|
|
/**
|
|
|
|
* Return the fleet that this user is allowed to
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-11 08:40:01 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-11 08:40:01 +08:00
|
|
|
* @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));
|
2021-04-23 22:33:13 +08:00
|
|
|
if ($user === null) {
|
|
|
|
throw new UserNotFound();
|
|
|
|
}
|
|
|
|
|
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
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-20 00:35:49 +08:00
|
|
|
* @throws RepositoryException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
|
2018-02-20 00:35:49 +08:00
|
|
|
*/
|
|
|
|
public function pireps(Request $request)
|
|
|
|
{
|
|
|
|
$this->pirepRepo->pushCriteria(new RequestCriteria($request));
|
|
|
|
|
|
|
|
$where = [
|
|
|
|
'user_id' => $this->getUserId($request),
|
|
|
|
];
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if (filled($request->query('state'))) {
|
2018-02-20 00:35:49 +08:00
|
|
|
$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
|
|
|
}
|