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 16 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
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),
];
}
}
35 changes: 35 additions & 0 deletions app/Http/Resources/StatsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Http\Controllers\Api;

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

class StatsController extends Controller
{
public function index(Request $request)
{
$table = 'pireps';
$where = [];
$where['user_id'] = Auth::user()->id;
rcomunica marked this conversation as resolved.
Show resolved Hide resolved
$where['state'] = PirepState::ACCEPTED;
$avgStats = ['flight_time', 'landing_rate', 'fuel_used', 'score'];
$response = [];

$response['flights'] = DB::table($table)->selectRaw('count(id) as count')->where($where)->value('count');
rcomunica marked this conversation as resolved.
Show resolved Hide resolved
$response['flight_time'] = DB::table($table)->selectRaw('sum(flight_time) as count')->where($where)->value('count');
rcomunica marked this conversation as resolved.
Show resolved Hide resolved

foreach ($avgStats as $static) {
$response['average_'.$static] = DB::table($table)->selectRaw('avg('.$static.') as static')
->where($where)
->value('static');
rcomunica marked this conversation as resolved.
Show resolved Hide resolved
}

$response['balance'] = Auth::user()->journal->balance->getValue() ?? 0;
return new Stats((object) $response);
}
}
1 change: 1 addition & 0 deletions app/Http/Resources/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function toArray($request)
'total_time' => $this->flight_time,
'timezone' => $this->timezone,
'state' => $this->state,

];

$res['airline'] = Airline::make($this->whenLoaded('airline'));
Expand Down
Loading