public async Task <IActionResult> Register(RegisterModel model) { if (ModelState.IsValid) { User user = await _context.Set <User>().FirstOrDefaultAsync(u => u.Email == model.Email); if (user == null) { string pas = PasswordEncrypt.EncryptStringAes(model.Password, _config.Value.Salt); _context.Set <User>().Add(new User { Email = model.Email, Password = pas, FirstName = model.FirstName, MiddleName = model.MiddleName, LastName = model.LastName, MobilePhone = model.MobilePhone, Created = DateTime.UtcNow }); await _context.SaveChangesAsync(); await Authenticate(model.Email); return(RedirectToAction("Index", "Home")); } ModelState.AddModelError("", "Некорректные логин и(или) пароль"); } return(View(model)); }
public async Task <IActionResult> Register(ContactViewModel model) { List <Item> cart = HttpContext.Session.GetObjectFromJson <List <Item> >("cart"); if (ModelState.IsValid && cart != null && cart.Any()) { User user = await _context.Set <User>().SingleOrDefaultAsync(a => a.Email == User.Identity.Name); Order order = new Order { Created = DateTime.UtcNow, User = user, Contact = new Contact { Created = DateTime.UtcNow, Name = model.Name, Address = model.Address, MobilePhone = model.MobilePhone, Comment = model.Comment } }; foreach (Item item in cart) { Product product = await _context.Set <Product>() .Include(a => a.Prices) .ThenInclude(a => a.ProductSize) .Include(a => a.ProductImages) .ThenInclude(a => a.Color) .FirstAsync(a => a.Id == item.ProductId); order.OrderItems.Add(new OrderItem { ProductId = item.ProductId, SizeId = item.SizeId, ColorId = item.ColorId, Quantity = item.Quantity, Price = product.Prices.First(a => a.ProductSizeId == item.SizeId).Value }); } _context.Set <Order>().Add(order); await _context.SaveChangesAsync(); HttpContext.Session.SetObjectAsJson("cart", new List <Item>()); return(View()); } return(View("Index", model)); }