-
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
Open
rcomunica
wants to merge
25
commits into
nabeelio:dev
Choose a base branch
from
rcomunica:dev
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 16 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
e0eda0a
Create Stats.php
rcomunica 62b454a
Update User.php
rcomunica 3f3c059
Update Stats.php
rcomunica e430c1b
Update Stats.php
rcomunica bcd84e9
Update User.php
rcomunica b586caa
Update Stats.php
rcomunica 47047da
Update Stats.php
rcomunica da0720f
Update Stats.php
rcomunica 6624700
Update Stats.php
rcomunica 79dfe58
Update User.php
rcomunica 877fe53
Create StatsController.php
rcomunica fa7aa69
Update Stats.php
rcomunica de45a73
Update StatsController.php
rcomunica a898903
Update User.php
rcomunica 1c23688
Update StatsController.php
rcomunica 5c25738
Update Stats.php
rcomunica fe6e89c
Create StatsController.php
rcomunica 399c69c
Delete app/Http/Resources/StatsController.php
rcomunica 8bff08f
Update RouteServiceProvider.php
rcomunica cea6ee0
FIX CI
rcomunica 0b49bfd
Update User.php
rcomunica a4fe752
FIX CI 1/2
rcomunica 6a8caa9
FIX CI 2/2
rcomunica dc0f8e8
Update StatsController.php
rcomunica a1c47e9
Merge branch 'dev' into dev
rcomunica File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
'average_landing' => number_format($this->average_landing_rate), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 😉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.
Something like below should work