private static string ConstructEmailBody(Tuple<DateTime, DateTime> monthPeriod)
        {
            using (var context = new AttendanceKeepingContext())
            {
                var rostersFromPerdiod = context.RosterAttendances
                    .Include(r => r.ClassRoster)
                    .Include(r => r.ClassRoster.ClassSchedule)
                    .Include(r => r.ClassRoster.Person)
                    .Where(t => t.ClassDate >= monthPeriod.Item1 && t.ClassDate <= monthPeriod.Item2)
                    .ToList();
                var body = new StringBuilder();
                body.AppendFormat("Attendance Figures For {0}-{1} : {3}{2}",
                    monthPeriod.Item1.ToShortDateString(),
                    monthPeriod.Item2.ToShortDateString(),
                    Environment.NewLine,
                    rostersFromPerdiod.Count);

                body.AppendLine();
                foreach (var roster in rostersFromPerdiod.GroupBy(t => t.ClassDate.ToShortDateString()))
                {
                    const string dateTemplate = "Number of attendees for {0}: {1}{2}";
                    body.AppendFormat(dateTemplate, roster.Key, roster.Count(), Environment.NewLine);
                    foreach (var item in roster)
                    {
                        const string lineTemplate = "{0}{3} ~ {1}, {2}{4}";
                        body.AppendFormat(lineTemplate, "\t", item.ClassRoster.Person.LastName,
                                          item.ClassRoster.Person.FirstName,
                                          item.ClassRoster.ClassSchedule.ClassName,
                                          Environment.NewLine);
                    }
                    body.AppendLine();
                }
                return body.ToString();
            }
        }
        public static void SendAttendanceEmail()
        {
            using (var context = new AttendanceKeepingContext())
            {
                var emailAddresses = context.Persons.Where(t => t.Email != null).ToList();

                foreach (var emailAddress in emailAddresses.Where(t => EmailRegex.IsMatch(t.Email)))
                {
                    var fromAddress = new MailAddress("*****@*****.**", "Michael Roylance");
                    var fromPassword = ConfigurationManager.AppSettings["gmailPwd"];
                    var toAddress = new MailAddress(emailAddress.Email, string.Format("{0} {1}", emailAddress.FirstName, emailAddress.LastName));
                    var monthPeriod = ReturnBeginEndMonth();
                    var subject = string.Format("Attendance Figures For {0}-{1}", monthPeriod.Item1.ToShortDateString(), monthPeriod.Item2.ToShortDateString());
                    var body = ConstructEmailBody(monthPeriod);

                    var smtp = new SmtpClient
                    {
                        Host = "smtp.gmail.com",
                        Port = 587,
                        EnableSsl = true,
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                        UseDefaultCredentials = false,
                        Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
                    };
                    using (var message = new MailMessage(fromAddress, toAddress)
                    {
                        Subject = subject,
                        Body = body
                    })
                    {
                        smtp.Send(message);
                    }
                }
            }
        }
 public static bool AuthorizeEmail(string email)
 {
     using(var context = new AttendanceKeepingContext())
     {
         return context.Persons.Any(t => string.Equals(t.Email, email));
     }
 }
 public void SuccessWhenCtorInitiated()
 {
     //arrange
     //act
     using (var disposablePerson = HelperUtilities.CreateNewPerson())
     using(var atc = new AttendanceKeepingContext())
     {
         //assert
         Assert.IsTrue(atc.Persons.Any(t => t.PersonId == disposablePerson.Id));
     }
 }
 public static void ClearDataBase()
 {
     using(var context = new AttendanceKeepingContext())
     {
         var people = context.Persons.ToList();
         foreach(var p in people)
         {
             context.Persons.Remove(p);
         }
         context.SaveChanges();
     }
 }
        public static DisposableDatabaseWrapper CreateNewPerson()
        {
            var person = new Person
                {
                    BirthDate = new DateTime(1982, 12, 9),
                    FirstName = Guid.NewGuid().ToString(),
                    LastName = Guid.NewGuid().ToString()
                };
            var attendanceContext = new AttendanceKeepingContext();
            attendanceContext.Persons.Add(person);
            attendanceContext.SaveChanges();

            var disposable = new DisposableDatabaseWrapper();
            disposable.ExecuteWhenDispose = () =>
                {
                    attendanceContext.Persons.Remove(person);
                    attendanceContext.SaveChanges();
                    attendanceContext.Dispose();
                };
            disposable.Id = person.PersonId;
            disposable.ResultingObject = person;
            return disposable;
        }