Beispiel #1
0
        public async Task <TariffDto> Create(TariffCreateOrUpdateDto input)
        {
            using (var unitOfWork = UnitOfWork.Begin())
            {
                foreach (var item in input.TariffItems)
                {
                    item.CreateDateTime = DateTime.Now;
                }

                //map tariff base
                var tariff = input.MapTo <Eron.Core.Entities.Financial.Order.Tariff>();

                tariff.CustomerType = CustomerType.EndUserCustomer;
                if (tariff.ImageId.HasValue)
                {
                    var image = await _fileHelper.GetFileAsync(tariff.ImageId.Value);

                    await _fileHelper.TransferToDatabaseAsync(image);
                }

                //create tariff base
                var tariffCreated = UnitOfWork.TariffRepository.Create(tariff);
                await UnitOfWork.SaveAsync();

                //handle price
                var tariffPrice = new Eron.Core.Entities.Financial.Order.TariffPrice().Create(input.TariffPrice, tariffCreated.Id);
                UnitOfWork.TariffPriceRepository.Create(tariffPrice);

                tariffCreated.TariffPrices = new List <Eron.Core.Entities.Financial.Order.TariffPrice>()
                {
                    tariffPrice
                };

                ////handle tariff items
                //foreach (var item in input.TariffItems)
                //{
                //    var tariffItem = item.MapTo<TariffItem>();
                //    tariffItem.TariffId = tariffCreated.Id;
                //    var tariffItemCreated = UnitOfWork.TariffItemRepository.Create(tariffItem);

                //    tariffCreated.TariffItems.Add(tariffItemCreated);
                //}

                //Save all dependent entites to database
                await UnitOfWork.SaveAsync();

                //Complete the transaction
                unitOfWork.Complete();
                return(tariffCreated.MapTo <TariffDto>());
            }
        }
Beispiel #2
0
        public async Task <TariffDto> Update(TariffCreateOrUpdateDto input)
        {
            if (!input.IsUpdateEntry())
            {
                throw new NoNullAllowedException("item does not exist or could not be found");
            }

            //var existingItems = UnitOfWork.TariffItemRepository.GetAllWithTariffId(input.Id.Value);
            var newItems = input.TariffItems.MapTo <List <TariffItem> >();
            var tariffEntityFromDatabase = await UnitOfWork.TariffRepository.GetByIdAsync(input.Id);

            var tariff = input.MapTo <Eron.Core.Entities.Financial.Order.Tariff>();

            #region Handle Items

            ////delete old items which do not exist any more
            //foreach (var item in existingItems.Where(x => !newItems.Contains(x)))
            //{
            //    UnitOfWork.TariffItemRepository.Delete(item);
            //}

            ////add new Items which did not exist before
            //foreach (var item in newItems.Where(x => !existingItems.Contains(x)))
            //{
            //    item.TariffId = input.Id.Value;
            //    UnitOfWork.TariffItemRepository.Create(item);
            //}

            #endregion

            #region Handle Price

            //fetch existing valid price
            var existingPrice = UnitOfWork.TariffPriceRepository.GetValidByTariffId(input.Id.Value);
            //check if current price is up to date
            var newPrice = new Eron.Core.Entities.Financial.Order.TariffPrice();
            if (existingPrice.Price != input.TariffPrice)
            {
                existingPrice.IsValid = false;

                //update old tariff price
                UnitOfWork.TariffPriceRepository.Update(existingPrice);

                //create new tariff price
                newPrice = new Eron.Core.Entities.Financial.Order.TariffPrice().Create(input.TariffPrice, input.Id.Value);
                newPrice = UnitOfWork.TariffPriceRepository.Create(newPrice);
                await UnitOfWork.SaveAsync();
            }

            #endregion

            #region Handle Images

            if (tariff.ImageId.HasValue && tariff.ImageId != tariffEntityFromDatabase.ImageId)
            {
                var newImage = await _fileHelper.GetFileAsync(tariff.ImageId.Value);

                if (tariffEntityFromDatabase.ImageId.HasValue)
                {
                    await _fileHelper.DeleteFileAsync(tariffEntityFromDatabase.ImageId.Value);
                }
                await _fileHelper.TransferToDatabaseAsync(newImage);
            }

            #endregion

            var tariffPriceList = UnitOfWork.TariffPriceRepository.GetAllWithTariffId(tariff.Id);

            tariff.TariffPrices = tariffPriceList;
            Mapper.Map(tariff, tariffEntityFromDatabase);

            UnitOfWork.TariffRepository.Update(tariffEntityFromDatabase);

            await UnitOfWork.SaveAsync();

            return(input.MapTo <TariffDto>());
        }