예제 #1
0
        private async Task <TData> UpdateAsync(string id, Delta <TData> patch, bool includeDeleted)
        {
            if (id == null)
            {
                throw new ArgumentNullException("id");
            }

            if (patch == null)
            {
                throw new ArgumentNullException("patch");
            }

            TData current = await this.Lookup(id, includeDeleted).Queryable.FirstOrDefaultAsync();

            if (current == null)
            {
                throw new HttpResponseException(this.Request.CreateNotFoundResponse());
            }

            // Set the original version based on etag (if present)
            byte[] patchVersion = patch.GetPropertyValueOrDefault <TData, byte[]>(TableUtils.VersionPropertyName);
            if (patchVersion != null)
            {
                this.context.Entry(current).OriginalValues[TableUtils.VersionPropertyName] = patchVersion;
            }

            // Apply the patch and verify that keys match (keys can't change as part of an update)
            patch.Patch(current);
            this.VerifyUpdatedKey(id, current);

            await this.SubmitChangesAsync();

            return(current);
        }
        protected virtual async Task <TData> UpdateEntityAsync(Delta <TData> patch, bool includeDeleted, params object[] keys)
        {
            if (patch == null)
            {
                throw new ArgumentNullException("patch");
            }

            if (keys == null)
            {
                throw new ArgumentNullException("keys");
            }

            TModel model = await this.Context.Set <TModel>().FindAsync(keys);

            if (model == null)
            {
                throw new HttpResponseException(this.Request.CreateNotFoundResponse());
            }

            TData data = Mapper.Map <TModel, TData>(model);

            if (!includeDeleted && data.Deleted)
            {
                throw new HttpResponseException(this.Request.CreateNotFoundResponse());
            }

            // Set the original version based on etag (if present)
            byte[] patchVersion = patch.GetPropertyValueOrDefault <TData, byte[]>(TableUtils.VersionPropertyName);
            if (patchVersion != null)
            {
                this.SetOriginalVersion(model, patchVersion);
            }

            patch.Patch(data);
            Mapper.Map <TData, TModel>(data, model);

            await this.SubmitChangesAsync();

            return(Mapper.Map <TData>(model));
        }