示例#1
0
        public async Task <ProductDto> Update(ProductCreateOrUpdateDto input)
        {
            if (!input.IsUpdateEntry())
            {
                throw new EntityNotFoundException();
            }

            using (var unitOfWork = UnitOfWork.Begin())
            {
                #region Handle Product Property Names

                var allPropertyNames = (await UnitOfWork.ProductPropertyNameRepository.GetAllAsync()).ToList();

                foreach (var item in input.Properties)
                {
                    var productPropertyNameEntity = new ProductPropertyName(item.PropertyName);
                    if (allPropertyNames.Any(x => x.Name == item.PropertyName))
                    {
                        item.ProductPropertyNameId = allPropertyNames.Find(x => x.Name == item.PropertyName).Id;
                    }
                    else
                    {
                        var newProductPropertyName = UnitOfWork.ProductPropertyNameRepository.Create(productPropertyNameEntity);
                        await UnitOfWork.SaveAsync();

                        item.ProductPropertyNameId = newProductPropertyName.Id;
                    }

                    item.ProductId = input.Id;
                }

                #endregion

                #region Handle Main Product

                var updateEntity = input.MapTo <Eron.Core.Entities.Financial.Shop.Product>();

                #endregion

                #region Handle ProductProperties and PropertyName

                #endregion

                UnitOfWork.ProductRepository.Update(updateEntity);
                await UnitOfWork.SaveAsync();

                #region Handle Price

                var currentPrice =
                    await UnitOfWork.ProductPriceRepository.GetActiveForProduct(updateEntity.Id);

                if (currentPrice.Price != updateEntity.Price)
                {
                    currentPrice.IsValid = false;
                    var newPrice =
                        new Eron.Core.Entities.Financial.Shop.ProductPrice(updateEntity.Id, updateEntity.Price);
                    UnitOfWork.ProductPriceRepository.Update(currentPrice);
                    UnitOfWork.ProductPriceRepository.Create(newPrice);
                    await UnitOfWork.SaveAsync();
                }

                #endregion

                #region Properties

                //foreach (var item in updateEntity.Properties)
                //{
                //    var propertyEntity = item.MapTo<ProductProperty>();
                //    if (item.Id == 0)
                //    {
                //        UnitOfWork.ProductPropertyRepository.Create(propertyEntity);
                //    }
                //    else
                //    {
                //        _productPropertyRepository.Update(propertyEntity);
                //    }
                //}

                //await UnitOfWork.SaveAsync();

                #endregion Properties

                unitOfWork.Complete();

                return(updateEntity.MapTo <ProductDto>());
            }
        }
        public async Task <IHttpActionResult> Put(ProductCreateOrUpdateDto input)
        {
            var result = await _service.Update(input);

            return(Ok(result));
        }
示例#3
0
        public async Task <ProductDto> Create(ProductCreateOrUpdateDto input)
        {
            using (var unitOfWork = UnitOfWork.Begin())
            {
                #region Handle Product Property Names

                var allPropertyNames = (await UnitOfWork.ProductPropertyNameRepository.GetAllAsync()).ToList();

                foreach (var item in input.Properties)
                {
                    var productPropertyNameEntity = new ProductPropertyName(item.PropertyName);
                    if (allPropertyNames.Any(x => x.Name == item.PropertyName))
                    {
                        item.ProductPropertyNameId = allPropertyNames.Find(x => x.Name == item.PropertyName).Id;
                    }
                    else
                    {
                        var newProductPropertyName = UnitOfWork.ProductPropertyNameRepository.Create(productPropertyNameEntity);
                        await UnitOfWork.SaveAsync();

                        item.ProductPropertyNameId = newProductPropertyName.Id;
                    }
                }

                #endregion

                #region Handle Main Product

                var entity = input.MapTo <Eron.Core.Entities.Financial.Shop.Product>();
                entity = UnitOfWork.ProductRepository.Create(entity);

                await UnitOfWork.SaveAsync();

                var entityId = entity.Id;

                foreach (var image in input.Images)
                {
                    var eronFile = await _fileHelper.GetFileAsync(image);

                    eronFile.ProductId = entityId;
                    await _fileHelper.TransferToDatabaseAsync(eronFile);
                }

                await UnitOfWork.SaveAsync();

                #endregion

                #region Handle Price

                var price = new Eron.Core.Entities.Financial.Shop.ProductPrice(entityId, input.ProductPrice);
                UnitOfWork.ProductPriceRepository.Create(price);
                await UnitOfWork.SaveAsync();

                #endregion

                var entityDto = entity.MapTo <ProductDto>();
                entityDto.Images     = input.Images;
                entityDto.Properties = input.Properties.MapTo <List <ProductPropertyDto> >();
                entityDto.Price      = input.ProductPrice;

                unitOfWork.Complete();

                return(entityDto);
            }
        }