public async Task <IActionResult> Push(Recipe recipe, Session session) { var sessionRecipe = new SessionRecipe { Recipe = recipe, StartTime = DateTime.Now }; session.SessionRecipes.Add(sessionRecipe); _context.Sessions.Update(session); await _context.SaveChangesAsync(); return(RedirectToAction("Show", "SessionRecipe", new { sessionId = session.Id, recipeSessionId = sessionRecipe.Id })); }
public async Task <IActionResult> Create(int recipeId, bool?redirect) { var user = await _context.AppUsers.FirstAsync(r => r.Email == User.Identity.Name); if (user == null) { return(null); } var session = await _context.Sessions .Include(r => r.SessionRecipes) .ThenInclude(s => s.Recipe) .FirstOrDefaultAsync(r => r.User == user); var recipe = _context.Recipes.FirstOrDefaultAsync(r => r.Id == recipeId); var sessionRecipe = new SessionRecipe { Recipe = await recipe, StartTime = DateTime.Now }; if (session == null) { session = new Session { User = user, SessionRecipes = new List <SessionRecipe>(1) }; session.SessionRecipes.Add(sessionRecipe); _context.Sessions.Add(session); } else if (redirect.HasValue && redirect.Value) { return(await Push(await recipe, session)); } else { session.SessionRecipes.Clear(); session.SessionRecipes.Add(sessionRecipe); _context.Sessions.Update(session); } _context.SaveChanges(); return(RedirectToAction("Show", "SessionRecipe", new { sessionId = session.Id, recipeSessionId = sessionRecipe.Id })); }