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));
        }
Пример #2
0
        public static void Patch <TDto, TEntity>(this Delta <TDto> sourceDto, TEntity destinationEntity)
            where TDto : class, IDto
            where TEntity : class, IEntity
        {
            if (sourceDto == null)
            {
                throw new ArgumentNullException(nameof(sourceDto));
            }

            sourceDto.GetChangedPropertyNames()
            .ToList()
            .ForEach(changedPropName =>
            {
                PropertyInfo prop = typeof(TEntity).GetTypeInfo().GetProperty(changedPropName);
                if (prop == null)
                {
                    return;
                }
                sourceDto.TryGetPropertyValue(changedPropName, out object obj);
                if (obj != null && !prop.PropertyType.IsInstanceOfType(obj))
                {
                    return;
                }
                prop.SetValue(destinationEntity, obj);
            });
        }
Пример #3
0
        public void CanChangeDerivedClassProperties()
        {
            // Arrange
            dynamic delta = new Delta <Base>(typeof(Derived));

            // Act
            delta.DerivedInt = 10;

            // Assert
            Assert.Equal(delta.GetChangedPropertyNames(), new[] { "DerivedInt" });
        }
Пример #4
0
        public static void IgnoreChanges <TDto>(this Delta <TDto> source, params string[] members)
            where TDto : class, IDto
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            foreach (string member in members)
            {
                (((HashSet <string>)source.GetChangedPropertyNames())).Remove(member);
            }
        }
Пример #5
0
        public static bool IsChangedProperty <TDto>(this Delta <TDto> dto, Expression <Func <TDto, object> > prop)
            where TDto : class, IDto
        {
            if (dto == null)
            {
                throw new ArgumentNullException(nameof(dto));
            }

            if (prop == null)
            {
                throw new ArgumentNullException(nameof(prop));
            }

            string memberName = ((MemberExpression)prop.Body).Member.Name;

            return(dto.GetChangedPropertyNames().Any(p => p == memberName));
        }
Пример #6
0
        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));
        }
Пример #7
0
        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] string key, Delta<Namespace> ns, [ValueProvider(typeof(CultureValueProviderFactory))] string culture = "en-US")
		{
			if (!ModelState.IsValid)
			{
				return BadRequest(ModelState);
			}
			var entity = await db.Dictionaries.FindAsync(key);
			if (entity == null)
			{
				return NotFound();
			}

			foreach (var property in ns.GetChangedPropertyNames())
			{
				if (property == nameof(entity.Description))
				{
					entity.Description = ns.GetEntity().Description;
				}
			}
			try
			{
				await db.SaveChangesAsync();
			}
			catch (DbUpdateConcurrencyException)
			{
				if (!NamespaceExists(key))
				{
					return NotFound();
				}
				else
				{
					throw;
				}
			}
			return Updated(entity);
		}
Пример #9
0
        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 async Task<IHttpActionResult> Patch([FromODataUri] int key, Delta<Translation> translation, [ValueProvider(typeof(CultureValueProviderFactory))] string culture = "en-US")
		{
			if (!ModelState.IsValid)
			{
				return BadRequest(ModelState);
			}
			var entity = (from term in db.Terms.Where(t => t.Id == key)
				join termTranslation in db.TermTranslations.Where(t => t.Culture == culture)
					on term.Id equals termTranslation.TermId
				select term).Include(t => t.Translations).SingleOrDefault();

            if (entity == null)
			{
				return NotFound();
			}

			var translationEntity =  translation.GetEntity();
			var tr = entity.Translations.FirstOrDefault();
			foreach (var property in translation.GetChangedPropertyNames())
			{
				// term
				if (property == nameof(entity.Key))
				{
					entity.Key = translationEntity.Key;
				} else if (property == nameof(entity.DictionryId))
				{
					entity.DictionryId = translationEntity.NamespaceId;
				}

				// term translation
				if (property == nameof(tr.Value))
				{
					tr.Value = translationEntity.Value;
				}
			}
			try
			{
				await db.SaveChangesAsync();
			}
			catch (DbUpdateConcurrencyException)
			{
				if (!TranslationExists(key))
				{
					return NotFound();
				}
				else
				{
					throw;
				}
			}
			return Updated(entity);
		}
Пример #11
0
        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);
        }