コード例 #1
0
        public void UpdateEntityDelegatesGenerationTest(string json)
        {
            // Get expected values from incoming json
            var jObject = JObject.Parse(json);

            var nameExpectedValue = jObject.GetValue("Name")?.Value <string>();
            var nameValueExists   = jObject.GetValue("Name") != null;

            var descriptionExpectedValue = jObject.GetValue("Description")?.Value <string>();
            var descriptionValueExists   = jObject.GetValue("Description") != null;

            // Create entity to be updated with values from json
            var sourceEntity = new Car()
            {
                Name        = Guid.NewGuid().ToString("N"),
                Description = Guid.NewGuid().ToString("N")
            };

            // Building update delegated & apply updates
            var updateDelegates = dynamicHelper.GetItemUpdateDelegates(jObject);
            var updatedEntity   = ApplyUpdateDelegates(updateDelegates, sourceEntity);

            if (nameValueExists)
            {
                // Expect name was updated
                Assert.AreEqual(nameExpectedValue, updatedEntity.Name);
            }
            else
            {
                // Expect name left unchanged
                Assert.AreEqual(sourceEntity.Name, updatedEntity.Name);
            }

            if (descriptionValueExists)
            {
                // Expect description was updated
                Assert.AreEqual(descriptionExpectedValue, updatedEntity.Description);
            }
            else
            {
                // Expect description left unchanged
                Assert.AreEqual(sourceEntity.Description, updatedEntity.Description);
            }
        }
コード例 #2
0
        public async Task <IActionResult> Update([FromBody] JObject value)
        {
            IEnumerable <Action <Car> > propertyUpdateActions;

            var itemId = carDynamicUpdateHelper.GetItemId(value);

            propertyUpdateActions = carDynamicUpdateHelper.GetItemUpdateDelegates(value);

            try
            {
                await carsService.CreateOrUpdate(itemId, propertyUpdateActions);

                return(Ok());
            }
            catch (ValidationException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (KeyNotFoundException)
            {
                return(NotFound());
            }
        }