예제 #1
0
        private SpecificationPropertyGroupList GetSpecificationPropertyGroups(Product product)
        {
            SpecificationPropertyGroupList specificationPropertyGroups = new SpecificationPropertyGroupList();

            foreach (Specification specification in product.Specifications)
            {
                foreach (SpecificationPropertyValue specificationPropertyValue in specification.PropertyValues)
                {
                    PropertyValueDto specificationPropertyValueDto = specificationPropertyValue.MapTo <PropertyValueDto>();

                    if (specificationPropertyGroups.HasSpecificationProperty(specificationPropertyValue.SpecificationProperty))
                    {
                        var specificationPropertyGroup = specificationPropertyGroups.GetFromSpecificationProperty(specificationPropertyValue.SpecificationProperty);

                        if (!HasPropertyValueDto(specificationPropertyGroup.SpecificationPropertyValues, specificationPropertyValueDto))
                        {
                            specificationPropertyGroup.SpecificationPropertyValues.Add(specificationPropertyValueDto);
                        }
                    }
                    else
                    {
                        var specificationPropertyGroup = new SpecificationPropertyGroup()
                        {
                            SpecificationProperty       = specificationPropertyValue.SpecificationProperty.MapTo <SpecificationPropertyDto>(),
                            SpecificationPropertyValues = new List <PropertyValueDto>()
                        };
                        specificationPropertyGroup.SpecificationPropertyValues.Add(specificationPropertyValueDto);
                        specificationPropertyGroups.Add(specificationPropertyGroup);
                    }
                }
            }
            return(specificationPropertyGroups);
        }
예제 #2
0
        public virtual async Task <TSelectDto> UpdateProperty(TKey id, PropertyValueDto dto, CancellationToken cancellationToken = default)
        {
            var entityType = typeof(TEntity);
            var property   = entityType.GetProperty(dto.PropertyName);

            if (property == null)
            {
                throw new NotFoundException($"property {dto.PropertyName} not gound in entity : {typeof(TEntity).Name}");
            }

            var model = await repository.GetByIdAsync(cancellationToken, id);

            if (model == null)
            {
                throw new NotFoundException($"not found {typeof(TEntity).Name} entity to PKey(Id) : '{id}'");
            }

            if (dto.PropertyValue != null)
            {
                if (entityType.GetProperty(dto.PropertyName).PropertyType == typeof(Guid))
                {
                    entityType.GetProperty(dto.PropertyName).SetValue(model, new Guid(dto.PropertyValue.ToString()));
                }
                else if (entityType.GetProperty(dto.PropertyName).PropertyType == typeof(Guid?))
                {
                    entityType.GetProperty(dto.PropertyName).SetValue(model, new Guid(dto.PropertyValue.ToString()));
                }
                else
                {
                    var value = Convert.ChangeType(dto.PropertyValue, entityType.GetProperty(dto.PropertyName).PropertyType, CultureInfo.InvariantCulture);
                    entityType.GetProperty(dto.PropertyName).SetValue(model, value);
                }
            }
            else
            {
                //todo check property support null value
                var  propertyType = entityType.GetProperty(dto.PropertyName).PropertyType;
                bool canBeNull    = !propertyType.IsValueType || (Nullable.GetUnderlyingType(propertyType) != null);
                if (canBeNull)
                {
                    entityType.GetProperty(dto.PropertyName).SetValue(model, null);
                }
                else
                {
                    //todo exception
                }
            }

            await repository.UpdateAsync(model, cancellationToken);

            var resultDto = await repository.TableNoTracking.ProjectTo <TSelectDto>(mapper.ConfigurationProvider)
                            .SingleOrDefaultAsync(p => p.Id.Equals(model.Id), cancellationToken);

            return(resultDto);
        }
예제 #3
0
 private bool HasPropertyValueDto(List <PropertyValueDto> list, PropertyValueDto propertyValueDto)
 {
     foreach (PropertyValueDto propertyValueDtoItem in list)
     {
         if (propertyValueDtoItem.Value == propertyValueDto.Value)
         {
             return(true);
         }
     }
     return(false);
 }
예제 #4
0
        public virtual async Task <ApiResult <TSelectDto> > UpdateProperty(TKey id, PropertyValueDto dto, CancellationToken cancellationToken)
        {
            if (dto == null)
            {
                throw new NotFoundException("'dto' can't be NULL");
            }

            var resultDto = await serviceRepository.UpdateProperty(id, dto, cancellationToken);

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

            return(resultDto);
        }