public IActionResult JoinWedding(int WeddingId) { // Get user object var CurrentUser = _context.Users.Where(u => u.UserId == HttpContext.Session.GetInt32("CurrentUserId")).Include(w => w.WeddingsAttending).ThenInclude(x => x.Weddings).SingleOrDefault(); // Check if user is already attending the wedding. foreach (var wedding in CurrentUser.WeddingsAttending) { if (wedding.WeddingsId == WeddingId) { TempData["RsvpError"] = "You're already attending that wedding!"; return(RedirectToAction("Account")); } } // Change UpdatedAt field in User object CurrentUser.UpdatedAt = DateTime.Now; // Get wedding object in order to increment NumGuests Weddings SelectedWedding = _context.Weddings.Where(w => w.WeddingsId == WeddingId).SingleOrDefault(); SelectedWedding.NumGuests++; // Add user to weddings attending list. Atendees NewAtendee = new Atendees { User = CurrentUser, Weddings = SelectedWedding }; _context.Atendees.Add(NewAtendee); _context.SaveChanges(); return(RedirectToAction("ShowWedding", new { WeddingId = WeddingId })); }
public IActionResult LeaveWedding(int WeddingId) { Atendees SelectedAtendeeObject = _context.Atendees.Where(w => w.WeddingsId == WeddingId && w.UserId == HttpContext.Session.GetInt32("CurrentUserId")).SingleOrDefault(); _context.Remove(SelectedAtendeeObject); // Get wedding object in order to decrement NumGuests Weddings SelectedWedding = _context.Weddings.Where(w => w.WeddingsId == WeddingId).SingleOrDefault(); SelectedWedding.NumGuests--; _context.SaveChanges(); return(RedirectToAction("Account")); }