Add more API resources; user bid management and tests #35 #36

This commit is contained in:
Nabeel Shahzad
2017-12-12 16:58:27 -06:00
parent 5b25a464ba
commit 248a8d1488
27 changed files with 470 additions and 166 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Http\Controllers\Api;
use Log;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
@@ -25,6 +26,10 @@ class FlightController extends AppBaseController
return new FlightResource($flight);
}
/**
* @param Request $request
* @return mixed
*/
public function search(Request $request)
{
try {

View File

@@ -0,0 +1,55 @@
<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use App\Repositories\UserRepository;
use App\Models\UserBid;
use App\Http\Resources\Flight as FlightResource;
use App\Http\Resources\User as UserResource;
use App\Http\Resources\UserBid as UserBidResource;
class UserController extends AppBaseController
{
protected $userRepo;
public function __construct(UserRepository $userRepo)
{
$this->userRepo = $userRepo;
}
/**
* Return the profile for the currently auth'd user
*/
public function index(Request $request)
{
UserResource::withoutWrapping();
return new UserResource($request->user);
}
/**
* Get the profile for the passed-in user
*/
public function get($id)
{
UserResource::withoutWrapping();
return new UserResource($this->userRepo->find($id));
}
/**
* Return all of the bids for the passed-in user
*/
public function bids($id)
{
$flights = UserBid::where(['user_id' => $id])->get()
->pluck('flight');
return FlightResource::collection($flights);
}
}

View File

@@ -6,7 +6,6 @@
namespace App\Http\Middleware;
use Auth;
use Cache;
use Closure;
use App\Models\User;
@@ -28,16 +27,8 @@ class ApiAuth
// Try to find the user via API key. Cache this lookup
$api_key = $request->header('Authorization');
$user = User::where('apikey', $api_key)->first();
/*$user = Cache::remember(
config('cache.keys.USER_API_KEY.key') . $api_key,
config('cache.keys.USER_API_KEY.time'),
function () use ($api_key) {
return User::where('apikey', $api_key)->first();
}
);*/
if(!$user) {
$user = User::where('api_key', $api_key)->first();
if($user === null) {
return $this->unauthorized();
}

View File

@@ -0,0 +1,13 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class Aircraft extends Resource
{
public function toArray($request)
{
return parent::toArray($request);
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class Airline extends Resource
{
public function toArray($request)
{
return [
'id' => $this->id,
'icao' => $this->icao,
'iata' => $this->iata,
'name' => $this->name,
'country' => $this->country,
'logo' => $this->logo,
#'active' => $this->active,
#'created_at' => $this->created_at,
#'updated_at' => $this->updated_at,
];
}
}

View File

@@ -6,12 +6,6 @@ use Illuminate\Http\Resources\Json\Resource;
class Airport extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return parent::toArray($request);

View File

@@ -6,12 +6,6 @@ use Illuminate\Http\Resources\Json\Resource;
class Flight extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
@@ -29,8 +23,10 @@ class Flight extends Resource
'flight_time' => $this->flight_time,
'notes' => $this->notes,
'active' => $this->active,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'subfleet' => Subfleet::collection($this->subfleets),
#'created_at' => $this->created_at,
#'updated_at' => $this->updated_at,
];
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class Rank extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'name' => $this->name,
#'auto_approve_acars' => $this->auto_approve_acars,
#'auto_approve_manual' => $this->auto_approve_manual,
#'auto_promote' => $this->auto_promote,
#'created_at' => $this->created_at,
#'updated_at' => $this->updated_at,
];
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class Subfleet extends Resource
{
public function toArray($request)
{
return [
'id' => $this->id,
'airline_id' => $this->airline_id,
'name' => $this->name,
'type' => $this->type,
'fuel_type' => $this->fuel_type,
'cargo_capacity' => $this->cargo_capacity,
'fuel_capacity' => $this->fuel_capacity,
'gross_weight' => $this->gross_weight,
'aircraft' => Aircraft::collection($this->aircraft),
];
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class User extends Resource
{
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
'apikey' => $this->apikey,
'rank_id' => $this->rank_id,
'home_airport' => $this->home_airport_id,
'curr_airport' => $this->curr_airport_id,
'last_pirep_id' => $this->last_pirep_id,
'flights' => $this->flight,
'flight_time' => $this->flight_time,
'balance' => $this->balance,
'timezone' => $this->timezone,
'active' => $this->active,
'airline' => Airline::make($this->airline),
'bids' => UserBid::collection($this->bids),
'rank' => Rank::make($this->rank),
];
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class UserBid extends Resource
{
public function toArray($request)
{
return [
'id' => $this->id,
'user_id' => $this->user_id,
'flight_id' => $this->flight_id,
'flight' => Flight::collection($this->whenLoaded('flight'))
#'created_at' => $this->created_at,
#'updated_at' => $this->updated_at,
];
}
}