public static System.Threading.Tasks.Task <int> SaveAsync(Models.SchoolEvent schoolEvent)
 {
     using (var db = new Context.SqlContext())
     {
         db.Entry(schoolEvent).State = schoolEvent.SchoolEventId.Equals(0) ? EntityState.Added : EntityState.Modified;
         return(db.SaveChangesAsync());
     }
 }
        public static int Save(Models.SchoolEvent schoolEvent, bool notifyVolunteers, string customMessage)
        {
            try
            {
                using (var db = new Context.SqlContext())
                {
                    db.Entry(schoolEvent).State = schoolEvent.SchoolEventId.Equals(0) ? EntityState.Added : EntityState.Modified;
                    var ret = db.SaveChanges();

                    if ((schoolEvent.IsVolunteerOpportunity) && (notifyVolunteers))
                    {
                        List <string> volunteerEmails = new List <string>();
                        if (schoolEvent.Volunteers == null)
                        {
                            db.Entry(schoolEvent).Collection(v => v.Volunteers).Load();
                        }
                        if (schoolEvent.Volunteers.Count > 0)
                        {
                            foreach (EventVolunteer ev in schoolEvent.Volunteers)
                            {
                                if (!string.IsNullOrEmpty(ev.Email))
                                {
                                    volunteerEmails.Add(ev.Email);
                                }
                            }
                        }
                        if ((volunteerEmails.Count > 0) || !string.IsNullOrEmpty(schoolEvent.PrimaryContactsEmail))
                        {
                            List <string> bcc = volunteerEmails;
                            List <string> to  = new List <string>();
                            if (!string.IsNullOrEmpty(schoolEvent.PrimaryContactsEmail))
                            {
                                to.Add(schoolEvent.PrimaryContactsEmail);
                            }
                            string subject = "Updated: \"" + schoolEvent.Title + "\" (starts on " + string.Format("{0:g}", schoolEvent.Start) + ")";
                            string body    = "Details regarding the \"" + schoolEvent.Title + "\" event have been updated. <br>" +
                                             (string.IsNullOrEmpty(customMessage) ? "" : customMessage) + "<br>" +
                                             "<b>Start</b>: " + string.Format("{0:f}", schoolEvent.Start) + "<br>" +
                                             "<b>End</b>: " + string.Format("{0:f}", schoolEvent.End) + "<br>" +
                                             (!string.IsNullOrEmpty(schoolEvent.Location) ? ("<b>Location</b>: " + schoolEvent.Location + "<br>") : "") +
                                             (!string.IsNullOrEmpty(schoolEvent.Description) ? ("<b>Description</b>: " + schoolEvent.Description + "<br>") : "");
                            bool emailsent = SendEventEmail(to, null, bcc, subject, body);
                        }
                    }

                    return(ret);
                }
            }
            catch (Exception ex)
            {
                return(-1);
            }
        }
        public static object Delete(Models.SchoolEvent schoolEvent, bool notifyVolunteers, string customMessage)
        {
            using (var db = new Context.SqlContext())
            {
                List <string> volunteerEmails = new List <string>();
                if (schoolEvent.Volunteers == null)
                {
                    db.Entry(schoolEvent).Collection(v => v.Volunteers).Load();
                }
                if (schoolEvent.Volunteers.Count > 0)
                {
                    if (notifyVolunteers)
                    {
                        foreach (EventVolunteer ev in schoolEvent.Volunteers)
                        {
                            if (!string.IsNullOrEmpty(ev.Email))
                            {
                                volunteerEmails.Add(ev.Email);
                            }
                        }
                    }
                    schoolEvent.Volunteers.Clear();
                }
                db.Entry(schoolEvent).State = schoolEvent.SchoolEventId.Equals(0) ? EntityState.Unchanged : EntityState.Deleted;
                var ret = db.SaveChanges();

                //Send email notifying primary contact and all volunteers this event has been canceled
                if (notifyVolunteers)
                {
                    if ((volunteerEmails.Count > 0) || string.IsNullOrEmpty(schoolEvent.PrimaryContactsEmail))
                    {
                        List <string> bcc = volunteerEmails;
                        List <string> to  = new List <string>();
                        if (!string.IsNullOrEmpty(schoolEvent.PrimaryContactsEmail))
                        {
                            to.Add(schoolEvent.PrimaryContactsEmail);
                        }
                        string subject = "Canceled: \"" + schoolEvent.Title + "\" (starts on " + string.Format("{0:g}", schoolEvent.Start) + ")";
                        string body    = "The \"" + schoolEvent.Title + "\" event has been canceled. <br>" +
                                         (string.IsNullOrEmpty(customMessage) ? "" : customMessage) + "<br>" +
                                         "<b>Start</b>: " + string.Format("{0:f}", schoolEvent.Start) + "<br>" +
                                         "<b>End</b>: " + string.Format("{0:f}", schoolEvent.End) + "<br>" +
                                         (!string.IsNullOrEmpty(schoolEvent.Location) ? ("<b>Location</b>: " + schoolEvent.Location + "<br>") : "") +
                                         (!string.IsNullOrEmpty(schoolEvent.Description) ? ("<b>Description</b>: " + schoolEvent.Description + "<br>") : "");
                        bool emailsent = SendEventEmail(to, null, bcc, subject, body);
                    }
                }

                return(ret);
            }
        }