Esempio n. 1
0
        public async Task <TResult> ExecuteStoredProcedureAsync <TResult>(Domain.RequestOptions options, params dynamic[] procedureParams) where TResult : class
        {
            if (_databaseName == null)
            {
                throw new DataStoreException("Connect must be called to query CosmosDB");
            }

            try
            {
                var response = await _client.ExecuteStoredProcedureAsync <TResult>(
                    UriFactory.CreateStoredProcedureUri(_databaseName, _collectionName, _storedProcedureName),
                    options.ToRequestOptions(), procedureParams);

                //Console.WriteLine("RU Charge " + response.RequestCharge);

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    return(null);
                }

                return(response.Response);
            }
            catch (DocumentClientException e)
            {
                if (e.StatusCode == HttpStatusCode.NotFound)
                {
                    return(null);
                }

                throw;
            }
        }
        public async Task <TResult> QueryById <TResult>(string id, Domain.RequestOptions options = null) where TResult : class
        {
            if (_databaseName == null)
            {
                throw new DataStoreException("Connect must be called to query CosmosDB");
            }

            try
            {
                var response = await _client.ReadDocumentAsync(UriFactory.CreateDocumentUri(_databaseName, _collectionName, id), options.ToRequestOptions(), _cancellationToken);

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    return(null);
                }
                return((TResult)(dynamic)response.Resource);
            }
            catch (DocumentClientException e)
            {
                if (e.StatusCode == HttpStatusCode.NotFound)
                {
                    return(null);
                }
                throw;
            }
        }
        public async Task <bool> DeleteAsync(string id, Domain.RequestOptions options = null)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentException("Missing document Id", nameof(id));
            }

            if (_databaseName == null)
            {
                throw new DataStoreException("Connect must be called to delete from CosmosDB");
            }

            try
            {
                var response = await _client.DeleteDocumentAsync(UriFactory.CreateDocumentUri(_databaseName, _collectionName, id), options.ToRequestOptions(), _cancellationToken);

                return(response.StatusCode == HttpStatusCode.NoContent);
            }
            catch (DocumentClientException e)
            {
                if (e.StatusCode == HttpStatusCode.NotFound)
                {
                    return(true);
                }
                throw;
            }
        }
        public async Task <TEntity> UpdateAsync <TEntity>(TEntity entity, Domain.RequestOptions options = null) where TEntity : class
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }
            if (_databaseName == null)
            {
                throw new DataStoreException("Connect must be called to update CosmosDB");
            }

            try
            {
                var response = await _client.UpsertDocumentAsync(UriFactory.CreateDocumentCollectionUri(_databaseName, _collectionName), entity, options.ToRequestOptions(), true, _cancellationToken);

                if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created)
                {
                    return(entity);
                }
                throw new DocumentUpdateException("Unable to update product. Invalid status returned from CosmosDB.");
            }
            catch (DocumentClientException e)
            {
                if (e.StatusCode == HttpStatusCode.NotFound)
                {
                    throw new DocumentUpdateException("Unable to update product. Invalid status returned from CosmosDB.");
                }
                throw;
            }
        }
        public static Microsoft.Azure.Documents.Client.RequestOptions ToRequestOptions(this Domain.RequestOptions options)
        {
            if (options == null)
            {
                return(null);
            }
            var ro = new Microsoft.Azure.Documents.Client.RequestOptions
            {
                PreTriggerInclude  = options.PreTriggerInclude,
                PostTriggerInclude = options.PostTriggerInclude
            };

            if (!string.IsNullOrWhiteSpace(options.PartitionKey))
            {
                ro.PartitionKey = new PartitionKey(options.PartitionKey);
            }
            return(ro);
        }