using MiaoJiZhang.Infrastructure.Persistence; using Microsoft.EntityFrameworkCore; namespace MiaoJiZhang.Api.Services; public class AccountDataEraser(AppDbContext db) { public async Task EraseAsync(long userId, CancellationToken ct = default) { await using var transaction = await db.Database.BeginTransactionAsync(ct); await db.PushMessages .Where(message => message.TargetUserId == userId) .ExecuteDeleteAsync(ct); await db.PushDevices .Where(device => device.UserId == userId) .ExecuteDeleteAsync(ct); await db.UserPushPreferences .Where(preference => preference.UserId == userId) .ExecuteDeleteAsync(ct); await db.BudgetNotificationReceipts .Where(receipt => receipt.UserId == userId) .ExecuteDeleteAsync(ct); await db.ChatMessages .Where(message => message.UserId == userId) .ExecuteDeleteAsync(ct); await db.Transactions .IgnoreQueryFilters() .Where(item => item.UserId == userId) .ExecuteDeleteAsync(ct); await db.Budgets .Where(item => item.UserId == userId) .ExecuteDeleteAsync(ct); await db.Categories .Where(item => item.UserId == userId) .ExecuteDeleteAsync(ct); await db.AiCompanionSettings .Where(item => item.UserId == userId) .ExecuteDeleteAsync(ct); await db.Ledgers .Where(item => item.OwnerId == userId) .ExecuteDeleteAsync(ct); await db.Users .Where(item => item.Id == userId) .ExecuteDeleteAsync(ct); await transaction.CommitAsync(ct); } }