示例#1
0
        public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken ct)
        {
            using TransactionScope tran    = Tran.BeginScope(IsolationLevel.ReadUncommitted);
            using SqlConnection connection = _dbContextFactory.CreateConnection();
            await connection.OpenAsync(ct);

            SqlCommand cmd = connection.CreateCommand();

            cmd.CommandText =
                "select top(1) MigrationId from Tds.__EFMigrationsHistory order by MigrationId desc";
            object lastMigration = await cmd.ExecuteScalarAsync(ct);

            cmd.CommandText = "select count(*) from Tds.__EFMigrationsHistory";
            object count = await cmd.ExecuteScalarAsync(ct);

            return(HealthCheckResult.Healthy(
                       CompareMigrationHistoryToAssembly(lastMigration.ToString(), int.Parse(count.ToString() !))));
        }
示例#2
0
        public async Task <WalletReportLine[]> GenerateReportAsync(string accountId, CancellationToken ct)
        {
            using TransactionScope tran = Tran.BeginScope(IsolationLevel.ReadCommitted);
            using AppDbContext ctx      = _dbContextFactory.CreateContext();

            _logger.LogDebug("Trying calculate balance");

            WalletReportLine[] income = await ctx.Set <PaymentEntity>()
                                        .Where(p => p.To == accountId && p.TransactionSuccessful)
                                        .GroupBy(g => g.From)
                                        .Select(s => new WalletReportLine()
            {
                AccountId = s.Key, Amount = s.Sum(r => r.Amount)
            })
                                        .ToArrayAsync(ct);

            WalletReportLine[] outcome = await ctx.Set <PaymentEntity>()
                                         .Where(p => p.From == accountId && p.TransactionSuccessful)
                                         .GroupBy(g => g.To)
                                         .Select(s => new WalletReportLine()
            {
                AccountId = s.Key, Amount = -s.Sum(r => r.Amount)
            })
                                         .ToArrayAsync(ct);

            WalletReportLine[] report =
                income.Union(outcome)
                .GroupBy(g => g.AccountId)
                .Select(s => new WalletReportLine()
            {
                AccountId = s.Key, Amount = s.Sum(r => r.Amount)
            })
                .ToArray();

            _logger.LogDebug("The balance is calculated");
            tran.Complete();

            return(report);
        }
示例#3
0
        private async Task SavePaymentsAsync(PaymentEntity[] payments, CancellationToken ct)
        {
            if (!payments.Any())
            {
                return;
            }

            using TransactionScope tran = Tran.BeginScope(IsolationLevel.Serializable);
            using AppDbContext ctx      = _dbContextFactory.CreateContext();

            IEnumerable <long> range = payments.Select(p => p.Id);

            _logger.LogDebug("Trying to fetch existed payments in range {0}", range);

            ctx.SelectWithUpdLock = true;
            long[] existingPayments = await ctx.Set <PaymentEntity>()
                                      .Where(p => range.Contains(p.Id))
                                      .AsNoTracking()
                                      .Select(p => p.Id)
                                      .ToArrayAsync(ct);


            _logger.LogDebug("{0} payments found", existingPayments.Length);

            PaymentEntity[] paymentsToSave =
                payments.Where(p => !existingPayments
                               .Contains(p.Id)).ToArray();

            _logger.LogDebug("Trying to save new payments {0}", (object)paymentsToSave);

            ctx.Set <PaymentEntity>().AddRange(paymentsToSave);

            await ctx.SaveChangesAsync(ct);

            tran.Complete();
            _logger.LogDebug("{0} new payments successfully saved", paymentsToSave.Count());
        }