예제 #1
0
        public new async Task <ServiceResult <PriceTresholdBaseDto, PrcAdminCreateInfo> > CreateAsync(PriceTresholdBaseDto entity)
        {
            var createInfo = new PrcAdminCreateInfo();

            var recoverItem = await _repository.FirstOrDefaultAsync(x => x.IsDeleted && x.MinCharges == entity.MinCharges && x.PricePerCharge == entity.PricePerCharge);

            if (recoverItem != null)
            {
                recoverItem.IsDeleted = false;
                _repository.Edit(recoverItem);
                await _unitOfWork.CommitAsync();

                createInfo.Recovered = true;
                return(ServiceResult <PriceTresholdBaseDto, PrcAdminCreateInfo> .Success(_mapper.Map <PriceTresholdBaseDto>(recoverItem), createInfo));
            }

            var conflictingItems = await _repository.GetAllAsync(x => !x.IsDeleted && (x.MinCharges == entity.MinCharges || x.PricePerCharge == entity.PricePerCharge));

            if (conflictingItems.Any())
            {
                if (entity.MinCharges == 0)
                {
                    var currentDefault = await _repository.FirstOrDefaultAsync(x => !x.IsDeleted && x.MinCharges == 0);

                    currentDefault.IsDeleted = true;

                    foreach (var conflictingItem in conflictingItems)
                    {
                        conflictingItem.IsDeleted = true;
                        _repository.Edit(conflictingItem);
                    }

                    createInfo.ReplacedDefault = true;
                }
                else
                {
                    return(ServiceResult <PriceTresholdBaseDto, PrcAdminCreateInfo>
                           .Failure($"Podane wartości kolidują z już istniejącym przedziałem (min. wyjazdy: {conflictingItems.First().MinCharges}, cena za szt.: {conflictingItems.First().PricePerCharge.ToString("##.00")})"));
                }
            }

            var newPrc = _repository.Add(_mapper.Map <PriceTreshold>(entity));
            await _unitOfWork.CommitAsync();

            return(ServiceResult <PriceTresholdBaseDto, PrcAdminCreateInfo> .Success(_mapper.Map <PriceTresholdBaseDto>(newPrc), createInfo));
        }
        public void UpdateEntity_ThenResultIsValid()
        {
            //Before
            var entites = _repository.GetAll().ToList();
            var lastEntity = entites.Last();

            //Act
            lastEntity.MinCharges = 999;
            _repository.Edit(lastEntity);
            _uow.Commit();

            //Then
            var result = _repository.GetAll().ToList();
            result.Count.Should().Be.EqualTo(entites.Count);
            lastEntity.MinCharges.Should().Be.EqualTo(999);
        }