public virtual IEnumerable <ReminderModel> Execute(int userId) { // this results in a SELECT N+1 scenario try { var vehicles = _vehicleRepository.GetVehicles(userId); var reminders = (from vehicle in vehicles let stats = CalculateStatistics.Calculate(vehicle.Fillups) let odometer = stats.Odometer from reminder in _reminderRepository.GetRemindersForVehicle(vehicle.VehicleId) select new { viewmodel = ToViewModel(reminder), odometer }) .ToList(); foreach (var item in reminders) { item.viewmodel.UpdateLastVehicleOdometer(item.odometer); } var overdueReminders = reminders .Select(x => x.viewmodel) .Where(f => !f.IsFulfilled && f.IsOverdue) .OrderBy(f => f.DueDistance) .ThenBy(f => f.DueDate) .ToList(); return(overdueReminders.ToList()); } catch (InvalidOperationException ex) { throw new BusinessServicesException(Resources.UnableToRetrieveOverdueReminders, ex); } }
public virtual FleetStatistics Execute(int userId) { var vehicles = _vehicleRepository.GetVehicles(userId); var stats = from vehicle in vehicles where vehicle.Fillups.Any() let statistics = CalculateStatistics.Calculate(vehicle.Fillups, includeFirst: false) select statistics; return(new FleetStatistics(stats)); }
public virtual IEnumerable <ImminentReminderModel> Execute(int userId, DateTime dueDate, int selectedVehicle = 0) { var vehicles = _vehicleRepository.GetVehicles(userId); var reminders = new List <AggregatedData>(); //NOTE: the structure of the data forces us into a SELECT N+1 scenario foreach (var vehicle in vehicles) { var fillups = _fillupRepository.GetFillups(vehicle.VehicleId); var stats = CalculateStatistics.Calculate(fillups); var odometer = stats.Odometer ?? 0; var overdue = _reminderRepository .GetOverdueReminders(vehicle.VehicleId, dueDate, odometer) .Select(r => new AggregatedData { IsOverdue = true, Reminder = r, Vehicle = vehicle }) .ToList(); var end = dueDate.AddDays(UpcomingReminderDaysTolerance).Date; var start = dueDate.Date; var upcoming = _reminderRepository .GetUpcomingReminders(vehicle.VehicleId, start, end, odometer, UpcomingReminderOdometerTolerance) .Select(r => new AggregatedData { IsOverdue = false, Reminder = r, Vehicle = vehicle }) // we need to exclude reminders that qualify as both overdue and upcoming // performing this check in memory is less than desirable .Where(r => !overdue.Any(o => o.Reminder.ReminderId == r.Reminder.ReminderId)) .ToList(); reminders.AddRange(overdue); reminders.AddRange(upcoming); } var result = reminders .Select(x => new ImminentReminderModel(x.Vehicle, x.Reminder, x.IsOverdue)) .ToList(); result.Sort(ImminentReminderModel.CompareImminentReminders); return(result); }
public virtual VehicleModel Execute(int userId, int vehicleId) { Vehicle vehicle = _vehicleRepository.GetVehicle(userId, vehicleId); if (vehicle == null) { return(null); } var fillups = _fillupRepository.GetFillups(vehicle.VehicleId).OrderBy(f => f.Odometer); var statistics = CalculateStatistics.Calculate(fillups); return(new VehicleModel(vehicle, statistics)); }
public virtual IEnumerable <VehicleModel> Execute(int userId) { IEnumerable <Vehicle> vehicleData = null; vehicleData = _vehicleRepository.GetVehicles(userId); var vehicles = from vehicle in vehicleData let fillups = _fillupRepository.GetFillups(vehicle.VehicleId).OrderBy(f => f.Odometer) let statistics = CalculateStatistics.Calculate(fillups, includeFirst: false) orderby vehicle.SortOrder select new VehicleModel(vehicle, statistics); return(vehicles); }
public virtual FleetStatistics Execute(int userId) { var vehicles = _vehicleRepository.GetVehicles(userId); var stats = from vehicle in vehicles let fillups = _fillupRepository.GetFillups(vehicle.VehicleId) where fillups.Any() let statistics = CalculateStatistics.Calculate(fillups, includeFirst: false) select statistics; // These statistic calculations are for illustrative purposes only. // If you were to build this application for real, you probably want to calculate this // data more accurately and cache the results. return(new FleetStatistics(stats)); }
public virtual VehicleModel Execute(int userId, int vehicleId) { Vehicle vehicle; try { vehicle = _vehicleRepository.GetVehicle(userId, vehicleId); } catch (InvalidOperationException) { return(null); } var fillups = vehicle.Fillups.OrderBy(f => f.Odometer).Skip(1); var statistics = CalculateStatistics.Calculate(fillups); return(new VehicleModel(vehicle, statistics)); }
public virtual IEnumerable <VehicleModel> Execute(int userId) { IEnumerable <Vehicle> vehicleData = null; try { vehicleData = _vehicleRepository.GetVehicles(userId); } catch (InvalidOperationException e) { throw new BusinessServicesException("Unable to retrieve vehicle from database.", e); } var vehicles = from vehicle in vehicleData let fillups = vehicle.Fillups.OrderBy(f => f.Odometer) let statistics = CalculateStatistics.Calculate(fillups, includeFirst: false) orderby vehicle.SortOrder select new VehicleModel(vehicle, statistics); return(vehicles); }
public virtual IEnumerable <ValidationResult> Execute(int userId, ICreateReminderCommand reminder) { var foundVehicle = _vehicleRepository.GetVehicle(userId, reminder.VehicleId); if (foundVehicle == null) { yield return(new ValidationResult(Resources.VehicleNotFound)); } else { var stats = CalculateStatistics.Calculate(foundVehicle.Fillups); var odometer = stats.Odometer; if ((reminder.DueDistance.HasValue) && (reminder.DueDistance.Value <= odometer)) { yield return(new ValidationResult( "DueDistance", string.Format(CultureInfo.CurrentUICulture, Resources.DueDistanceNotGreaterThanOdometer, odometer))); } } }