コード例 #1
0
        public async Task <IHttpActionResult> Patch(int key, Delta <ResourcePool> patch)
        {
            var resourcePool = await _resourcePoolManager.GetByIdAsync(key, true);

            // Owner check: Entity must belong to the current user
            var currentUserId = User.Identity.GetUserId <int>();

            if (currentUserId != resourcePool.UserId)
            {
                return(StatusCode(HttpStatusCode.Forbidden));
            }

            patch.Patch(resourcePool);

            try
            {
                await _resourcePoolManager.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                // Unique key exception
                if (!patch.GetChangedPropertyNames().Any(item => item == "Key"))
                {
                    throw;
                }

                patch.TryGetPropertyValue("Key", out object resourcePoolKey);

                if (resourcePoolKey == null)
                {
                    throw;
                }

                if (await _resourcePoolManager.All.AnyAsync(item => item.Key == resourcePoolKey.ToString()))
                {
                    return(new UniqueKeyConflictResult(Request, "Key", resourcePoolKey.ToString()));
                }

                throw;
            }

            return(Ok(resourcePool));
        }