public async Task SaveAccountClanInfoAsync(long accountId, AccountClanInfo accountClanInfo) { var existingClanInfo = await _dbContext.AccountClanInfo .FirstOrDefaultAsync(c => c.AccountId == accountId); if (accountClanInfo == null || accountClanInfo.ClanId == 0) { // Account not in clan - delete clan info from DB if (existingClanInfo != null) { _dbContext.Entry(existingClanInfo).State = EntityState.Deleted; await _dbContext.SaveChangesAsync(); } return; } if (existingClanInfo == null) { // Insert new clan info await _dbContext.AccountClanInfo.AddAsync(accountClanInfo); await _dbContext.SaveChangesAsync(); return; } // Update clan info _dbContext.Entry(existingClanInfo).State = EntityState.Detached; accountClanInfo.AccountClanInfoId = existingClanInfo.AccountClanInfoId; _dbContext.AccountClanInfo.Attach(accountClanInfo); _dbContext.Entry(accountClanInfo).State = EntityState.Modified; await _dbContext.SaveChangesAsync(); }
public async Task SaveAccountClanInfoTest( long accountId, long dbClanId, long wgClanId, string dbPlayerRole, string wgPlayerRole) { var accountDataAccessor = _container.Resolve <IAccountDataAccessor>(); if (dbClanId > 0) { var dbAccountClanInfo = new AccountClanInfo { AccountId = accountId, ClanId = dbClanId, PlayerRole = dbPlayerRole }; await _dbContext.AccountClanInfo.AddAsync(dbAccountClanInfo); await _dbContext.SaveChangesAsync(); _dbContext.Entry(dbAccountClanInfo).State = EntityState.Detached; } var wgAccountClanInfo = new AccountClanInfo { AccountId = accountId, ClanId = wgClanId, PlayerRole = wgPlayerRole }; await accountDataAccessor.SaveAccountClanInfoAsync(accountId, wgAccountClanInfo); var savedClanInfo = await _dbContext.AccountClanInfo .SingleOrDefaultAsync(c => c.AccountId == accountId); if (wgClanId > 0) { Assert.NotNull(savedClanInfo); Assert.Equal(wgClanId, savedClanInfo.ClanId); Assert.Equal(wgPlayerRole, savedClanInfo.PlayerRole); } else { Assert.Null(savedClanInfo); } }
public static async Task MergeLanguages(this BlitzStaticianDbContext dbContext, List <DictionaryLanguage> languages) { var langIds = languages.Select(l => l.LanguageId); var existing = await dbContext.DictionaryLanguage .Where(l => langIds.Contains(l.LanguageId)) .Select(l => l.LanguageId) .AsNoTracking() .ToListAsync(); languages.ForEach(l => { dbContext.DictionaryLanguage.Attach(l); dbContext.Entry(l).State = existing.Contains(l.LanguageId) ? EntityState.Modified : EntityState.Added; }); }
public static async Task MergeNations(this BlitzStaticianDbContext dbContext, List <DictionaryNations> nations) { var nationIds = nations.Select(l => l.NationId); var existing = await dbContext.DictionaryNation .Where(l => nationIds.Contains(l.NationId)) .Select(l => l.NationId) .AsNoTracking() .ToListAsync(); nations.ForEach(l => { dbContext.DictionaryNation.Attach(l); dbContext.Entry(l).State = existing.Contains(l.NationId) ? EntityState.Modified : EntityState.Added; }); }
public static async Task MergeVehicles(this BlitzStaticianDbContext dbContext, List <VehicleEncyclopedia> vehicles) { var ids = vehicles.Select(l => l.TankId); var existing = await dbContext.VehicleEncyclopedia .Where(l => ids.Contains(l.TankId)) .Select(l => l.TankId) .ToListAsync(); vehicles.ForEach(v => { dbContext.VehicleEncyclopedia.Attach(v); dbContext.Entry(v).State = existing.Contains(v.TankId) ? EntityState.Modified : EntityState.Added; }); }
public static async Task MergeVehicleType(this BlitzStaticianDbContext dbContext, List <DictionaryVehicleType> vehicleTypes) { var ids = vehicleTypes.Select(l => l.VehicleTypeId); var existing = await dbContext.DictionaryVehicleType .Where(l => ids.Contains(l.VehicleTypeId)) .Select(l => l.VehicleTypeId) .AsNoTracking() .ToListAsync(); vehicleTypes.ForEach(l => { dbContext.DictionaryVehicleType.Attach(l); dbContext.Entry(l).State = existing.Contains(l.VehicleTypeId) ? EntityState.Modified : EntityState.Added; }); }
public static async Task MergeAchievementSection(this BlitzStaticianDbContext dbContext, List <AchievementSection> achievementSections) { var ids = achievementSections.Select(l => l.Section); var existing = await dbContext.AchievementSection .Where(l => ids.Contains(l.Section)) .Select(l => l.Section) .AsNoTracking() .ToListAsync(); achievementSections.ForEach(l => { dbContext.AchievementSection.Attach(l); dbContext.Entry(l).State = existing.Contains(l.Section) ? EntityState.Modified : EntityState.Added; }); }
public static async Task MergeClanRoles(this BlitzStaticianDbContext dbContext, List <DictionaryClanRole> clanRoles) { var stopwatch = Stopwatch.StartNew(); var ids = clanRoles.Select(l => l.ClanRoleId); var existing = await dbContext.DictionaryClanRole .Where(l => ids.Contains(l.ClanRoleId)) .Select(l => l.ClanRoleId) .AsNoTracking() .ToListAsync(); clanRoles.ForEach(l => { dbContext.DictionaryClanRole.Attach(l); dbContext.Entry(l).State = existing.Contains(l.ClanRoleId) ? EntityState.Modified : EntityState.Added; }); stopwatch.Stop(); }
public async Task CreateClanHistoryOperationTest( long accountId, int databaseClanId, int wgClanId, string databasePlayerRole, string wgPlayerRole, bool historyMustBeCreated) { var operation = _container.Resolve <CreateAccountClanHistoryOperation>(); // FillingDatabase if (databaseClanId > 0) { var clanInfo = new AccountClanInfo { AccountId = accountId, ClanId = databaseClanId, PlayerRole = databasePlayerRole }; await _dbContext.AccountClanInfo.AddAsync(clanInfo); await _dbContext.SaveChangesAsync(); _dbContext.Entry(clanInfo).State = EntityState.Detached; } var operationContext = new StatisticsCollectorOperationContext { Accounts = new List <SatisticsCollectorAccountOperationContext> { new SatisticsCollectorAccountOperationContext { WargamingAccountInfo = new AccountInfo { AccountId = accountId, AccountClanInfo = new AccountClanInfo { ClanId = wgClanId, PlayerRole = wgPlayerRole } }, CurrentAccountInfo = new AccountInfo { AccountId = accountId } } } }; await operation.Execute(operationContext); var history = operationContext.Accounts.Single().AccountClanHistory; if (historyMustBeCreated) { Assert.NotNull(history); Assert.Equal(wgClanId, history.ClanId ?? 0); Assert.Equal(wgPlayerRole, history.PlayerRole); } else { Assert.Null(history); } }