public async Task <IHttpActionResult> Post(HouseDto houseDto)
        {
            // get a standard DocumentDB client
            await EnsureClientIsConnected();

            // convert the DTO into the Document type
            var houseDoc = JsonConvert.DeserializeObject <HouseDocument>(JsonConvert.SerializeObject(houseDto));

            // DocumentDB Triggers are "opt-in", so opt-in
            var docdbOptions = new RequestOptions();

            docdbOptions.PreTriggerInclude = new List <string> {
                "MaintainHistoryAndTimestamps"
            };

            // execute the create document call safely with retries
            ResourceResponse <Document> created = null;

            try
            {
                created = await DocumentDbExtensions.ExecuteResultWithRetryAsync(() =>
                                                                                 client.CreateDocumentAsync(collectionLink, houseDoc, docdbOptions));
            }
            catch (DocumentDbNonRetriableResponse e)
            {
                return(StatusCode((e.InnerException as DocumentClientException).StatusCode ?? HttpStatusCode.InternalServerError));
            }

            // get the resulting document (it is returned above, but in non-typed form - probably should really be using AutoMapper or something like that here)
            var result = await DocumentDbExtensions.ExecuteQueryWithContinuationAndRetryAsync(
                client.CreateDocumentQuery <HouseDto>(collectionLink)
                .Where(x => x.DocumentType == DocumentType.House)
                .Where(x => x.Id == created.Resource.Id));

            return(Created(result.Single()));
        }
        public async Task <IHttpActionResult> Patch([FromODataUri] string key, Delta <HouseDto> dtoUpdate, ODataQueryOptions <HouseDto> odataOptions)
        {
            // get a standard IQueryable from the standard DocumentDB client
            await EnsureClientIsConnected();

            // apply the clause for getting the document by key
            // because our collection contains multiple document types, filter on that as well
            // execute the query safely with continuation and retries
            var results = await DocumentDbExtensions.ExecuteQueryWithContinuationAndRetryAsync(
                client.CreateDocumentQuery <HouseDto>(collectionLink)
                .Where(x => x.DocumentType == DocumentType.House)
                .Where(x => x.Id == key));

            if (results.Count == 0)
            {
                return(NotFound());
            }

            // patch the original with the delta
            var original = results.Single();

            dtoUpdate.Patch(original);

            // convert the updated DTO into the Document type
            var updatedDoc = JsonConvert.DeserializeObject <HouseDocument>(JsonConvert.SerializeObject(original));

            // DocumentDB Triggers are "opt-in", so opt-in
            var docdbOptions = new RequestOptions();

            docdbOptions.PreTriggerInclude = new List <string> {
                "MaintainHistoryAndTimestamps"
            };

            // DocumentDB ETag checks are "opt-in", so opt-in if requested
            if (odataOptions.IfMatch != null)
            {
                docdbOptions.AccessCondition = new AccessCondition()
                {
                    Type      = AccessConditionType.IfMatch,
                    Condition = (string)odataOptions.IfMatch["ETag"],
                };
            }

            ResourceResponse <Document> updated = null;

            try
            {
                // execute the replace document call safely with retries
                string documentLink = string.Format(documentLinkFormat, key);
                updated = await DocumentDbExtensions.ExecuteResultWithRetryAsync(() =>
                                                                                 client.ReplaceDocumentAsync(documentLink, updatedDoc, docdbOptions));
            }
            catch (DocumentDbNonRetriableResponse e)
            {
                return(StatusCode((e.InnerException as DocumentClientException).StatusCode ?? HttpStatusCode.InternalServerError));
            }

            // get the resulting document (it is returned above, but in non-typed form - probably should really be using AutoMapper or something like that here)
            var result = await DocumentDbExtensions.ExecuteQueryWithContinuationAndRetryAsync(
                client.CreateDocumentQuery <HouseDto>(collectionLink)
                .Where(x => x.DocumentType == DocumentType.House)
                .Where(x => x.Id == updated.Resource.Id));

            return(Updated(result.Single()));
        }