public ActionResult RsvpGuests(Dictionary <String, object>[] guests, Boolean isAttending) { DateTime dateOfRsvp = DateTime.Now; DatabaseAction(conn => { List <Business.Invite> invites = new List <Business.Invite>(); foreach (Dictionary <String, object> guestData in guests) { Int32 inviteId = (Int32)guestData["inviteId"]; Int32 guestId = (Int32)guestData["guestId"]; Business.Invite invite = invites.SingleOrDefault(p => p.Id == inviteId); if (invite == null) { invite = new Business.Invite(conn, inviteId); invites.Add(invite); } Business.Guest guest = invite.Guests.Single(p => p.Id == guestId); guest.IsAttending = isAttending; guest.DateOfRsvp = dateOfRsvp; } foreach (Business.Invite invite in invites) { invite.Save(conn); } }); return(Json(dateOfRsvp)); }
/// <summary> /// Updates a guest in the database /// </summary> /// <param name="conn">Open connection to the database</param> /// <param name="inviteId">Unique identifier of the invite the guest is a member of</param> /// <param name="guest">Guest being updated in the database</param> public static void UpdateGuest(SqlConnection conn, Int32 inviteId, Business.Guest guest) { try { sql.SqlCommand cmd = new sql.SqlCommand("UpdateGuest", conn); cmd.Parameters.AddRange(guest.GetParametersForStoredProcedure(inviteId, true)); cmd.ExecuteNonQuery(); } catch (System.Exception e) { throw new UpdateSqlDbObjectException("Could not update guest", e); } }
/// <summary> /// Adds a guest to the database /// </summary> /// <param name="conn">Open connection to the database</param> /// <param name="inviteId">Unique identifier of the invite the guest is a member of</param> /// <param name="guest">Guest being added to the database</param> /// <returns>Unique identifier of the guest, generated by the database</returns> public static Int32 AddGuest(SqlConnection conn, Int32 inviteId, Business.Guest guest) { try { sql.SqlCommand cmd = new sql.SqlCommand("AddGuest", conn); cmd.Parameters.AddRange(guest.GetParametersForStoredProcedure(inviteId, false)); return(cmd.ExecuteScalar <Int32>()); } catch (System.Exception e) { throw new AddSqlDbObjectException("Could not add guest", e); } }
public ActionResult SaveInvite(Int32 inviteId, String emailAddress, Boolean includesCeremony, Boolean reserveSandholeRoom, Boolean isAdmin, String guests, InviteEmailType emailType) { Boolean result = true; String errorMessage = String.Empty; emailAddress = emailAddress.Trim(); DatabaseAction(conn => { if (!new EmailAddressAttribute().IsValid(emailAddress)) { result = false; errorMessage = "Please enter a valid email address."; return; } if (Business.Invite.GetInvites(conn).Any(p => p.Id != inviteId && p.EmailAddress.ToLower() == emailAddress.ToLower())) { result = false; errorMessage = "There is already another invite using that email address."; return; } Business.Invite invite = new Business.Invite(); if (inviteId != -1) { invite = new Business.Invite(conn, inviteId); } invite.EmailAddress = emailAddress; invite.IncludesCeremony = includesCeremony; invite.ReserveSandholeRoom = reserveSandholeRoom; invite.IsAdmin = isAdmin; JavaScriptSerializer serializer = new JavaScriptSerializer(); Dictionary <String, object>[] guestsData = (Dictionary <String, object>[])serializer.Deserialize(guests, typeof(Dictionary <String, object>[])); invite.Guests.Clear(); foreach (Dictionary <String, object> guestData in guestsData) { Int32 guestId = (Int32)guestData["guestId"]; String forename = (String)guestData["forename"]; String surname = (String)guestData["surname"]; Boolean?isAttending = (Boolean?)guestData["isAttending"]; DateTime?dateOfRsvp = guestData["dateOfRsvp"] == null ? (DateTime?)null : DateTime.Parse((String)guestData["dateOfRsvp"]); Boolean isChild = (Boolean)guestData["isChild"]; Boolean isVegetarian = (Boolean)guestData["isVegetarian"]; Boolean canBringPlusOne = (Boolean)guestData["canBringPlusOne"]; Int32?tableId = guestData["tableId"] == null ? (Int32?)null : (Int32)guestData["tableId"]; Int32?roomId = guestData["roomId"] == null ? (Int32?)null : (Int32)guestData["roomId"]; String notes = (String)guestData["notes"]; Business.Guest guest = new Business.Guest(guestId, forename, surname, isAttending, dateOfRsvp, isChild, isVegetarian, canBringPlusOne, tableId, roomId, notes); invite.Guests.Add(guest); } invite.Save(conn); switch (emailType) { case InviteEmailType.Invite: SendInviteEmail(invite); break; case InviteEmailType.Update: SendUpdateInviteEmail(invite); break; } }); return(Json(new { result = result, errorMessage = errorMessage })); }