public async Task <bool> AddOperationsHistory(AddTransaction message) { OperationHistoryEntity operationHistory1 = new OperationHistoryEntity(); OperationHistoryEntity operationHistory2 = new OperationHistoryEntity(); operationHistory1.AccountId = message.FromAccount; var b = await _context.Accounts.FirstOrDefaultAsync(b => b.AccountId == message.FromAccount).ConfigureAwait(false); operationHistory1.Balance = b.Balance; operationHistory1.CreditOrDebit = false; operationHistory1.Date = DateTime.Now; operationHistory1.Id = Guid.NewGuid(); operationHistory1.TransactionAmount = message.Amount; operationHistory1.TransactionId = message.TransactionId; operationHistory2.AccountId = message.ToAccount; var b2 = await _context.Accounts.FirstOrDefaultAsync(b => b.AccountId == message.ToAccount).ConfigureAwait(false); operationHistory2.Balance = b2.Balance; operationHistory2.CreditOrDebit = true; operationHistory2.Date = DateTime.Now; operationHistory2.Id = Guid.NewGuid(); operationHistory2.TransactionAmount = message.Amount; operationHistory2.TransactionId = message.TransactionId; await _context.OperationHistoryEntity.AddAsync(operationHistory1); await _context.OperationHistoryEntity.AddAsync(operationHistory2); await _context.SaveChangesAsync(); return(true); }
private async ValueTask HandleOperationUpdate(List <OperationUpdate> updates) { using var locker = await _locker.GetLocker(); using var activity = MyTelemetry.StartActivity("Handle Operation OutgoingEvent's") ?.AddTag("count-events", updates.Count); try { var sw = new Stopwatch(); sw.Start(); var operations = new List <OperationHistoryEntity>(); var groups = updates.GroupBy(t => new { t.OperationId, t.OperationType, t.WalletId, t.AssetId }); foreach (var group in groups) { var entity = group.OrderByDescending(t => t.TimeStamp).First(); var operationEntity = new OperationHistoryEntity(entity); operations.Add(operationEntity); } if (operations.Any()) { await using var ctx = DatabaseContext.Create(_dbContextOptionsBuilder); await ctx.UpsertOperationAsync(operations); await PopulateBalances(operations); } sw.Stop(); _logger.LogDebug( "Write {countUpdates} operations update in database from {countEvents} operations events. Time: {elapsedText} [{elapsedMilliseconds} ms]", operations.Count, updates.Count, sw.Elapsed.ToString(), sw.ElapsedMilliseconds); } catch (Exception ex) { ex.FailActivity(); updates.AddToActivityAsJsonTag("operation events"); _logger.LogError(ex, "When handling operations updates"); throw; } }