public IActionResult Create(UserProfileVM upvm) { AspNetUsers aspuser = new AspNetUsers { UserName = upvm.aspNetUsers.UserName }; RestaurantGoer rg = new RestaurantGoer { Name = upvm.restaurantGoer.Name, PriceCategory = upvm.restaurantGoer.PriceCategory }; db.SaveChanges(); return(RedirectToAction("Index", upvm.aspNetUsers.Id)); }
public bool Update(string id, UserProfileVM upVM) { RestaurantGoer restaurantGoer = db.RestaurantGoer .Where(r => r.UserId == id) .FirstOrDefault(); if (restaurantGoer != null) { restaurantGoer.Name = upVM.Name; //restaurantGoer.PriceCategory = upVM.price_category; restaurantGoer.UserId = id; db.SaveChanges(); } else { RestaurantGoer restaurantGoers = new RestaurantGoer(); restaurantGoers.UserId = id; restaurantGoers.Name = upVM.Name; //restaurantGoers.PriceCategory = upVM.price_category; db.Add(restaurantGoers); db.SaveChanges(); } var restaurantgoerId = db.RestaurantGoer .Where(r => r.UserId == id) .FirstOrDefault(); var foodCategoryId = db.FoodCategory .Where(f => f.Name == upVM.foodCategoryName) .FirstOrDefault(); //RestaurantGoerFoodCategory restaurantGoerFoodCategory = new RestaurantGoerFoodCategory(); //restaurantGoerFoodCategory.FoodCategoryId = foodCategoryId.Id; //restaurantGoerFoodCategory.RestaurantGoerId = restaurantgoerId.Id; //db.Add(restaurantGoerFoodCategory); //db.SaveChanges(); var restaurantId = db.Restaurant .Where(r => r.Name == upVM.RestaurantName) .FirstOrDefault(); RestaurantGoerRestaurant restaurantGoerRestaurant = new RestaurantGoerRestaurant(); restaurantGoerRestaurant.RestaurantGoerId = restaurantgoerId.Id; restaurantGoerRestaurant.RestaurantId = restaurantId.Id; db.Add(restaurantGoerRestaurant); db.SaveChanges(); foreach (var checkbox in upVM.FoodCategoryCheckboxes) { if (checkbox.IsChecked) { RestaurantGoerFoodCategory entry = new RestaurantGoerFoodCategory() { FoodCategoryId = checkbox.ID, RestaurantGoerId = restaurantGoer.Id }; db.Add(entry); db.SaveChanges(); } else { ICollection <RestaurantGoerFoodCategory> entries = db.RestaurantGoerFoodCategory .Where(f => f.RestaurantGoerId == restaurantGoer.Id && f.FoodCategoryId == checkbox.ID) .Distinct().ToList(); foreach (var entry in entries) { db.RestaurantGoerFoodCategory.Remove(entry); db.SaveChanges(); } } } PriceCategoryDropdownBuilder helper = new PriceCategoryDropdownBuilder(); upVM.PriceCategory = helper.convertType(upVM.PriceCategoryString); if (upVM.PriceCategory != null) { restaurantGoer.PriceCategory = upVM.PriceCategory; db.SaveChanges(); } return(true); }
//public void OnGet(string returnUrl = null) //{ // ViewData["SiteKey"] = _configuration["Recaptcha:SiteKey"]; // ReturnUrl = returnUrl; //} //[ValidateRecaptcha] public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { var user = new IdentityUser { UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); // creates a new restraunt or restraunt goer associated with the new user and adds role UserRoleAccountRepo userRoleRepo = new UserRoleAccountRepo(_context, _serviceProvider); var usertype = Input.Usertype; var userid = user.Id; var email = user.Email; if (usertype == "Member") { await userRoleRepo.AddUserRole(email, "Member"); RestaurantGoer restaurantgoer = new RestaurantGoer() { UserId = userid, User = _context.AspNetUsers.Where(u => u.Id == userid) .FirstOrDefault() }; await _context.AddAsync(restaurantgoer); await _context.SaveChangesAsync(); } if (usertype == "Manager") { await userRoleRepo.AddUserRole(email, "Manager"); Restaurant restaurant = new Restaurant() { UserId = userid, User = _context.AspNetUsers.Where(u => u.Id == userid) .FirstOrDefault() }; await _context.AddAsync(restaurant); await _context.SaveChangesAsync(); } var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = user.Id, code = code }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // Reset the site key if there is an error. // ViewData["SiteKey"] = _configuration["Recaptcha:SiteKey"]; // If we got this far, something failed, redisplay form return(Page()); }
public UserProfileVM GetProfile(string id) { AspNetUsers user = db.AspNetUsers .Where(u => u.Id == id) .FirstOrDefault(); RestaurantGoer newGoers = db.RestaurantGoer .Where(rg => rg.UserId == id) .FirstOrDefault(); int resGoerId = db.RestaurantGoer .Where(r => r.UserId == id) .Select(r => r.Id).FirstOrDefault(); ICollection <FoodCategory> newCategory = db.RestaurantGoerFoodCategory .Where(f => f.RestaurantGoerId == resGoerId) .Select(f => f.FoodCategory).Distinct().ToList(); ICollection <Restaurant> newRestaurant = db.RestaurantGoerRestaurant .Where(r => r.RestaurantGoerId == resGoerId) .Select(r => r.Restaurant).Distinct().ToList(); var allCategories = db.FoodCategory.ToList(); var checkBoxListItems = new List <CheckBoxListItem>(); foreach (var category in allCategories) { bool IsChecked = false; foreach (var c in newCategory) { if (c.Id == category.Id) { IsChecked = true; break; } } checkBoxListItems.Add(new CheckBoxListItem() { ID = category.Id, Display = category.Name, IsChecked = IsChecked }); } List <SelectListItem> priceCategoryDropdownList = new PriceCategoryDropdownBuilder().priceCategories; foreach (var c in priceCategoryDropdownList) { if (c.Value == newGoers.PriceCategory.ToString()) { c.Selected = true; break; } } UserProfileVM profile = new UserProfileVM() { aspNetUsers = user, restaurantGoer = newGoers, foodCategoryList = newCategory, restaurantList = newRestaurant, foodCategories = newCategory, FoodCategoryCheckboxes = checkBoxListItems, PriceCategory = newGoers.PriceCategory, PriceCategoryList = priceCategoryDropdownList }; return(profile); }