Example #1
0
        public async Task UpsertDocument <T>(string collectionId, string documentId, DocumentBase <T> item)
        {
            if (string.IsNullOrWhiteSpace(collectionId))
            {
                throw new ArgumentNullException(nameof(collectionId));
            }
            if (string.IsNullOrWhiteSpace(documentId))
            {
                throw new ArgumentNullException(nameof(documentId));
            }
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }
            if (item.VM == null)
            {
                throw new ArgumentNullException(nameof(item.VM));
            }
            if (string.IsNullOrWhiteSpace(item.ETag))
            {
                throw new ArgumentNullException(nameof(item.ETag));
            }

            var accessOption = new AccessCondition {
                Condition = item.ETag, Type = AccessConditionType.IfMatch
            };

            var collectionUri = UriFactory.CreateDocumentCollectionUri(dbId, collectionId);

            try
            {
                //Here we need to await so as to convert the exception to the correct, domain-specific one.
                await client.UpsertDocumentAsync(collectionUri, DocumentWrapper.FromBase(item, documentId),
                                                 new RequestOptions { AccessCondition = accessOption }, true);
            }
            catch (DocumentClientException ex) when(ex.StatusCode == HttpStatusCode.PreconditionFailed || ex.StatusCode == HttpStatusCode.Conflict)
            {
                throw new ConcurrencyException(ex.Message, JsonConvert.SerializeObject(item));
            }
        }