Skip to content

Commit

Permalink
Create StatsController.php
Browse files Browse the repository at this point in the history
  • Loading branch information
rcomunica authored Dec 1, 2024
1 parent 5c25738 commit fe6e89c
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions app/Http/Controllers/Api/StatsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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);
}
}

0 comments on commit fe6e89c

Please sign in to comment.