public bool AddRefuling(NewRefulingVm model, NewCarHistoryVm carHistoryVm)
        {
            if (model is null)
            {
                return(false);
            }
            else
            {
                var refuelingModelToAdd  = _mapper.Map <Refueling>(model);
                var carHistoryModelToAdd = _mapper.Map <CarHistory>(carHistoryVm);

                refuelingModelToAdd.IsActive = true;
                refuelingModelToAdd.BurningFuelPerOneHundredKilometers =
                    VehicleServiceHelpers.ReturnBurningPerOneHoundredKilometers(model);

                refuelingModelToAdd.FuelPrice = decimal.Multiply(refuelingModelToAdd.AmountOfFuel, refuelingModelToAdd.PriceForOneUnit);
                string userId = GetUserIdByVehicleId(refuelingModelToAdd.VehicleId);
                bool   refuelingSucessfullyAdded = _vehicleRepository.AddRefueling(refuelingModelToAdd, userId, carHistoryModelToAdd);

                if (refuelingSucessfullyAdded == true)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
        public void ShouldBeReturnBurnigFuelPerOneHounderKilometers()
        {
            //Arrange
            //Correct data
            var firstModel = new NewRefulingVm()
            {
                LastMeters     = 100,
                MeterStatus    = 550,
                AmountOfFuel   = 30,
                IsRefulingFull = true
            };
            //Kilometers before refueling is greather than actually meter status
            var secondModel = new NewRefulingVm()
            {
                LastMeters     = 1200,
                MeterStatus    = 1190,
                IsRefulingFull = true
            };
            //Kilometers before refueling is equal with actually meter status
            var thirdModel = new NewRefulingVm()
            {
                LastMeters     = 1200,
                MeterStatus    = 1200,
                IsRefulingFull = true
            };
            var fourthModel = new NewRefulingVm()
            {
                LastMeters     = 1200,
                MeterStatus    = 1390,
                IsRefulingFull = true
            };
            //Act
            decimal burningFuelPerOneHoundredKilometersIsRefuelingFullTrue = VehicleServiceHelpers
                                                                             .ReturnBurningPerOneHoundredKilometers(firstModel);

            decimal burningFuelPerOneHoundredKilometersIsRefuelingFullFalse = VehicleServiceHelpers
                                                                              .ReturnBurningPerOneHoundredKilometers(secondModel);

            decimal burningFuelPerOneHoundredKilometersIsRefuelingAfterRefuelingIsLessThanBefore = VehicleServiceHelpers
                                                                                                   .ReturnBurningPerOneHoundredKilometers(thirdModel);

            decimal burningFuelPerOneHoundredKilometersIsRefuelingAfterRefuelingIsEqualThanBefore = VehicleServiceHelpers
                                                                                                    .ReturnBurningPerOneHoundredKilometers(fourthModel);

            //Assert
            burningFuelPerOneHoundredKilometersIsRefuelingFullTrue.Should().Be(6.6667M);
            burningFuelPerOneHoundredKilometersIsRefuelingFullTrue.Should().BePositive();
            burningFuelPerOneHoundredKilometersIsRefuelingFullTrue.Should().NotBe(0M);

            burningFuelPerOneHoundredKilometersIsRefuelingFullFalse.Should().Be(0M);

            burningFuelPerOneHoundredKilometersIsRefuelingAfterRefuelingIsLessThanBefore.Should().Be(0);

            burningFuelPerOneHoundredKilometersIsRefuelingAfterRefuelingIsEqualThanBefore.Should().Be(0);
        }
        public bool DeleteRefueling(string refuelingId)
        {
            bool isRemove = false;

            if (!string.IsNullOrEmpty(refuelingId))
            {
                var vehicleHistory = _vehicleRepository.GetAllVehicleHistory()
                                     .FirstOrDefault(x => x.RefulingRef.Equals(refuelingId));
                var vehicle = _vehicleRepository.GetVehicleById(vehicleHistory.VehicleId);
                var refuelingByVehicleId = GetVehicleRefuelingById(vehicleHistory.VehicleId);
                if (refuelingByVehicleId.Count > 1)
                {
                    vehicle.Millage = VehicleServiceHelpers.GetMillageBeforeEvent(refuelingByVehicleId);
                }
                vehicleHistory.IsActive = false;

                isRemove = _vehicleRepository.DeleteRefueling(refuelingId, vehicleHistory, vehicle);
                return(isRemove);
            }
            return(isRemove);
        }
        public void ShouldReturnMillageBeforeEvent()
        {
            //Arrange
            var firstRefueling = new RefuelDetailsVm()
            {
                Id = "1", IsActive = true, VehicleId = 1, MeterStatus = 1500
            };
            var secondRefueling = new RefuelDetailsVm()
            {
                Id = "2", IsActive = true, VehicleId = 1, MeterStatus = 1200
            };
            var thirdRefueling = new RefuelDetailsVm()
            {
                Id = "3", IsActive = true, VehicleId = 1, MeterStatus = 3500
            };
            var forthRefueling = new RefuelDetailsVm()
            {
                Id = "2", IsActive = true, VehicleId = 1, MeterStatus = 1430
            };
            var listRefuelings      = new List <RefuelDetailsVm>();
            var listRefuelingsEmpty = new List <RefuelDetailsVm>();

            listRefuelings.Add(firstRefueling);
            listRefuelings.Add(secondRefueling);
            listRefuelings.Add(thirdRefueling);
            listRefuelings.Add(forthRefueling);
            //Act
            var millageBeforeEvent     = VehicleServiceHelpers.GetMillageBeforeEvent(listRefuelings);
            var millageBeforeEventZero = VehicleServiceHelpers.GetMillageBeforeEvent(listRefuelingsEmpty);

            //Assert
            millageBeforeEvent.Should().Be(1500);
            millageBeforeEvent.Should().BePositive();
            millageBeforeEvent.Should().NotBe(0);
            millageBeforeEventZero.Should().Be(0);
        }
        public bool EditRefueling(NewRefulingVm refueling)
        {
            if (refueling is null)
            {
                return(false);
            }

            var refuelingForUpdate = _mapper.Map <Refueling>(refueling);

            refuelingForUpdate.BurningFuelPerOneHundredKilometers = VehicleServiceHelpers.ReturnBurningPerOneHoundredKilometers(refueling);

            refuelingForUpdate.FuelPrice        = decimal.Multiply(refuelingForUpdate.AmountOfFuel, refuelingForUpdate.PriceForOneUnit);
            refuelingForUpdate.ModifiedDateTime = DateTime.UtcNow;
            bool isCorrectEdit = _vehicleRepository.EditRefueling(refuelingForUpdate);

            if (isCorrectEdit == true)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }