Ejemplo n.º 1
0
        public async Task Reset(DateTime from, DateTime to)
        {
            // Reset based on day
            var days = await dayRepository.GetDays(from, to);

            var pings = new List <Ping>();

            foreach (var day in days)
            {
                pings = await pingRepository.GetPings(day);
                await ResetPings(pings);

                if (day.RideId != null)
                {
                    var ride = await rideRepository.GetByIdFull((int)day.RideId);
                    await ResetPings(ride.Pings.ToList());

                    rideRepository.Delete(day.RideId);
                    await rideRepository.SaveAsync();
                }

                dayRepository.Delete(day.Id);
            }

            await dayRepository.SaveAsync();

            // Reset the remaining pings
            pings = await pingRepository.GetBetweenDates(from, to);
            await ResetPings(pings);

            cache.ClearCache();
        }
Ejemplo n.º 2
0
        public async Task DeleteDay(int id, bool removePings = false)
        {
            var day = await GetByIdAsync(id);

            var pings = new List <Ping>();

            // Find the previous day
            var previousDay = await GetPrevious(day);

            // Merge with the previous day
            if (previousDay is Day)
            {
                // Up the end time of the previous record
                previousDay.TimeTo = day.TimeTo;
                Update(previousDay);

                var rideId     = previousDay.RideId;
                var locationId = previousDay.LocationId;
                var dayId      = previousDay.Id;

                // Update the pings of the to be deleted day to the location of the previous day
                pings = await pingRepository.GetPings(day);

                foreach (var ping in pings)
                {
                    if (!removePings)
                    {
                        ping.RideId     = rideId;
                        ping.LocationId = locationId;
                        ping.DayId      = dayId;
                        pingRepository.Update(ping);
                    }
                    else
                    {
                        pingRepository.Delete(ping.Id);
                    }
                }

                await SaveAsync();

                // Check if the previous record is a ride. This also needs to be extended
                if (previousDay.Ride is Ride)
                {
                    previousDay.Ride.TimeTo = day.TimeTo;
                    rideRepository.Update(previousDay.Ride);
                    var nextDay = await GetNext(day);

                    // Is the next record also a ride, Merge them together.
                    if (nextDay is Day && nextDay.Ride is Ride)
                    {
                        previousDay.TimeTo                = nextDay.TimeTo;
                        previousDay.Ride.TimeTo           = nextDay.TimeTo;
                        previousDay.Ride.DistanceInMeters = null;

                        pings = await pingRepository.GetPings(nextDay);

                        foreach (var ping in pings)
                        {
                            ping.RideId     = rideId;
                            ping.LocationId = locationId;
                            ping.DayId      = dayId;
                            pingRepository.Update(ping);
                        }

                        rideRepository.Delete(nextDay.Ride.Id);
                        Delete(nextDay.Id);
                    }

                    previousDay.Ride.ResetDistance();
                    rideRepository.Update(previousDay.Ride);
                }

                Update(previousDay);
                await SaveAsync();

                if (day.Ride is Ride)
                {
                    // The previous merge actions removed all pings.
                    // But in case any pings didn't get moved double check by selecting with the ride instead of the day.
                    pings = await pingRepository.GetPings(day.Ride);

                    foreach (var ping in pings)
                    {
                        if (!removePings)
                        {
                            ping.RideId     = rideId;
                            ping.LocationId = locationId;
                            ping.DayId      = dayId;
                            pingRepository.Update(ping);
                        }
                        else
                        {
                            pingRepository.Delete(ping.Id);
                        }
                    }

                    rideRepository.Delete(day.Ride.Id);
                }
            }

            Delete(day.Id);
            await SaveAsync();
        }