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 static void Patch <TSourceDto, TSourceModel>(this Delta <TSourceDto> source, TSourceModel dest) where TSourceDto : class, IDto where TSourceModel : class, IEntity { if (source == null) { throw new ArgumentNullException(nameof(source)); } source.GetChangedPropertyNames() .ToList() .ForEach(changedPropName => { PropertyInfo prop = typeof(TSourceModel).GetTypeInfo().GetProperty(changedPropName); if (prop == null) { return; } object obj = null; source.TryGetPropertyValue(changedPropName, out obj); if (obj != null && !prop.PropertyType.IsInstanceOfType(obj)) { return; } prop.SetValue(dest, obj); }); }
public IHttpActionResult Put(int key, Delta <DateTimeModel> dt) { Assert.Equal(new[] { "BirthdayA", "BirthdayB" }, dt.GetChangedPropertyNames()); object value; bool success = dt.TryGetPropertyValue("BirthdayA", out value); Assert.True(success); DateTime dateTime = Assert.IsType <DateTime>(value); Assert.Equal(DateTimeKind.Unspecified, dateTime.Kind); Assert.Equal(new DateTime(2098, 12, 31, 17, 2, 3), dateTime); success = dt.TryGetPropertyValue("BirthdayB", out value); Assert.True(success); Assert.Null(value); return(Updated(dt)); }
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(int key, Delta<SpatialCustomer> customer) { // Assert part Assert.Equal(3, key); Assert.Equal(new[] {"Location"}, customer.GetChangedPropertyNames()); object value; customer.TryGetPropertyValue("Location", out value); GeographyPoint point = value as GeographyPoint; Assert.NotNull(point); Assert.Equal(7, point.Longitude); Assert.Equal(8, point.Latitude); Assert.Equal(9, point.Z); Assert.Equal(10, point.M); return Ok(); }
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); }
public IHttpActionResult Put(int key, Delta<DateAndTimeOfDayModel> dt) { Assert.Equal(new[] { "Birthday", "CreatedTime" }, dt.GetChangedPropertyNames()); // Birthday object value; bool success = dt.TryGetPropertyValue("Birthday", out value); Assert.True(success); DateTime dateTime = Assert.IsType<DateTime>(value); Assert.Equal(DateTimeKind.Unspecified, dateTime.Kind); Assert.Equal(new DateTime(2199, 1, 2), dateTime); // CreatedTime success = dt.TryGetPropertyValue("CreatedTime", out value); Assert.True(success); TimeSpan timeSpan = Assert.IsType<TimeSpan>(value); Assert.Equal(new TimeSpan(0, 14, 13, 15, 179), timeSpan); return Updated(dt); }
public IHttpActionResult Put(int key, Delta<DateTimeModel> dt) { Assert.Equal(new[] { "BirthdayA", "BirthdayB" }, dt.GetChangedPropertyNames()); object value; bool success = dt.TryGetPropertyValue("BirthdayA", out value); Assert.True(success); DateTime dateTime = Assert.IsType<DateTime>(value); Assert.Equal(DateTimeKind.Unspecified, dateTime.Kind); Assert.Equal(new DateTime(2098, 12, 31, 17, 2, 3), dateTime); success = dt.TryGetPropertyValue("BirthdayB", out value); Assert.True(success); Assert.Null(value); return Updated(dt); }