Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stats in User resource API #1900

Open
wants to merge 25 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions app/Http/Controllers/Api/StatsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Http\Controllers\Api;

use App\Contracts\Controller;
use App\Http\Resources\Stats;
use App\Models\Enums\PirepState;
use App\Models\Pirep;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class StatsController extends Controller
{
public function index(Request $request)
{
$where = [];
$where['user_id'] = Auth::id();
$where['state'] = PirepState::ACCEPTED;
$avgStats = ['flight_time', 'landing_rate', 'fuel_used', 'score'];
$response = [];

$response['flights'] = Pirep::where($where)->count();
$response['flight_time'] = Pirep::where($where)->count();

foreach ($avgStats as $static) {
$response['average_'.$static] = Pirep::where($where)->avg($static);
}

$response['balance'] = Auth::user()->journal->balance->getValue() ?? 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't want to create extra queries, you should eager load the journal relationship here.

Also no need to call the Auth facade twice, you can get the user once with relationships and use wherever needed 😉

return new Stats((object) $response);
}
}
24 changes: 24 additions & 0 deletions app/Http/Resources/Stats.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Http\Resources;

use App\Contracts\Resource;

/**
* @mixin \App\Models\Stats
*/
class Stats extends Resource
{
public function toArray($request)
{
return [
'flights' => $this->flights,
'total_time' => $this->flight_time,
'average_time' => $this->average_flight_time,
'average_score' => number_format($this->average_score),
'balance' => $this->balance,
'average_fuel' => number_format($this->average_fuel_used / 2.20462262185).' Kg',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think that this is correct. It should be casted as Fuel and then we can get both lbs and kg results.

Forcing something to kg is not logical 😉

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like below should work

use App\Support\Units\Fuel

'average_fuel' => Fuel::make($this->average_fuel_used, config('phpvms.internal_units.fuel'))->getResponseUnits();

'average_landing' => number_format($this->average_landing_rate),
];
}
}
1 change: 1 addition & 0 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ private function mapApiRoutes()

// This is the info of the user whose token is in use
Route::get('user', 'UserController@index');
Route::get('user/stats', 'StatsController@index');
Route::get('user/fleet', 'UserController@fleet');
Route::get('user/pireps', 'UserController@pireps');

Expand Down
Loading