// POST: api/Registration
        public long Post([FromBody]RegistrationDTO request)
        {
            int changesSaved = 0;
            try
            {
                using (var dbContext = new WeddingSiteBuilderEntities())
                {
                    var person = new Person()
                    {
                        Email = request.Email,
                        Password = request.Password,
                        FirstName = request.FirstName,
                        LastName = request.LastName
                    };
                    dbContext.People.Add(person);

                    var wedding = new Wedding();

                    dbContext.Weddings.Add(wedding);

                    changesSaved = dbContext.SaveChanges();

                    var attendee = new Attendee()
                    {
                        WeddingID = wedding.WeddingID,
                        PersonID = person.PersonID,
                        WeddingRole = request.BrideOrGroom == "Bride" ? "Bride" : "Groom",
                        Side = request.BrideOrGroom == "Bride" ? "Bride" : "Groom"
                    };

                    dbContext.Attendees.Add(attendee);

                    changesSaved += dbContext.SaveChanges();

                    if(changesSaved == 3)
                    {
                        return wedding.WeddingID;
                    }
                    else
                    {
                        return 0;
                    }
                }
            }
            catch(Exception)
            {
                return 0;
            }
        }
 public AttendeeModel(Attendee att)
 {
     AttendeeID = att.AttendeeID;
     PersonID = att.PersonID;
     WeddingID = att.WeddingID;
     WeddingRole = att.WeddingRole;
     Relationship = att.Relationship;
     Side = att.Side;
     Attending = att.Attending;
     PartyMember = att.PartyMember;
     PartyMemberBlurb = att.PartyMemberBlurb;
     RSVPStatus = att.RSVPStatus;
     NumberofRSVPs = att.NumberofRSVPs;
     Person = new PersonModel(att.Person);
 }
        // POST: api/RSVP
        public AttendeeWithRSVP Post([FromBody]AttendeeDTO request)
        {
            using(var dbContext = new WeddingSiteBuilderEntities())
            {
                var attendee = new Attendee()
                {
                    Side = request.Side,
                    WeddingID = request.WeddingId,
                    Person = new Person()
                    {
                        FirstName = request.FirstName,
                        LastName = request.LastName,
                        Email = request.Email
                    }
                };

                dbContext.Attendees.Add(attendee);
                var changesSaved = dbContext.SaveChanges();

                var rsvp = new RSVPLink()
                {
                    AttendeeID = attendee.AttendeeID,
                    GuidToken = new Guid()
                };

                dbContext.RSVPLinks.Add(rsvp);
                changesSaved += dbContext.SaveChanges();

                var attendeeWithRSVP = (AttendeeWithRSVP)attendee;
                attendeeWithRSVP.RSVP = rsvp;

                SendRSVPEmail(attendeeWithRSVP, dbContext);

                return attendeeWithRSVP;
            }
        }
        // POST: api/Attendee
        public bool Post([FromBody]AttendeeDTO request)
        {
            using(var dbContext = new WeddingSiteBuilderEntities())
            {
                var wedding = dbContext.Weddings.FirstOrDefault(w => w.WeddingID == request.WeddingId);
                if (wedding != null)
                {
                    if (request.AttendeeId.HasValue)
                    {
                        var existingAttendee = dbContext.Attendees.FirstOrDefault(a => a.AttendeeID == request.AttendeeId.Value);
                        if (existingAttendee != null)
                        {
                            if (!string.IsNullOrWhiteSpace(request.FirstName)) existingAttendee.Person.FirstName = request.FirstName;
                            if (!string.IsNullOrWhiteSpace(request.LastName)) existingAttendee.Person.LastName = request.LastName;
                            if (!string.IsNullOrWhiteSpace(request.Email)) existingAttendee.Person.Email = request.Email;
                            if (!string.IsNullOrWhiteSpace(request.Relationship)) existingAttendee.Relationship = request.Relationship;
                            if (!string.IsNullOrWhiteSpace(request.Role)) existingAttendee.WeddingRole = request.Role;
                            if (request.Role.ToLower() == "bride" || request.Role.ToLower() == "groom")
                            {
                                existingAttendee.Side = request.Role;
                            }
                            else if (request.Side.ToLower() == "bride" || request.Side.ToLower() == "groom")
                            {
                                existingAttendee.Side = request.Side;
                            }
                            if (!string.IsNullOrWhiteSpace(request.Blurb)) existingAttendee.PartyMemberBlurb = request.Blurb;
                            existingAttendee.PartyMember = request.PartyMember;

                            try
                            {
                                var changesSaved = dbContext.SaveChanges();
                                return true;
                            }
                            catch (Exception)
                            {
                                return false;
                            }

                        }
                    }
                    else if (!(string.IsNullOrWhiteSpace(request.FirstName) || string.IsNullOrWhiteSpace(request.LastName)))
                    {
                        if (request.Role.ToLower() == "bride" || request.Role.ToLower() == "groom")
                        {
                            request.Side = request.Role;
                        }
                        else if (!(request.Side.ToLower() == "bride" || request.Side.ToLower() == "groom"))
                        {
                            request.Side = "Bride";
                        }

                        var attendee = new Attendee()
                        {
                            Side = request.Side,
                            WeddingRole = request.Role,
                            Relationship = request.Relationship,
                            PartyMemberBlurb = request.Blurb,
                            PartyMember = request.PartyMember,
                            WeddingID = request.WeddingId,
                            Person = new Person()
                            {
                                FirstName = request.FirstName,
                                LastName = request.LastName,
                                Email = request.Email
                            }
                        };

                        dbContext.Attendees.Add(attendee);
                        var changesSaved = dbContext.SaveChanges();
                        return (changesSaved == 2);
                    }
                }
                return false;
            }
        }
        // POST: api/RSVP
        public AttendeeWithRSVP Post([FromBody]AttendeeDTO request)
        {
            using(var dbContext = new WeddingSiteBuilderEntities())
            {
                var attendee = new Attendee()
                {
                    Side = request.Side,
                    WeddingID = request.WeddingId,
                    Person = new Person()
                    {
                        FirstName = request.FirstName,
                        LastName = request.LastName,
                        Email = request.Email
                    }
                };
                try
                {
                    dbContext.Attendees.Add(attendee);
                    var changesSaved = dbContext.SaveChanges();

                    var rsvp = new RSVPLink()
                    {
                        AttendeeID = attendee.AttendeeID,
                        GuidToken = Guid.NewGuid(),
                        RSVPNameBlub = string.Empty,
                        IsAnswered = false
                    };

                    dbContext.RSVPLinks.Add(rsvp);
                    changesSaved += dbContext.SaveChanges();

                    var attendeeWithRSVP = new AttendeeWithRSVP
                    {
                        Attendee = new AttendeeModel(attendee),
                        RSVP = new RSVPLinkModel(rsvp)
                    };

                    SendRSVPEmail(attendee, rsvp, dbContext);

                    return attendeeWithRSVP;
                }
                catch(Exception)
                {
                    return null;
                }
            }
        }
        private void SendRSVPEmail(Attendee attendee, RSVPLink rsvp, WeddingSiteBuilderEntities dbContext)
        {
            var couple = dbContext.Attendees.Where(a => a.WeddingID == attendee.WeddingID && (a.WeddingRole == "Bride" || a.WeddingRole == "Groom")).ToList();
            string subject;

            if(couple.Count() == 1)
            {
                var herOrHis = couple.FirstOrDefault().WeddingRole == "Bride" ? "her" : "his";
                subject = "Hey " + attendee.Person.FirstName + ", let {0} know if you'll be coming to " + herOrHis + " wedding";
            }
            else
            {
                subject = "Hey " + attendee.Person.FirstName + ", let {0} and {1} know if you'll be coming to their wedding";
            }

            var message = new StringBuilder();
            message.AppendLine("RSVP for the wedding by clicking on the link below.");
            message.AppendLine(string.Format("http://localhost:59998/Views/respondtorsvp.html?token={0}", rsvp.GuidToken));

            EmailService.Instance.SendEmail(
                attendee.Person.Email,
                couple.Count() == 1 ? string.Format(subject, couple.FirstOrDefault().Person.FirstName) : string.Format(subject, couple.FirstOrDefault().Person.FirstName, couple.LastOrDefault().Person.FirstName),
                message.ToString());
        }