public RSVPLinkModel(RSVPLink rsvp) { RSVPLinkID = rsvp.RSVPLinkID; AttendeeID = rsvp.AttendeeID; RSVPNameBlub = rsvp.RSVPNameBlub; GuidToken = rsvp.GuidToken; IsAnswered = rsvp.IsAnswered; }
// 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/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()); }