public Task ExecuteAsync(TContext context, TResult result, CancellationToken cancellationToken)
        {
            // NOTE: the etag provide is always synchronous so it makes no sense to move this code into the async method.
            try
            {
                // Don't process model if ETag isn't present
                if (context.ETag == null)
                {
                    return(Task.CompletedTask);
                }

                // Check the ETag for no modifications
                if (context.ETag == etagProvider.GetETag(context.PersistentModel))
                {
                    // TODO: Consider using System.Net.HttpResult for these results instead of exceptions
                    result.Exception = new NotModifiedException();
                }
            }
            catch (Exception ex)
            {
                result.Exception = ex;
            }

            return(Task.CompletedTask);
        }
Esempio n. 2
0
        public async Task ExecuteAsync(TContext context, TResult result, CancellationToken cancellationToken)
        {
            try
            {
                var updatedEntityResult = await _upsertEntity.UpsertAsync(
                    context.PersistentModel,
                    context.EnforceOptimisticLock, cancellationToken);

                context.PersistentModel = updatedEntityResult.Entity;

                // Set the resulting resource's identifier
                if (result.ResourceId == null)
                {
                    result.ResourceId = updatedEntityResult.Entity.Id;
                }

                result.ResourceWasCreated   = updatedEntityResult.IsCreated;
                result.ResourceWasUpdated   = updatedEntityResult.IsModified;
                result.ResourceWasPersisted = true;

                // Set the etag value
                result.ETag = _etagProvider.GetETag(updatedEntityResult.Entity);
            }

            catch (Exception ex)
            {
                result.Exception = ex;
            }
        }
        public Task ExecuteAsync(TContext context, TResult result, CancellationToken cancellationToken)
        {
            try
            {
                result.ResourceWasCreated = false;
                result.ResourceWasUpdated = true;
                result.ETag = _etagProvider.GetETag(context.Resource.ETag);

                result.ResourceId = context.Resource.Id == default(Guid)
                    ? new Guid(context.Resource.ETag)
                    : context.Resource.Id;
            }
            catch (Exception ex)
            {
                result.Exception = ex;
            }

            return(Task.CompletedTask);
        }
        public Task ExecuteAsync(TContext context, TResult result, CancellationToken cancellationToken)
        {
            try
            {
                var model = new TEntityModel();
                context.Resource.Map(model);
                result.ResourceWasCreated = true;
                result.ETag = etagProvider.GetETag(context.Resource.ETag);

                result.ResourceId = model.Id == default(Guid)
                    ? new Guid(context.Resource.ETag)
                    : model.Id;
            }
            catch (Exception ex)
            {
                result.Exception = ex;
            }

            return(Task.CompletedTask);
        }
Esempio n. 5
0
        public async Task <ActionResult <UserResource> > GetUserById([FromRoute] long userId)
        {
            var user = await _userService.GetUser(userId).ConfigureAwait(true);

            if (user == null)
            {
                return(NotFound());
            }

            var responseETag = _etag.GetETag(user);

            if (Request.HasETagHeader() && responseETag == Request.GetETagHeader())
            {
                Response.Headers[HeaderNames.ContentLength] = "0";
                Response.ContentType = string.Empty;
                return(StatusCode((int)HttpStatusCode.NotModified));
            }
            Response.AddETagHeader(responseETag);

            // To return only a user excluding links, use the following return
            //return Ok(user);

            return(Ok(_resourceFactory.CreateUserResource(user)));
        }
Esempio n. 6
0
        public void When_entity_IDateVersionedEntity_value_default_Should_return_null()
        {
            var dateversionedEntity = new DateVersionedEntity
            {
                LastModifiedDate = default(DateTime)
            };

            var result = _eTagProvider.GetETag(dateversionedEntity);

            Assert.AreEqual(null, result);
        }