90 lines
2.8 KiB
PHP
90 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Inertia\Inertia;
|
|
use App\Models\GameBet;
|
|
|
|
class WalletController extends Controller
|
|
{
|
|
/**
|
|
* Wallet overview page — now fully local (no external API).
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$user = Auth::user();
|
|
abort_unless($user, 403);
|
|
|
|
// Synchronize balance if possible from external API during page load for better initial state
|
|
// (Optional, maybe already done in Proxy if user just came from a game)
|
|
|
|
// Frontend expects:
|
|
// - wallets: array of currency accounts (we return an empty list for now; extend later if needed)
|
|
// - btxBalance: main BTX balance as number/string
|
|
$wallets = [];
|
|
$btxBalance = (string) ($user->balance ?? '0');
|
|
|
|
return Inertia::render('Wallet', [
|
|
'wallets' => $wallets,
|
|
'btxBalance' => $btxBalance,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* GET /api/wallet/balance — return current authenticated user balance
|
|
*/
|
|
public function balance(Request $request)
|
|
{
|
|
$user = Auth::user();
|
|
if (!$user) return response()->json(['error' => 'unauthorized'], 401);
|
|
|
|
return response()->json([
|
|
'balance' => (string) $user->balance,
|
|
'btx_balance' => (string) $user->balance,
|
|
// Add other currencies if user has multiple wallets
|
|
'wallets' => $user->wallets()->get()->map(fn($w) => [
|
|
'currency' => $w->currency,
|
|
'balance' => (string) $w->balance,
|
|
]),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* GET /wallet/bets — fetch user's game bets
|
|
*/
|
|
public function bets(Request $request)
|
|
{
|
|
$user = Auth::user();
|
|
abort_unless($user, 403);
|
|
|
|
$query = GameBet::where('user_id', $user->id);
|
|
|
|
if ($request->filled('search')) {
|
|
$query->where('game_name', 'like', '%' . $request->input('search') . '%');
|
|
}
|
|
|
|
$sort = $request->input('sort', 'created_at');
|
|
$order = $request->input('order', 'desc');
|
|
|
|
if (in_array($sort, ['created_at', 'wager_amount', 'payout_amount', 'payout_multiplier', 'game_name'])) {
|
|
$query->orderBy($sort, $order === 'asc' ? 'asc' : 'desc');
|
|
}
|
|
|
|
$bets = $query->paginate(20)->through(function ($bet) {
|
|
return [
|
|
'id' => $bet->id,
|
|
'game_name' => $bet->game_name,
|
|
'wager_amount' => (string) $bet->wager_amount,
|
|
'payout_multiplier' => (string) $bet->payout_multiplier,
|
|
'payout_amount' => (string) $bet->payout_amount,
|
|
'currency' => $bet->currency,
|
|
'created_at' => $bet->created_at->toIso8601String(),
|
|
];
|
|
});
|
|
|
|
return response()->json($bets);
|
|
}
|
|
}
|