♻️ Splitted wallet service

This commit is contained in:
2026-02-03 02:18:02 +08:00
parent bb9105c78c
commit 9a1f36ee26
43 changed files with 623 additions and 590 deletions

View File

@@ -0,0 +1,50 @@
using DysonNetwork.Shared.Models;
using Microsoft.EntityFrameworkCore;
namespace DysonNetwork.Wallet.Payment;
public class WalletService(AppDatabase db)
{
public async Task<SnWallet?> GetWalletAsync(Guid accountId)
{
return await db.Wallets
.Include(w => w.Pockets)
.FirstOrDefaultAsync(w => w.AccountId == accountId);
}
public async Task<SnWallet> CreateWalletAsync(Guid accountId)
{
var existingWallet = await db.Wallets.FirstOrDefaultAsync(w => w.AccountId == accountId);
if (existingWallet != null)
{
throw new InvalidOperationException($"Wallet already exists for account {accountId}");
}
var wallet = new SnWallet { AccountId = accountId };
db.Wallets.Add(wallet);
await db.SaveChangesAsync();
return wallet;
}
public async Task<(SnWalletPocket wallet, bool isNewlyCreated)> GetOrCreateWalletPocketAsync(
Guid walletId,
string currency,
decimal? initialAmount = null
)
{
var pocket = await db.WalletPockets.FirstOrDefaultAsync(p => p.Currency == currency && p.WalletId == walletId);
if (pocket != null) return (pocket, false);
pocket = new SnWalletPocket
{
Currency = currency,
Amount = initialAmount ?? 0,
WalletId = walletId
};
db.WalletPockets.Add(pocket);
return (pocket, true);
}
}