public void Add(CheckOutModel model) { var errors = model.GetValidationErrors(); if (!model.IsDelivery.HasValue) errors.Add(new DbValidationError("IsDelivery", "Выберите способ получения товара")); errors.ThrowIfHasErrors(); var order = Db.CreateAndAdd<tblOrder>(); order.IsDelivery = model.IsDelivery; order.Address = model.Address; order.City = model.City; order.Email = model.Email; order.Phone = model.Phone; order.Name = model.Name; var productIds = UserInfo.Cart.Select(x => x.ProductId).ToList(); var forSale = ProductSingleStateEnum.ForSale.CastTo<int>(); var soldState = ProductSingleStateEnum.Sold.CastTo<int>(); lock (Locker) { var productItems = Db.Set<tblProduct>() .Where(x => productIds.Contains(x.Id)) .Select(x => new { x.Id, x.Name, Items = x.tblProductSingles .Where(s => s.State == forSale) .Where(s => s.OrderId == null) .ToList() }); foreach (var cartItem in UserInfo.Cart) { var count = cartItem.Count; var prod = productItems.Single(x => x.Id == cartItem.ProductId); var items = prod.Items.Take(count).ToList(); if (items.Count != count) throw new ValidationException($@"Выбранный вами товар: {prod.Name} уже куплен"); order.tblProductSingles.AddRangeUnique(items); items.ForEach(x => x.State = soldState); } Db.SaveChanges(); model.Id = order.Id; } }
public ActionResult Cart(CheckOutModel model) { try { Service.Order.Add(model); return RedirectToRouteNotify("Ordered", new { id = model.Id }); } catch (ValidationException ex) { AddModelErrors(ex); var cart = Service.Cart.GetCart(); cart.CheckOutModel = model; return View("~/Views/Public/Cart/Index.cshtml", cart); } }
public CartPageModel() { Products = new List <CartProductItem>(); CheckOutModel = new CheckOutModel(); }
public CartPageModel() { Products = new List<CartProductItem>(); CheckOutModel = new CheckOutModel(); }