public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Price,Quantity,ProductId")] CartItem cartItem)
        {
            if (id != cartItem.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(cartItem);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CartItemExists(cartItem.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(Ok(cartItem));
        }
 public void EditProductQuantity(Product pToEdit, int quantityOrdered)
 {
     pToEdit.Stock      = pToEdit.Stock - quantityOrdered;
     pToEdit.CategoryId = pToEdit.Category.Id;
     pToEdit.Category   = null;
     //var p = GetProduct(pToEdit.ID);
     _context.Update(pToEdit);
 }
        public void HideProduct(Guid id)
        {
            var myProduct = GetProduct(id);

            myProduct.isVisible = false;
            _context.Update(myProduct);
            _context.SaveChanges();
        }
Ejemplo n.º 4
0
        public void CloseOrder(Guid orderId, Guid userId)
        {
            var myOrder = _context.Order.SingleOrDefault(x => x.Id == orderId);
            var email   = _context.Members.SingleOrDefault(x => x.UserId == userId).Email;

            myOrder.OrderStatusId = _context.OrderStatus.SingleOrDefault(x => x.Status == "Checked_Out").Id;
            myOrder.DatePlaced    = DateTime.Now;
            myOrder.UserId        = userId;
            myOrder.Email         = email;

            _context.Update(myOrder);
            _context.SaveChanges();
        }
Ejemplo n.º 5
0
        public void AddToCart(Guid productId, Guid userId)
        {
            var orderId   = GetOrderId(userId);
            var orderItem = _context.OrderDetails.SingleOrDefault(
                x => x.OrderId == orderId && x.ProductId == productId);
            var product = _context.Products.SingleOrDefault(
                x => x.Id == productId);

            if (orderItem == null)
            {
                _context.OrderDetails.Add(new OrderDetails()
                {
                    OrderId = orderId, ProductId = productId, Quantity = 1, SoldPrice = product.Price
                });
            }
            else
            {
                orderItem.Quantity++;
            }
            product.Stock--;
            _context.Update(product);
            _context.SaveChanges();
        }
Ejemplo n.º 6
0
        public void FinalizeOrder(Guid orderId)
        {
            //Comparing the id from the Orders Table to the passing id
            var id = _context.Orders.SingleOrDefault(x => x.Id == orderId);

            //Unless the id is null, update the time/date and match the price to the subtotal
            if (id != null)
            {
                id.DatePlace  = DateTime.Now;
                id.TotalPrice = _orderDetailsRepository.Subtotal(orderId, id.Email);

                _context.Update(id);
                _context.SaveChanges();
            }
        }
Ejemplo n.º 7
0
        public void ReduceStock(IQueryable <Guid> ProductIds, Guid orderId)
        {
            //Calculating every order detail in the Cart once checkout is completed.

            //If we want to get the quantity... we also need the orderId. erm why..quantity f order details qieghed. But In orderDetails jista jkun li il productId jider izjed min darba for different orders
            foreach (var productIdAtLocation in ProductIds)
            {
                var product  = _context.Products.SingleOrDefault(x => x.Id == productIdAtLocation);
                var quantity = _context.OrderDetails.SingleOrDefault(x => x.ProductFk == productIdAtLocation && x.OrderFk == orderId).Quantity;

                if (product.Stock != 0)
                {
                    product.Stock -= quantity;
                    _context.Update(product);
                }
            }

            _context.SaveChanges();
        }
Ejemplo n.º 8
0
        public Boolean AddOrderDetails(Guid orderId, Guid productId)
        {
            //Creating a new order detail
            OrderDetails o = new OrderDetails();

            o.OrderFk      = orderId;
            o.ProductFk    = productId;
            o.SellingPrice = _context.Products.SingleOrDefault(x => x.Id == productId).Price;

            var stockForProduct = _context.Products.SingleOrDefault(x => x.Id == productId).Stock;

            var orderDetail = _context.OrderDetails.FirstOrDefault(x => x.ProductFk == productId && x.OrderFk == orderId);

            //If there is this product already in the list and the user clicks on add to cart again -> increase the quantity.
            if (stockForProduct > 0)
            {
                if (orderDetail == null)
                {
                    o.Quantity = 1;
                    _context.Add(o);
                    _context.SaveChanges();
                }
                else
                {
                    orderDetail.Quantity++;
                    _context.Update(orderDetail);
                    _context.SaveChanges();
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }
 public void UpdateCartProduct(Cart cart)
 {
     _context.Update(cart);
     _context.SaveChanges();
 }