-
Notifications
You must be signed in to change notification settings - Fork 144
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
base: dev
Are you sure you want to change the base?
Changes from all commits
e0eda0a
62b454a
3f3c059
e430c1b
bcd84e9
b586caa
47047da
da0720f
6624700
79dfe58
877fe53
fa7aa69
de45a73
a898903
1c23688
5c25738
fe6e89c
399c69c
8bff08f
cea6ee0
0b49bfd
a4fe752
6a8caa9
dc0f8e8
a1c47e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
return new Stats((object) $response); | ||
} | ||
} |
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', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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), | ||
]; | ||
} | ||
} |
There was a problem hiding this comment.
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 😉