예제 #1
0
        public void CreateOrder(Order order)
        {
            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    order.OrderPlaced = DateTime.Now;
                    _context.Orders.AddRange(order);
                    _context.SaveChanges();
                    var shoppingCartItems = _shoppingCart.ShoppingCartItems;

                    foreach (var item in shoppingCartItems)
                    {
                        var orderDetail = new OrderDetail
                        {
                            Quantity  = item.Quantity,
                            ProductId = item.Product.Id,
                            OrderId   = order.OrderId,
                            Price     = (decimal)item.Product.SellPrice
                        };
                        _context.OrderDetails.AddRange(orderDetail);
                    }
                    _context.SaveChanges();
                    transaction.Commit();
                }
                catch (Exception)
                {
                    transaction.Rollback();
                }
            }
        }
예제 #2
0
        public void AddToCart(Product product, int Quantity)
        {
            var shoppingCartItem = _context.ShoppingCartItems.SingleOrDefault(s => s.Product.Id == product.Id && s.ShoppingCartId == ShoppingCartId);

            if (shoppingCartItem == null)
            {
                shoppingCartItem = new ShoppingCartItem
                {
                    ShoppingCartId = ShoppingCartId,
                    Product        = product,
                    Quantity       = 1
                };

                _context.ShoppingCartItems.Add(shoppingCartItem);
            }
            else
            {
                shoppingCartItem.Quantity++;
            }
            _context.SaveChanges();
        }
예제 #3
0
        public IActionResult Edit(int id, [Bind("Id,Name,PCode,Description,PurchasePrice,SellPrice,InStock,Sale,CreatedDate,ProductCategoryID,CategoryCatId,Photo,PhtotPath")] ProductEditViewModel productVM)
        {
            if (id != productVM.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (ModelState.IsValid)
                    {
                        string uniqueFileName = UploadedFile(productVM);

                        Product products = new Product
                        {
                            Id                = productVM.Id,
                            Name              = productVM.Name,
                            PCode             = productVM.PCode,
                            Description       = productVM.Description,
                            PurchasePrice     = (decimal)productVM.PurchasePrice,
                            SellPrice         = productVM.SellPrice,
                            InStock           = productVM.InStock,
                            Sale              = productVM.Sale,
                            CreatedDate       = DateTime.Now,
                            ProductCategoryID = productVM.ProductCategoryID,
                            CategoryCatId     = productVM.CategoryCatId
                        };

                        if (uniqueFileName == null)
                        {
                            products.ImagePath = productVM.PhtotPath;
                        }
                        else if (productVM.PhtotPath != null)
                        {
                            string filePath = Path.Combine(webHostEnvironment.WebRootPath, "images", productVM.PhtotPath);
                            System.IO.File.Delete(filePath);
                            products.ImagePath = uniqueFileName;
                        }
                        else
                        {
                            products.ImagePath = uniqueFileName;
                        }

                        _context.Update(products);
                        _context.SaveChanges();

                        //  return RedirectToAction(nameof(Index));
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProductExists(productVM.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ProductCategoryID"] = new SelectList(_context.ProductCategory, "Id", "Name", productVM.ProductCategoryID);
            ViewData["CategoryCatId"]     = new SelectList(_context.Category, "CatId", "CatTitle", productVM.CategoryCatId);
            return(View(productVM));
        }