public async Task <IActionResult> Create(CreateViewModel model) { try { if (ModelState.IsValid) { var account = await db.Accounts .AsNoTracking() .FirstOrDefaultAsync(x => x.Email == User.Identity.Name); var baskets = await db.Baskets .Where(x => x.AccountId == account.Id) .ToListAsync(); if (baskets.Count() != 0) { var orderHeader = new OrderHeader() { AccountId = account.Id, DateTime = DateTimeOffset.Now, Status = OrderStatus.New, Address = model.Adress, Phone = model.Phone, CardNumber = model.CardNumber, CardMonth = model.CardMoth, CardYear = model.CardYear, CardCIV = model.CardCIV, }; await db.OrderHeaders.AddAsync(orderHeader); await db.SaveChangesAsync(); foreach (var basket in baskets) { await db.OrderBodies.AddAsync(new OrderBody() { GameId = basket.GameId, Quantity = basket.Quantity, OrderHeaderId = orderHeader.Id }); db.Baskets.Remove(basket); await db.SaveChangesAsync(); } } return(RedirectToAction(nameof(Index))); } } catch (DbUpdateException) { ModelState.AddModelError("", "Ощибка при создании заказа. Обратитесь к разработчику."); } return(View(model)); }
public async Task <BasketResultViewModel> AddBasket(int?gameId) { var basketResultViewModel = new BasketResultViewModel(); if (gameId == null) { basketResultViewModel.StatusCode = 404; basketResultViewModel.Message = "Не удалось найти игру."; return(basketResultViewModel); } var account = await db.Accounts .AsNoTracking() .FirstOrDefaultAsync(x => x.Email == User.Identity.Name); var currentBasket = await db.Baskets .AsNoTracking() .FirstOrDefaultAsync(x => x.AccountId == account.Id && x.GameId == gameId); if (currentBasket != null) { basketResultViewModel.StatusCode = 403; basketResultViewModel.Message = "Игра уже добавлена в корзину."; return(basketResultViewModel); } var basket = new Basket() { AccountId = account.Id, GameId = gameId.Value, Quantity = 1 }; await db.Baskets.AddAsync(basket); await db.SaveChangesAsync(); basketResultViewModel.StatusCode = 200; basketResultViewModel.Message = "Игра добавлена в корзину."; basketResultViewModel.BasketCnt = await db.Baskets .AsNoTracking() .Where(x => x.AccountId == account.Id) .SumAsync(x => x.Quantity); return(basketResultViewModel); }
public async Task <IActionResult> Register(RegisterModel model) { if (ModelState.IsValid) { Account account = await db.Accounts .FirstOrDefaultAsync(a => a.Email == model.Email); if (account == null) { db.Accounts.Add(new Account { Email = model.Email, Password = model.Password }); await db.SaveChangesAsync(); await Authenticate(model.Email); return(RedirectToAction("Index", "Game")); } else { ModelState.AddModelError("", "Некорректные логин и(или) пароль"); } } return(View(model)); }