Version 1.0 #102
@@ -8,7 +8,6 @@ use App\Models\User;
|
|||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use App\Models\CoinTransaction;
|
use App\Models\CoinTransaction;
|
||||||
use App\Models\CoinTransactionType;
|
|
||||||
|
|
||||||
class StatisticsController extends Controller
|
class StatisticsController extends Controller
|
||||||
{
|
{
|
||||||
@@ -86,7 +85,6 @@ class StatisticsController extends Controller
|
|||||||
->groupBy('type')
|
->groupBy('type')
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
// 3. Games Per Month (Line/Bar Chart)
|
|
||||||
$gamesPerMonth = Game::select(
|
$gamesPerMonth = Game::select(
|
||||||
DB::raw('DATE_FORMAT(began_at, "%Y-%m") as month'),
|
DB::raw('DATE_FORMAT(began_at, "%Y-%m") as month'),
|
||||||
DB::raw('count(*) as total')
|
DB::raw('count(*) as total')
|
||||||
@@ -97,19 +95,13 @@ class StatisticsController extends Controller
|
|||||||
->limit(12)
|
->limit(12)
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
// 4. Financial/Coin Stats (Using the new Tables)
|
|
||||||
// We calculate "Revenue" based on the volume of "Coin purchase" transactions
|
|
||||||
|
|
||||||
// Calculate Total Coins Purchased (All time)
|
|
||||||
// We join with types table to filter by name "Coin purchase"
|
|
||||||
$totalCoinsPurchased = CoinTransaction::whereHas('type', function($q) {
|
$totalCoinsPurchased = CoinTransaction::whereHas('type', function($q) {
|
||||||
$q->where('name', 'Coin purchase');
|
$q->where('name', 'Coin purchase');
|
||||||
})
|
})
|
||||||
->sum('coins');
|
->sum('coins');
|
||||||
|
|
||||||
// Calculate Purchases Per Month (Time Series)
|
$purchasesPerMonth = CoinTransaction::join('coin_transaction_types', 'coin_transactions.coin_transaction_type_id', '=', 'coin_transaction_types.id')
|
||||||
// Groups by 'transaction_datetime' (YYYY-MM)
|
|
||||||
$purchasesPerMonth = \App\Models\CoinTransaction::join('coin_transaction_types', 'coin_transactions.coin_transaction_type_id', '=', 'coin_transaction_types.id')
|
|
||||||
->where('coin_transaction_types.name', 'Coin purchase')
|
->where('coin_transaction_types.name', 'Coin purchase')
|
||||||
->select(
|
->select(
|
||||||
DB::raw('DATE_FORMAT(coin_transactions.transaction_datetime, "%Y-%m") as month'),
|
DB::raw('DATE_FORMAT(coin_transactions.transaction_datetime, "%Y-%m") as month'),
|
||||||
|
|||||||
@@ -197,4 +197,32 @@ class UserController extends Controller
|
|||||||
|
|
||||||
return response()->json(['message' => 'User unblocked successfully']);
|
return response()->json(['message' => 'User unblocked successfully']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getTransactions(Request $request, User $user)
|
||||||
|
{
|
||||||
|
if ($request->user()->id !== $user->id && $request->user()->type !== 'A') {
|
||||||
|
return response()->json(['message' => 'Forbidden'], 403);
|
||||||
|
}
|
||||||
|
|
||||||
|
$query = $user->transactions()->with('type');
|
||||||
|
|
||||||
|
if ($request->has('type')) {
|
||||||
|
$query->whereHas('type', function ($q) use ($request) {
|
||||||
|
$q->where('name', $request->type);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->has('date_from')) {
|
||||||
|
$query->whereDate('transaction_datetime', '>=', $request->date_from);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->has('date_to')) {
|
||||||
|
$query->whereDate('transaction_datetime', '<=', $request->date_to);
|
||||||
|
}
|
||||||
|
|
||||||
|
$transactions = $query->orderBy('transaction_datetime', 'desc')
|
||||||
|
->paginate(10);
|
||||||
|
|
||||||
|
return response()->json($transactions);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,4 +58,9 @@ class User extends Authenticatable
|
|||||||
{
|
{
|
||||||
return $this->hasMany(Game::class, 'player1_user_id')->orWhere('player2_user_id', $this->id);
|
return $this->hasMany(Game::class, 'player1_user_id')->orWhere('player2_user_id', $this->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function transactions()
|
||||||
|
{
|
||||||
|
return $this->hasMany(CoinTransaction::class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user