public ActionResult AddFoodToMeal(ViewModelForMeal viewModel)//int selected, int id) { this.DropDownList(); var kontekst = new Kontekst(); Food result = kontekst.Foods .FirstOrDefault(p => p.Id == viewModel.SelectedId); Recipe resultRecipe = new Recipe(); resultRecipe = kontekst.Recipes .Where(p => p.Id == viewModel.MealId) .FirstOrDefault(); resultRecipe.TotalCalories += result.Calories * resultRecipe.NoServings; resultRecipe.TotalCarbos += Convert.ToInt32(result.Carbs * resultRecipe.NoServings); resultRecipe.TotalFats += Convert.ToInt32(result.Fat * resultRecipe.NoServings); resultRecipe.TotalProteins += Convert.ToInt32(result.Protein * resultRecipe.NoServings); resultRecipe.Foods.Add(result); kontekst.SaveChanges(); kontekst.Dispose(); return(View("Details", resultRecipe)); }
protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); }
public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return(RedirectToAction("Index", "Manage")); } if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await AuthenticationManager.GetExternalLoginInfoAsync(); if (info == null) { return(View("ExternalLoginFailure")); } model.CaloriesPerDay = OptimalCaloriesCounter2(model); var user = new User { UserName = model.Email, Email = model.Email, Name = model.Name, LastName = model.LastName, Age = model.Age, CurrentWeight = model.CurrentWeight, GoalWeight = model.GoalWeight, Height = model.Height, LevelOfActivity = model.SelectedLevel, Gender = model.Gender, DailyCalories = model.CaloriesPerDay }; var progress = new Progress(); progress.UserId = user.Id; progress.UserDailyCalories = user.DailyCalories; progress.Weight = user.CurrentWeight; Kontekst kontekst = new Kontekst(); kontekst.Progress.Add(progress); kontekst.SaveChanges(); kontekst.Dispose(); var result = await UserManager.CreateAsync(user); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); return(View("UserHome", user)); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return(View(model)); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { model.CaloriesPerDay = OptimalCaloriesCounter(model); var user = new User { UserName = model.Email, Email = model.Email, Name = model.Name, LastName = model.LastName, Age = model.Age, CurrentWeight = model.CurrentWeight, GoalWeight = model.GoalWeight, Height = model.Height, LevelOfActivity = model.SelectedLevel, Gender = model.Gender, DailyCalories = model.CaloriesPerDay }; Progress progress = new Progress(); progress.UserId = user.Id; progress.UserDailyCalories = user.DailyCalories; progress.Weight = user.CurrentWeight; Kontekst kontekst = new Kontekst(); kontekst.Progress.Add(progress); kontekst.SaveChanges(); kontekst.Dispose(); var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); return(View("UserHome", user)); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public ActionResult AddFoodToMeal(ViewModelForMeal viewModel)//int selected, int id) { this.DropDownList(); var kontekst = new Kontekst(); Food result = kontekst.Foods .FirstOrDefault(p => p.Id == viewModel.SelectedId); viewModel.Foods.Add(result); Meal resultMeal = new Meal(); resultMeal = kontekst.Meals .Where(p => p.Id == viewModel.MealId) .FirstOrDefault(); resultMeal.Foods.Add(result); kontekst.SaveChanges(); kontekst.Dispose(); return(View("Details", resultMeal)); }