public void Update(Reminder updatedReminder)
 {
     this.GetDbSet<Reminder>().Attach(updatedReminder);
     this.SetEntityState(updatedReminder, updatedReminder.VehicleId == 0
                                              ? EntityState.Added
                                              : EntityState.Modified);
     this.UnitOfWork.SaveChanges();
 }
        public void WhenCreatingReminder_ThenPersists()
        {
            var repository = new ReminderRepository(this.GetUnitOfWork());

            Reminder reminder = new Reminder()
                                    {
                                        DueDate = DateTime.UtcNow.AddDays(30),
                                        DueDistance = 1000,
                                        Title = "Test Reminder"
                                    };

            repository.Create(this.defaultVehicle.VehicleId, reminder);

            var repositoryForVerification = new ReminderRepository(this.GetUnitOfWork());
            var returnedReminder =
                repositoryForVerification.GetRemindersForVehicle(this.defaultVehicle.VehicleId).First();

            Assert.NotNull(returnedReminder);
            Assert.Equal("Test Reminder", returnedReminder.Title);
        }
        private void CreateReminders(Vehicle vehicle)
        {
            FillupEntry lastFillup = vehicle.Fillups.OrderByDescending(f => f.Date).FirstOrDefault();
            if (lastFillup == null)
            {
                return;
            }

            Reminder reminder;

            // create overdue by mileage reminder
            reminder = new Reminder
                           {
                               DueDate = null,
                               DueDistance = lastFillup.Odometer - 10,
                               IsFulfilled = false,
                               Remarks = "Check air filter when oil is changed",
                               Title = "Oil Change",
                               VehicleId = vehicle.VehicleId
                           };
            vehicle.Reminders.Add(reminder);

            // create overdue by date reminder
            reminder = new Reminder
                           {
                               DueDate = lastFillup.Date.AddDays(-10),
                               DueDistance = null,
                               IsFulfilled = false,
                               Remarks = "Check condition of the wipers",
                               Title = "Check Wiper Fluid",
                               VehicleId = vehicle.VehicleId
                           };
            vehicle.Reminders.Add(reminder);

            // create to be done soon by mileage reminder
            reminder = new Reminder
                           {DueDate = null, DueDistance = lastFillup.Odometer + 400, IsFulfilled = false, Remarks = "Check air pressure", Title = "Rotate Tires", VehicleId = vehicle.VehicleId};
            vehicle.Reminders.Add(reminder);

            // create to be done soon by date reminder
            reminder = new Reminder {DueDate = DateTime.Now.AddDays(+10), DueDistance = null, IsFulfilled = false, Remarks = "Check air freshener", Title = "Vacuum Car", VehicleId = vehicle.VehicleId};
            vehicle.Reminders.Add(reminder);
        }
 public ReminderSummaryViewModel(Reminder reminder, bool isOvedue)
 {
     _reminder = reminder;
     _isOvedue = isOvedue;
 }
        private static IEnumerable<Reminder> GetListOfReminders()
        {
            var currentDate = DateTime.UtcNow;
            var reminders = new List<Reminder>
                                {
                                    new Reminder
                                        {
                                            ReminderId = 1,
                                            DueDate = currentDate.AddDays(300),
                                            DueDistance = CurrentOdometer + 10000,
                                            Title = "Future Reminder"
                                        },
                                    new Reminder
                                        {
                                            ReminderId = 2,
                                            DueDate = currentDate.AddDays(15),
                                            DueDistance = CurrentOdometer + 10000,
                                            Title = "Upcoming by Date Reminder"
                                        },
                                    new Reminder
                                        {
                                            ReminderId = 3,
                                            DueDate = currentDate.AddDays(300),
                                            DueDistance = CurrentOdometer + 499,
                                            Title = "Upcoming by Mileage Reminder"
                                        },
                                    new Reminder
                                        {
                                            ReminderId = 4,
                                            DueDate = currentDate.AddDays(-5),
                                            DueDistance = CurrentOdometer + 10000,
                                            Title = "Overdue By Date"
                                        },
                                    new Reminder
                                        {
                                            ReminderId = 5,
                                            DueDate = currentDate.AddDays(30),
                                            DueDistance = CurrentOdometer - 10,
                                            Title = "OverDue By Mileage"
                                        },
                                    new Reminder
                                        {
                                            ReminderId = 6,
                                            DueDate = DateTime.UtcNow.AddDays(-1),
                                            DueDistance = CurrentOdometer - 1,
                                            Title = "Overdue by date and mileage"
                                        },
                                    new Reminder
                                        {
                                            ReminderId = 7,
                                            DueDate = DateTime.UtcNow.AddDays(45),
                                            DueDistance = null,
                                            Title = "Not Upcoming or Overdue and DueDistance null"
                                        },
                                };

            var fulfilledReminder = new Reminder
                                        {
                                            ReminderId = 7,
                                            DueDate = DateTime.UtcNow.AddDays(-1),
                                            DueDistance = CurrentOdometer - 1,
                                            Title = "Fulfilled reminder",
                                            IsFulfilled = true
                                        };

            reminders.Add(fulfilledReminder);
            return reminders.AsEnumerable();
        }
        public void WhenRetrievingUpcomingReminders_ThenRemindersInDueDateRangeRetrieved()
        {
            DateTime dateStart = DateTime.UtcNow;
            DateTime dateEnd = DateTime.UtcNow.AddDays(5);
            int odometer = 1000;
            int warningThreshold = 500;
            int outsideOdometerRange = odometer + warningThreshold + 1;

            var repository = new ReminderRepository(this.GetUnitOfWork());

            // reminders just inside range
            var inRangeReminder1 = new Reminder()
                               {
                                   DueDate = dateStart.AddDays(1),
                                   DueDistance = outsideOdometerRange,
                                   Title = "UpcomingReminder"
                               };
            repository.Create(this.defaultVehicle.VehicleId,
                                inRangeReminder1);

            var inRangeReminder2 = new Reminder()
                                {
                                    DueDate = dateEnd.AddDays(-1),
                                    DueDistance = outsideOdometerRange,
                                    Title = "UpcomingReminder1"
                                };
            repository.Create(this.defaultVehicle.VehicleId,
                    inRangeReminder2);

            // reminders just outside of range
            repository.Create(this.defaultVehicle.VehicleId,
                              new Reminder()
                              {
                                  DueDate = dateStart.AddDays(-1),
                                  DueDistance = outsideOdometerRange,
                                  Title = "OutsideRangeReminder1"
                              });

            repository.Create(this.defaultVehicle.VehicleId,
                              new Reminder()
                              {
                                  DueDate = dateEnd.AddDays(1),
                                  DueDistance = outsideOdometerRange,
                                  Title = "OutsideRangeReminder2"
                              });

            var reminders = repository.GetUpcomingReminders(
                this.defaultVehicle.VehicleId,
                dateStart,
                dateEnd,
                odometer,
                warningThreshold
                );

            Assert.Equal(2, reminders.Count());
            Assert.True(reminders.Any(r => r.ReminderId == inRangeReminder1.ReminderId));
            Assert.True(reminders.Any(r => r.ReminderId == inRangeReminder2.ReminderId));

        }
 public void Create(int vehicleId, Reminder reminder)
 {
     reminder.VehicleId = vehicleId;
     this.GetDbSet<Reminder>().Add(reminder);
     this.UnitOfWork.SaveChanges();
 }