public async Task <ActionResult> AddRecurring(int weeklyId) { string userId = HttpContext.User.Identity.GetUserId(); int athleteId = db.Athletes.AsNoTracking().Select(a => new { Id = a.Id, AppId = a.ApplicationUserId }).Single(b => b.AppId.Equals(userId)).Id; WeeklyClassSetup weeklyClassSetup = await db.WeeklyClasses.FindAsync(weeklyId); ClassReminders reminders = await db.ClassReminders.FindAsync(athleteId); List <Class> classes = db.Classes.Where(a => a.WeeklyClass.Id.Equals(weeklyId)).ToList(); reminders.Reminders.AddRange(classes); foreach (var cls in classes) { cls.AttachedReminders.Add(reminders); } reminders.WeeklyClassSetups.Add(weeklyClassSetup); weeklyClassSetup.AttachedReminders.Add(reminders); await db.SaveChangesAsync(); return(RedirectToAction("Class", new { controller = "Reminders" })); }
public async Task <ActionResult> DeleteConfirmed(int id) { Athlete athlete = await _db.Athletes.FindAsync(id); ClassReminders reminders = await _db.ClassReminders.FindAsync(athlete.Id); List <int> reminderClassIds = reminders.Reminders.Select(r => r.Id).ToList(); List <Class> classes = _db.Classes.Where(c => reminderClassIds.Contains(c.Id)).ToList(); foreach (var cls in classes) { cls.AttachedReminders.Remove(reminders); } _db.ClassReminders.Remove(reminders); _db.Athletes.Remove(athlete); var user = await _userManager.FindByIdAsync(athlete.ApplicationUserId); await _userManager.DeleteAsync(user); await _db.SaveChangesAsync(); return(RedirectToAction("Index")); }