// GET: api/RSVP/5 public List<AttendeeWithRSVP> Get(long WeddingId) { if (WeddingId != 0) { using (var dbContext = new WeddingSiteBuilderEntities()) { var attendees = new List<AttendeeWithRSVP>(); dbContext.Attendees .Where(a => a.WeddingID == WeddingId && !(a.WeddingRole == "Bride" || a.WeddingRole == "Groom" || a.PartyMember == true)) .ToList() .ForEach(a => { var rsvp = dbContext.RSVPLinks.FirstOrDefault(r => r.AttendeeID == a.AttendeeID); if (rsvp != null) { var attendee = new AttendeeWithRSVP { Attendee = new AttendeeModel(a), RSVP = new RSVPLinkModel(rsvp) }; attendees.Add(attendee); } }); return attendees; } } else { return new List<AttendeeWithRSVP>(); } }
// GET: api/RSVP/1652c050-6c56-48be-98f1-a899e387078f public AttendeeWithRSVP Get(Guid Id) { using(var dbContext = new WeddingSiteBuilderEntities()) { var rsvp = dbContext.RSVPLinks.FirstOrDefault(r => r.GuidToken == Id); var att = dbContext.Attendees.FirstOrDefault(a => a.AttendeeID == rsvp.AttendeeID); if (rsvp == null || att == null) return null; var attendee = new AttendeeWithRSVP { Attendee = new AttendeeModel(att), RSVP = new RSVPLinkModel(rsvp) }; return attendee; } }
// 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(AttendeeWithRSVP request, WeddingSiteBuilderEntities dbContext) { var couple = dbContext.Attendees.Where(a => a.WeddingID == request.WeddingID && (a.WeddingRole == "Bride" || a.WeddingRole == "Groom")).ToList(); string subject; if(couple.Count() == 1) { var herOrHis = couple.FirstOrDefault().WeddingRole == "Bride" ? "her" : "his"; subject = "Hey " + request.Person.FirstName + ", let {0} know if you'll be coming to " + herOrHis + " wedding"; } else { subject = "Hey " + request.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/sendviewrsvp.html?token={0}", request.RSVP.GuidToken)); EmailService.Instance.SendEmail( request.Person.Email, couple.Count() == 1 ? string.Format(subject, couple.FirstOrDefault().Person.FirstName) : string.Format(subject, couple.LastOrDefault().Person.FirstName), message.ToString()); }