Example #1
0
        public async Task DeleteRefreshTokenGrantAsync(TClient client, string sessionId)
        {
            if (sessionId.IsNullOrWhiteSpace())
            {
                return;
            }

            logger.ScopeTrace($"Delete Refresh Token grant, Route '{RouteBinding.Route}', Session ID '{sessionId}'.");

            var idKey = new Track.IdKey {
                TenantName = RouteBinding.TenantName, TrackName = RouteBinding.TrackName
            };
            await idKey.ValidateObjectAsync();

            RefreshTokenGrant grant = await tenantRepository.DeleteAsync <RefreshTokenTtlGrant>(idKey, d => d.SessionId == sessionId);

            if (grant != null)
            {
                logger.ScopeTrace($"TTL Refresh Token grant deleted, Refresh Token '{grant.RefreshToken}', Session ID '{sessionId}'.");
            }
            else
            {
                grant = await tenantRepository.DeleteAsync <RefreshTokenGrant>(idKey, d => d.SessionId == sessionId);

                if (grant != null)
                {
                    logger.ScopeTrace($"Refresh Token grant deleted, Refresh Token '{grant.RefreshToken}', Session ID '{sessionId}'.");
                }
            }
        }
Example #2
0
        public async Task <int> DeleteListAsync <T>(Track.IdKey idKey, Expression <Func <T, bool> > whereQuery = null) where T : IDataDocument
        {
            if (idKey == null)
            {
                new ArgumentNullException(nameof(idKey));
            }
            await idKey.ValidateObjectAsync();

            var partitionId      = PartitionIdFormat <T>(idKey);
            var orderedQueryable = GetQueryAsync <T>(partitionId, -1);
            var query            = (whereQuery == null) ? orderedQueryable.AsDocumentQuery() : orderedQueryable.Where(whereQuery).AsDocumentQuery();

            double totalRU = 0;

            try
            {
                var response = await query.ExecuteNextAsync <T>();

                totalRU += response.RequestCharge;
                var li    = response.ToHashSet();
                var count = 0;
                foreach (var item in li)
                {
                    var requestOptions = new RequestOptions {
                        PartitionKey = new PartitionKey(partitionId)
                    };
                    var deleteResponse = await client.DeleteDocumentAsync(GetDocumentLink <T>(item.Id), requestOptions);

                    count++;
                    totalRU += deleteResponse.RequestCharge;
                }
                return(count);
            }
            catch (Exception ex)
            {
                throw new CosmosDataException(partitionId, ex);
            }
            finally
            {
                var scopedLogger = httpContextAccessor.HttpContext.RequestServices.GetService <TelemetryScopedLogger>();
                scopedLogger.ScopeMetric($"CosmosDB RU, tenant - delete list type '{typeof(T)}'.", totalRU);
            }
        }
Example #3
0
        public async Task <T> DeleteAsync <T>(Track.IdKey idKey, Expression <Func <T, bool> > whereQuery) where T : IDataDocument
        {
            if (idKey == null)
            {
                new ArgumentNullException(nameof(idKey));
            }
            await idKey.ValidateObjectAsync();

            var partitionId = DataDocument.PartitionIdFormat(idKey);
            var query       = GetQueryAsync <T>(partitionId).Where(whereQuery).AsDocumentQuery();

            double totalRU = 0;

            try
            {
                var response = await query.ExecuteNextAsync <T>();

                totalRU += response.RequestCharge;
                var item = response.FirstOrDefault();
                if (item != null)
                {
                    var requestOptions = new RequestOptions {
                        PartitionKey = new PartitionKey(partitionId)
                    };
                    var deleteResponse = await client.DeleteDocumentAsync(GetDocumentLink <T>(item.Id), requestOptions);

                    totalRU += deleteResponse.RequestCharge;
                }
                await item.ValidateObjectAsync();

                return(item);
            }
            catch (Exception ex)
            {
                throw new CosmosDataException(partitionId, ex);
            }
            finally
            {
                var scopedLogger = httpContextAccessor.HttpContext.RequestServices.GetService <TelemetryScopedLogger>();
                scopedLogger.ScopeMetric($"CosmosDB RU, tenant - delete type '{typeof(T)}'.", totalRU);
            }
        }
Example #4
0
        public async Task <int> DeleteListAsync <T>(Track.IdKey idKey, Expression <Func <T, bool> > whereQuery = null) where T : IDataDocument
        {
            if (idKey == null)
            {
                new ArgumentNullException(nameof(idKey));
            }
            await idKey.ValidateObjectAsync();

            var partitionId = PartitionIdFormat <T>(idKey);
            var query       = GetQueryAsync <T>(partitionId, -1);
            var setIterator = (whereQuery == null) ? query.ToFeedIterator() : query.Where(whereQuery).ToFeedIterator();

            double totalRU = 0;

            try
            {
                var response = await setIterator.ReadNextAsync();

                totalRU += response.RequestCharge;
                var items     = response.ToHashSet();
                var count     = 0;
                var container = GetContainer <T>();
                foreach (var item in items)
                {
                    var deleteResponse = await container.DeleteItemAsync <T>(item.Id, new PartitionKey(partitionId));

                    count++;
                    totalRU += deleteResponse.RequestCharge;
                }
                return(count);
            }
            catch (Exception ex)
            {
                throw new CosmosDataException(partitionId, ex);
            }
            finally
            {
                var scopedLogger = GetScopedLogger();
                scopedLogger.ScopeMetric(metric => { metric.Message = $"CosmosDB RU, tenant - delete list type '{typeof(T)}'."; metric.Value = totalRU; }, properties: GetProperties());
            }
        }