public async Task <ActionResult> Apply() { var viewModel = new IndexViewModel(); var currentClient = await GetCurrentClient(); var applier = new RulesApplier(); var allLinesOfClient = Db.Lines.Where(x => x.Account.ClientId == currentClient.Id); viewModel.ApplierResult = await applier.Apply(Db, currentClient.Id, allLinesOfClient, true); if (viewModel.ApplierResult.LinesUpdated > 0) { await Db.SaveChangesAsync(); } return(View("Index", viewModel)); }
public async Task <ImportResult> LoadFileAndImport(Db db, int clientId, int accountId, RulesApplier rulesApplier) { var result = new ImportResult(); var lines = GetRecords <Line>(); var anyChangesToDatabase = false; foreach (var line in lines) { var lineExists = db.Lines.Any(x => x.AccountId == accountId && x.OriginalDate == line.OriginalDate && x.OriginalText == line.OriginalText && // ReSharper disable once CompareOfFloatsByEqualityOperator x.OriginalAmount == line.OriginalAmount); if (!lineExists) { line.AccountId = accountId; await rulesApplier.Apply(db, clientId, line); line.DateOfCreationUtc = DateTime.UtcNow; db.Lines.Add(line); result.NewLinesCount++; anyChangesToDatabase = true; } else { result.DuplicateLinesCount++; } } if (anyChangesToDatabase) { await db.SaveChangesAsync(); } return(result); }