Example #1
0
        private void SeedFuelEntries(int count, bool randomDate = false, bool includeFuelEntryTypes = false)
        {
            var           random        = new Random();
            FuelEntryType fuelEntryType = null;

            if (includeFuelEntryTypes)
            {
                this.SeedFuelEntryTypes();
                fuelEntryType = this.dbContext.FuelEntryTypes.First(fet => fet.Id == SampleFuelEntryTypeId);
            }

            var entriesToSeed = new List <FuelEntry>();

            for (int i = 1; i <= count; i++)
            {
                var sampleFuelEntry = new FuelEntry
                {
                    Id              = i,
                    DateCreated     = SampleDateCreated,
                    FuelEntryTypeId = SampleFuelEntryTypeId,
                    FuelEntryType   = fuelEntryType,
                    VehicleId       = SampleVehicleId
                };

                if (randomDate)
                {
                    sampleFuelEntry.DateCreated = SampleDateCreated.AddDays(random.Next(1, 1000));
                }

                entriesToSeed.Add(sampleFuelEntry);
            }

            this.dbContext.AddRange(entriesToSeed);
            this.dbContext.SaveChanges();
        }
Example #2
0
        private async Task RemoveOldMappingEntities(FuelEntry fuelEntry)
        {
            var dbEntryRoutes = await this.db.FuelEntryRouteTypes.Where(fert => fert.FuelEntryId == fuelEntry.Id).ToListAsync();

            var dbEntryExtras = await this.db.FuelEntryExtraFuelConsumers.Where(feex => feex.FuelEntryId == fuelEntry.Id).ToListAsync();

            this.db.RemoveRange(dbEntryRoutes);
            this.db.RemoveRange(dbEntryExtras);
            await this.db.SaveChangesAsync();
        }
Example #3
0
        private async Task SetAverageConsumption(int modelOdometer, FuelEntry fuelEntry, Vehicle vehicle)
        {
            var firstFuelingType = await this.db.FuelEntryTypes.FirstAsync(fet => fet.Name == FirstFueling);

            var fullType = await this.db.FuelEntryTypes.FirstAsync(fet => fet.Name == FullFueling);

            var fuelEntries = vehicle.FuelEntries.Where(fe => fe.DateCreated < fuelEntry.DateCreated).OrderBy(fe => fe.DateCreated);

            if (fuelEntries.Any())
            {
                var lastOdometer = fuelEntries.Last().Odometer;
                fuelEntry.TripOdometer = modelOdometer - lastOdometer;
                if (fuelEntry.FuelEntryTypeId == firstFuelingType.Id)
                {
                    fuelEntry.FuelEntryTypeId = fullType.Id;
                }

                if (fuelEntry.FuelEntryTypeId == fullType.Id)
                {
                    var lastFullFueling = fuelEntries.LastOrDefault(fe => fe.FuelEntryTypeId == fullType.Id);
                    if (lastFullFueling == null)
                    {
                        lastFullFueling = fuelEntries.LastOrDefault(fe => fe.FuelEntryTypeId == firstFuelingType.Id);
                    }

                    var distance = fuelEntry.Odometer - lastFullFueling?.Odometer;

                    var indexOfLastFullFueling   = Array.LastIndexOf(fuelEntries.OrderBy(fe => fe.DateCreated).ToArray(), lastFullFueling);
                    var sumOfValidFuelQuantities = fuelEntries.Skip(indexOfLastFullFueling + 1).Sum(fe => fe.FuelQuantity) + fuelEntry.FuelQuantity;
                    if (distance != null)
                    {
                        fuelEntry.Average = (sumOfValidFuelQuantities / distance.Value) * 100.0;
                    }
                }
            }
            else
            {
                fuelEntry.FuelEntryTypeId = firstFuelingType.Id;
            }
        }
Example #4
0
        public async Task <bool> UpdateAsync(FuelEntry fuelEntry)
        {
            if (fuelEntry == null)
            {
                return(false);
            }

            var vehicle = await this.db
                          .Vehicles
                          .Where(v => v.Id == fuelEntry.VehicleId && !v.IsDeleted)
                          .AsNoTracking()
                          .Include(v => v.FuelEntries)
                          .FirstOrDefaultAsync();

            if (vehicle == null)
            {
                return(false);
            }

            await this.RemoveOldMappingEntities(fuelEntry);

            await this.SetAverageConsumption(fuelEntry.Odometer, fuelEntry, vehicle);

            try
            {
                this.ValidateEntityState(fuelEntry);

                this.db.FuelEntries.Update(fuelEntry);
                await this.db.SaveChangesAsync();

                await this.UpdateStatsOnFuelEntryChangedAsync(fuelEntry.VehicleId);

                return(true);
            }
            catch
            {
                return(false);
            }
        }