public IHttpActionResult Patch(Delta<SampleModel> delta) { // Using the Patch method on Delta<T>, will only overwrite only the properties whose value has // changed. var model = new SampleModel(); delta.Patch(model); // Using Delta doesn't invoke validation on the values that are provided, so use the Validate method // on the model object after patching to validate it. this.Validate(model); if (!ModelState.IsValid) { return BadRequest(ModelState); } var builder = new StringBuilder(); builder.AppendLine("Updated Properties:"); foreach(var property in delta.GetChangedPropertyNames()) { object value; delta.TryGetPropertyValue(property, out value); builder.AppendLine(String.Format("\t{0} : {1}", property, value)); } return Text(builder.ToString()); }
public void RoundTrip_Properties(string propertyName, object value) { Delta <DeltaModel> delta = new Delta <DeltaModel>(); Type propertyType; Assert.True(delta.TryGetPropertyType(propertyName, out propertyType)); Assert.True(delta.TrySetPropertyValue(propertyName, value)); object retrievedValue; delta.TryGetPropertyValue(propertyName, out retrievedValue); Assert.Equal(value, retrievedValue); }
public IHttpActionResult Patch([FromODataUri] int key, Delta<Product> patch) { if (!ModelState.IsValid) { return BadRequest(ModelState); } Delta<V2VM.Product> v2Patch = new Delta<V2VM.Product>(); foreach (string name in patch.GetChangedPropertyNames()) { object value; if (patch.TryGetPropertyValue(name, out value)) { v2Patch.TrySetPropertyValue(name, value); } } var v2Product = _repository.Patch((long)key, v2Patch, Request); return Updated(Mapper.Map<Product>(v2Product)); }
public async Task<IHttpActionResult> Patch([FromODataUri] int key, Delta<Customer> patch) { object id; if (!ModelState.IsValid) { return BadRequest(ModelState); } else if (patch.TryGetPropertyValue("Id", out id) && (int)id != key) { return BadRequest("The key from the url must match the key of the entity in the body"); } Customer originalEntity = await context.Customers.FindAsync(key); if (originalEntity == null) { return NotFound(); } else { patch.Patch(originalEntity); await context.SaveChangesAsync(); } return Updated(originalEntity); }