Ejemplo n.º 1
0
 internal void RegisterUpdateResource()
 {
     if (!_updateRegistered)
     {
         _updateRegistered = true;
         DeviceContext.Current.UpdateResource += OnUpdateResource;
         UpdateResource?.Invoke(this, EventArgs.Empty);
     }
 }
Ejemplo n.º 2
0
        public async virtual Task <IActionResult> UpdateAsync(ResourceModel <TResource> resourceModel)
        {
            if (!User.IsAuthorized(ResourceType))
            {
                return(Unauthorized());
            }

            var updateResourceEvent = new UpdateResource <TResource>(resourceModel);
            var context             = await _mediator.Send(updateResourceEvent);

            if (context != null)
            {
                resourceModel = context;
            }

            if (resourceModel.Errors.Any())
            {
                foreach (var error in resourceModel.Errors)
                {
                    ModelState.AddModelError(error.Key, error.Value);
                }
            }

            if (!ModelState.IsValid)
            {
                return(PartialView("_Editor", resourceModel));
            }

            var update = await _repository.UpdateAsync(resourceModel.Resource, UserID);

            if (update <= 0)
            {
                return(BadRequest());
            }

            _logger.LogResourceUpdated(ResourceType, resourceModel.ID);

            var updatedResource = await _repository.ReadAsync(resourceModel.ID);

            var resourceUpdatedEvent = new ResourceUpdated <TResource>(updatedResource);
            await _mediator.Publish(resourceUpdatedEvent);

            var resourcePersistedEvent = new ResourcePersisted <TResource>();
            await _mediator.Publish(resourcePersistedEvent);

            var result = new ResourceModel <TResource>()
            {
                ID       = resourceModel.ID,
                Resource = updatedResource
            };

            return(PartialView("_Editor", result));
        }
        /// <summary>
        /// Given a *sorted* list of source data items (currentItems),
        /// and a *sorted* list of resources:
        ///    For each source item that doesn't have a matching resource, attempt to create a resource.
        ///    For each resource that doesn't have a matching source item, destroy that resource.
        ///    For each source item with a matching resource, update the resource.
        /// </summary>
        /// <remarks>
        /// After this Sync, the list of resources will have exactly one resource for each item
        /// in currentItems, and currentItems and resources will be the same length.
        /// The exception is if the creator function returns false for any item(s), then those item(s)
        /// will not have matching resources, and resources will be shorter than currentItems.
        /// In any case, resources will remain sorted.
        /// Sync completes in a single pass over the data, so in O(max(currentItems.Count, resources.Count)) time.
        /// </remarks>
        /// <typeparam name="ItemType">Type of source items.</typeparam>
        /// <typeparam name="ResourceType">Type of resources.</typeparam>
        /// <param name="currentItems">List of current source items.</param>
        /// <param name="resources">List of resources to by synced to currentItems.</param>
        /// <param name="compareIds">Function to compare an item with a resource. See above.</param>
        /// <param name="creator">Callback to create a missing resource. See above.</param>
        /// <param name="updater">Callback to update an existing resource. See above.</param>
        /// <param name="destroyer">Callback to destroy a resource which no longer has a matching source item.</param>
        public static void Sync <ItemType, ResourceType>(
            IReadOnlyList <ItemType> currentItems,
            List <ResourceType> resources,
            CompareToResource <ItemType, ResourceType> compareIds,
            CreateResource <ItemType, ResourceType> creator,
            UpdateResource <ItemType, ResourceType> updater,
            DestroyResource <ResourceType> destroyer)
        {
            int iRsrc = resources.Count - 1;
            int iItem = currentItems.Count - 1;

            while (iRsrc >= 0 && iItem >= 0)
            {
                /// If the existing resource is greater than the current item,
                /// then there is no corresponding current item. So delete the resource.
                int comparison = compareIds(currentItems[iItem], resources[iRsrc]);
                if (comparison < 0)
                {
                    /// items id less than resources, means
                    ///    no item for this resource.
                    /// delete the surplus resource.
                    destroyer(resources[iRsrc]);
                    resources.RemoveAt(iRsrc);
                    --iRsrc;
                    /// Remain on iItem
                }
                /// If the existing resource is less, then we are missing a resource for the larger current item.
                /// Add it now.
                else if (comparison > 0)
                {
                    /// items id greater than resources, means
                    ///    for this item, no matching resource.
                    /// create and add.
                    ResourceType resource;
                    if (creator(currentItems[iItem], out resource))
                    {
                        resources.Insert(iRsrc + 1, resource);
                    }
                    /// If successful, now ci[iItem] <==> re[iRsrc+1]. So move on to ci[iItem-1] / re[iRsrc];
                    /// If failed, we've tried ci[iItem] (which failed) and re[] is unchanged.
                    /// So either way move on to ci[iItem-1] / re[iRsrc]
                    --iItem;
                }
                else
                {
                    /// item and resource match, just update.
                    updater(currentItems[iItem], resources[iRsrc]);
                    --iItem;
                    --iRsrc;
                }
            }

            // If iRsrc && iItem are both less than zero, then we are done.
            // If iRsrc < 0 but iItem >= 0, then we need more resources created, from iItem on down.
            // If iRsrc >= 0 but iItem < 0, then from iRsrc down needs to be deleted.
            Debug.Assert(iRsrc < 0 || iItem < 0);
            while (iItem >= 0)
            {
                ResourceType resource;
                if (creator(currentItems[iItem], out resource))
                {
                    resources.Insert(0, resource);
                }
                --iItem;
            }
            while (iRsrc >= 0)
            {
                destroyer(resources[iRsrc]);
                resources.RemoveAt(iRsrc);
                --iRsrc;
            }
            Debug.Assert(resources.Count <= currentItems.Count);
        }
        private async void VerifyPutPatchGetAndDeleteOperations_Scenarios(ProximityPlacementGroup inputProximityPlacementGroup,
                                                                          ProximityPlacementGroup expectedProximityPlacementGroup)
        {
            var proximityPlacementGroupName = Recording.GenerateAssetName("testppg");

            // Create and expect success.
            ProximityPlacementGroup outProximityPlacementGroup = await ProximityPlacementGroupsOperations.CreateOrUpdateAsync(m_resourceGroup1Name, proximityPlacementGroupName, inputProximityPlacementGroup);

            ValidateProximityPlacementGroup(expectedProximityPlacementGroup, outProximityPlacementGroup, proximityPlacementGroupName);

            // Update and expect success.
            inputProximityPlacementGroup.Tags.Add("UpdateTag1", "updateValue1");
            outProximityPlacementGroup = await ProximityPlacementGroupsOperations.CreateOrUpdateAsync(m_resourceGroup1Name, proximityPlacementGroupName, inputProximityPlacementGroup);

            ValidateProximityPlacementGroup(expectedProximityPlacementGroup, outProximityPlacementGroup, proximityPlacementGroupName);

            // Get and expect success.
            outProximityPlacementGroup = await ProximityPlacementGroupsOperations.GetAsync(m_resourceGroup1Name, proximityPlacementGroupName);

            ValidateProximityPlacementGroup(expectedProximityPlacementGroup, outProximityPlacementGroup, proximityPlacementGroupName);

            // Put and expect failure
            try
            {
                //Updating ProximityPlacementGroupType in inputProximityPlacementGroup for a Update call.
                if (expectedProximityPlacementGroup.ProximityPlacementGroupType == ProximityPlacementGroupType.Standard)
                {
                    inputProximityPlacementGroup.ProximityPlacementGroupType = ProximityPlacementGroupType.Ultra;
                }
                else
                {
                    inputProximityPlacementGroup.ProximityPlacementGroupType = ProximityPlacementGroupType.Standard;
                }

                outProximityPlacementGroup = null;
                outProximityPlacementGroup = await ProximityPlacementGroupsOperations.CreateOrUpdateAsync(m_resourceGroup1Name, proximityPlacementGroupName, inputProximityPlacementGroup);
            }
            catch (Exception ex)
            {
                //if (ex.StatusCode == HttpStatusCode.Conflict)
                //{
                //    Assert.AreEqual("Changing property 'proximityPlacementGroup.properties.proximityPlacementGroupType' is not allowed.", ex.Message );
                //}
                //else if (ex.Response.StatusCode == HttpStatusCode.BadRequest)
                //{
                //    Assert.Equal("The subscription is not registered for private preview of Ultra Proximity Placement Groups.", ex.Message, StringComparer.OrdinalIgnoreCase);
                //}
                //else
                //{
                //    Console.WriteLine($"Expecting HttpStatusCode { HttpStatusCode.Conflict} or { HttpStatusCode.BadRequest}, while actual HttpStatusCode is { ex.Response.StatusCode}.");
                //    throw;
                //}
                Console.WriteLine($"Expecting HttpStatusCode { HttpStatusCode.Conflict} or { HttpStatusCode.BadRequest}, while actual HttpStatusCode is { ex.Message}.");
                throw;
            }
            Assert.True(outProximityPlacementGroup == null, "ProximityPlacementGroup in response should be null.");

            //Patch and expect success
            UpdateResource proximityPlacementGroupUpdate = new UpdateResource();

            proximityPlacementGroupUpdate.Tags.InitializeFrom(inputProximityPlacementGroup.Tags);
            //Note: Same Tags object is referred in proximityPlacementGroupUpdate and expectedProximityPlacementGroup,
            //hence this will also update tags in expectedProximityPlacementGroup.
            proximityPlacementGroupUpdate.Tags.Add("UpdateTag2", "updateValue2");
            outProximityPlacementGroup = await ProximityPlacementGroupsOperations.UpdateAsync(m_resourceGroup1Name, proximityPlacementGroupName, proximityPlacementGroupUpdate);

            ValidateProximityPlacementGroup(expectedProximityPlacementGroup, outProximityPlacementGroup, proximityPlacementGroupName);

            // Clean up
            await ProximityPlacementGroupsOperations.DeleteAsync(m_resourceGroup1Name, proximityPlacementGroupName);
        }
 public virtual async Task <Response <ProximityPlacementGroup> > UpdateAsync(string resourceGroupName, string proximityPlacementGroupName, UpdateResource parameters, CancellationToken cancellationToken = default)
 {
     using var scope = _clientDiagnostics.CreateScope("ProximityPlacementGroupsOperations.Update");
     scope.Start();
     try
     {
         return(await RestClient.UpdateAsync(resourceGroupName, proximityPlacementGroupName, parameters, cancellationToken).ConfigureAwait(false));
     }
     catch (Exception e)
     {
         scope.Failed(e);
         throw;
     }
 }
Ejemplo n.º 6
0
 public virtual Response <ProximityPlacementGroup> Update(string resourceGroupName, string proximityPlacementGroupName, UpdateResource parameters, CancellationToken cancellationToken = default)
 {
     using var scope = _clientDiagnostics.CreateScope("ProximityPlacementGroupsClient.Update");
     scope.Start();
     try
     {
         return(RestClient.Update(resourceGroupName, proximityPlacementGroupName, parameters, cancellationToken));
     }
     catch (Exception e)
     {
         scope.Failed(e);
         throw;
     }
 }