public virtual async Task <IActionResult> ReplaceItemAsync(string id, [FromBody] TEntity item)
        {
            if (item.Id != id)
            {
                return(BadRequest());
            }

            var entity = await TableRepository.LookupAsync(id).ConfigureAwait(false);

            if (entity == null || entity.Deleted || !IsAuthorized(TableOperation.Replace, entity))
            {
                return(NotFound());
            }

            var preconditionStatusCode = ETag.EvaluatePreconditions(entity, Request.GetTypedHeaders());

            if (preconditionStatusCode != StatusCodes.Status200OK)
            {
                AddHeadersToResponse(entity);
                return(StatusCode(preconditionStatusCode, entity));
            }

            var replacement = await TableRepository.ReplaceAsync(PrepareItemForStore(item)).ConfigureAwait(false);

            AddHeadersToResponse(replacement);
            return(Ok(replacement));
        }
        public virtual async Task <IActionResult> DeleteItemAsync(string id)
        {
            var entity = await TableRepository.LookupAsync(id).ConfigureAwait(false);

            if (entity == null || entity.Deleted || !IsAuthorized(TableOperation.Delete, entity))
            {
                return(NotFound());
            }

            var preconditionStatusCode = ETag.EvaluatePreconditions(entity, Request.GetTypedHeaders());

            if (preconditionStatusCode != StatusCodes.Status200OK)
            {
                return(StatusCode(preconditionStatusCode, entity));
            }

            if (TableControllerOptions.SoftDeleteEnabled)
            {
                entity.Deleted = true;
                await TableRepository.ReplaceAsync(PrepareItemForStore(entity)).ConfigureAwait(false);
            }
            else
            {
                await TableRepository.DeleteAsync(entity.Id).ConfigureAwait(false);
            }
            return(NoContent());
        }
        public virtual async Task <IActionResult> CreateItemAsync([FromBody] TEntity item)
        {
            if (item.Id == null)
            {
                item.Id = Guid.NewGuid().ToString("N");
            }

            if (!IsAuthorized(TableOperation.Create, item))
            {
                return(Unauthorized());
            }

            var entity = await TableRepository.LookupAsync(item.Id).ConfigureAwait(false);

            var preconditionStatusCode = ETag.EvaluatePreconditions(entity, Request.GetTypedHeaders());

            if (preconditionStatusCode != StatusCodes.Status200OK)
            {
                return(StatusCode(preconditionStatusCode, entity));
            }

            if (entity != null)
            {
                AddHeadersToResponse(entity);
                return(Conflict(entity));
            }

            var createdEntity = await TableRepository.CreateAsync(PrepareItemForStore(item)).ConfigureAwait(false);

            AddHeadersToResponse(createdEntity);
            var uri = $"{Request.GetEncodedUrl()}/{createdEntity.Id}";

            return(Created(uri, createdEntity));
        }
        public virtual async Task <IActionResult> ReadItemAsync(string id)
        {
            var entity = await TableRepository.LookupAsync(id).ConfigureAwait(false);

            if (entity == null || entity.Deleted || !IsAuthorized(TableOperation.Read, entity))
            {
                return(NotFound());
            }

            var preconditionStatusCode = ETag.EvaluatePreconditions(entity, Request.GetTypedHeaders(), true);

            if (preconditionStatusCode != StatusCodes.Status200OK)
            {
                if (preconditionStatusCode == StatusCodes.Status304NotModified)
                {
                    AddHeadersToResponse(entity);
                }
                return(StatusCode(preconditionStatusCode, entity));
            }

            AddHeadersToResponse(entity);
            return(Ok(entity));
        }
        public virtual async Task <IActionResult> PatchItemAsync(string id, [FromBody] JsonPatchDocument <TEntity> patchDocument)
        {
            var entity = await TableRepository.LookupAsync(id).ConfigureAwait(false);

            if (entity == null || !IsAuthorized(TableOperation.Patch, entity))
            {
                return(NotFound());
            }
            var entityIsDeleted = (TableControllerOptions.SoftDeleteEnabled && entity.Deleted);

            var preconditionStatusCode = ETag.EvaluatePreconditions(entity, Request.GetTypedHeaders());

            if (preconditionStatusCode != StatusCodes.Status200OK)
            {
                AddHeadersToResponse(entity);
                return(StatusCode(preconditionStatusCode, entity));
            }

            patchDocument.ApplyTo(entity);
            if (entity.Id != id)
            {
                return(BadRequest());
            }

            // Special Case:
            //  If SoftDelete is enabled, and the original record is DELETED, then you can
            //  continue as long as one of the operations is an undelete operation.
            if (entityIsDeleted && entity.Deleted)
            {
                return(NotFound());
            }

            var replacement = await TableRepository.ReplaceAsync(PrepareItemForStore(entity)).ConfigureAwait(false);

            AddHeadersToResponse(replacement);
            return(Ok(replacement));
        }
Exemple #6
0
        public void EvaluatePreconditions_IfUnmodifiedSinceLater_POST_TestItem_200()
        {
            var actual = ETag.EvaluatePreconditions <Movie>(testMovie, GetRequestHeaders("If-Unmodified-Since", laterTestDate), false);

            Assert.AreEqual(200, actual);
        }
Exemple #7
0
        public void EvaluatePreconditions_IfUnmodifiedSInceEarlier_POST_TestItem_412()
        {
            var actual = ETag.EvaluatePreconditions <Movie>(testMovie, GetRequestHeaders("If-Unmodified-Since", earlierTestDate), false);

            Assert.AreEqual(412, actual);
        }
Exemple #8
0
        public void EvaluatePreconditions_IfModifiedSinceLater_GET_TestItem_304()
        {
            var actual = ETag.EvaluatePreconditions <Movie>(testMovie, GetRequestHeaders("If-Modified-Since", laterTestDate), true);

            Assert.AreEqual(304, actual);
        }
Exemple #9
0
        public void EvaluatePreconditions_IfModifiedSinceEarlier_GET_NullItem_200()
        {
            var actual = ETag.EvaluatePreconditions <Movie>(null, GetRequestHeaders("If-Modified-Since", earlierTestDate), true);

            Assert.AreEqual(304, actual);
        }
Exemple #10
0
        public void EvaluatePreconditions_IfNoneMatch_NonMatchingItem_200()
        {
            var actual = ETag.EvaluatePreconditions <Movie>(testMovie, GetRequestHeaders("If-None-Match", nonMatchingETag), false);

            Assert.AreEqual(200, actual);
        }
Exemple #11
0
        public void EvaluatePreconditions_GET_IfNoneMatch_TestItem_304()
        {
            var actual = ETag.EvaluatePreconditions <Movie>(testMovie, GetRequestHeaders("If-None-Match", matchingETag), true);

            Assert.AreEqual(304, actual);
        }
Exemple #12
0
        public void EvaluatePreconditions_POST_IfNoneMatch_TestItem_412()
        {
            var actual = ETag.EvaluatePreconditions <Movie>(testMovie, GetRequestHeaders("If-None-Match", matchingETag), false);

            Assert.AreEqual(412, actual);
        }
Exemple #13
0
        public void EvaluatePreconditions_IfMatchStar_TestItem_200()
        {
            var actual = ETag.EvaluatePreconditions <Movie>(testMovie, GetRequestHeaders("If-Match", "*"), false);

            Assert.AreEqual(200, actual);
        }
Exemple #14
0
        public void EvaluatePreconditions_IfMatch_NullItem_412()
        {
            var actual = ETag.EvaluatePreconditions <Movie>(null, GetRequestHeaders("If-Match", matchingETag), false);

            Assert.AreEqual(412, actual);
        }
Exemple #15
0
        public void EvaluatePreconditions_IfNoneMatchStar_NullItem_200()
        {
            var actual = ETag.EvaluatePreconditions <Movie>(null, GetRequestHeaders("If-None-Match", "*"), false);

            Assert.AreEqual(200, actual);
        }
Exemple #16
0
        public void EvaluatePreconditions_NoHeaders_TestItem_200()
        {
            var actual = ETag.EvaluatePreconditions <Movie>(testMovie, GetEmptyRequestHeaders(), false);

            Assert.AreEqual(200, actual);
        }