Remove empty fields from user return, add avatar

This commit is contained in:
Nabeel Shahzad
2020-03-24 15:53:59 -04:00
parent 877d5a5479
commit 3f7a7d0f2e
3 changed files with 23 additions and 10 deletions

View File

@@ -3,32 +3,36 @@
namespace App\Http\Resources;
use App\Contracts\Resource;
use App\Http\Resources\Flight as FlightResource;
/**
* @mixin \App\Models\User
*/
class User extends Resource
{
public function toArray($request)
{
return [
$res = [
'id' => $this->id,
'pilot_id' => $this->pilot_id,
'avatar' => $this->avatar,
'ident' => $this->ident,
'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,
'status' => $this->status,
'state' => $this->state,
'airline' => Airline::make($this->airline),
'bids' => UserBid::collection($this->bids),
'rank' => Rank::make($this->rank),
'rank' => Rank::make($this->rank),
];
$res['airline'] = Airline::make($this->airline);
$res['bids'] = UserBid::collection($this->whenLoaded('bids'));
$res['flights'] = new FlightResource($this->whenLoaded('flights'));
return $res;
}
}

View File

@@ -21,6 +21,7 @@ use Laratrust\Traits\LaratrustUserTrait;
* @property string ident
* @property string curr_airport_id
* @property string home_airport_id
* @property string avatar
* @property Airline airline
* @property Flight[] flights
* @property int flight_time
@@ -142,7 +143,7 @@ class User extends Authenticatable
public function getAvatarAttribute()
{
if (!$this->attributes['avatar']) {
return;
return $this->gravatar();
}
return new File([

View File

@@ -263,4 +263,12 @@ class ApiTest extends TestCase
$res = $this->get('/api/settings')->assertStatus(200);
$settings = $res->json();
}
public function testGetUser()
{
$this->user = factory(App\Models\User::class)->create();
$res = $this->get('/api/user')->assertStatus(200);
$user = $res->json('data');
$this->assertNotNull($user);
}
}