public async Task <IActionResult> Create([Bind("Id,Name,Price,Quantity,ProductId")] CartItem cartItem)
        {
            if (ModelState.IsValid)
            {
                _context.Add(cartItem);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(Ok(cartItem));
        }
示例#2
0
        public async Task <Order> CreateOrderAsync(Guid customerId, CancellationToken cancellationToken = default)
        {
            using (ShoppingCartDbContext context = _dbContextFactory())
            {
                CustomerEntity customer = context.Customers.SingleOrDefault(a => a.Id == customerId);

                if (customer == null)
                {
                    throw new CustomerNotFoundException($"Customer with ID {customerId} not found");
                }

                OrderEntity entity = new OrderEntity
                {
                    Id        = Guid.NewGuid(),
                    CreatedAt = DateTimeOffset.UtcNow,
                    Customer  = customer,
                };

                context.Orders.Add(entity);

                await context.SaveChangesAsync(cancellationToken);

                return(BuildOrderFromEntity(entity));
            }
        }
        public async Task <OrderItem> CreateOrderItemAsync(Guid orderId, Guid productId, int quantity, CancellationToken cancellationToken = default)
        {
            using (ShoppingCartDbContext context = _dbContextFactory())
            {
                OrderEntity order = context.Orders.SingleOrDefault(a => a.Id == orderId);

                if (order == null)
                {
                    throw new OrderNotFoundException($"Order with ID {orderId} not found");
                }

                ProductEntity product = context.Products.SingleOrDefault(a => a.Id == productId);

                if (product == null)
                {
                    throw new ProductNotFoundException($"Product with ID {productId} not found");
                }

                OrderItemEntity orderItem = new OrderItemEntity
                {
                    Id       = Guid.NewGuid(),
                    Order    = order,
                    Product  = product,
                    Quantity = quantity,
                };

                context.OrderItems.Add(orderItem);

                await context.SaveChangesAsync(cancellationToken);

                return(BuildOrderItemFromEntity(orderItem));
            }
        }
        public async Task <Products> updateProduct(Products products)
        {
            using (var shoppingCartDbContext = new ShoppingCartDbContext())
            {
                shoppingCartDbContext.Products.Update(products);
                await shoppingCartDbContext.SaveChangesAsync();

                return(products);
            }
        }
        public async Task deleteProduct(int id)
        {
            using (var shoppingCartDbContext = new ShoppingCartDbContext())
            {
                var deletedProduct = await getProductById(id);

                shoppingCartDbContext.Products.Remove(deletedProduct);
                await shoppingCartDbContext.SaveChangesAsync();
            }
        }
示例#6
0
        public async Task ClearOrderAsync(Guid orderId, CancellationToken cancellationToken = default)
        {
            using (ShoppingCartDbContext context = _dbContextFactory())
            {
                IEnumerable <OrderItemEntity> orderItems = context.OrderItems.Where(a => a.OrderId == orderId);

                foreach (OrderItemEntity item in orderItems)
                {
                    context.OrderItems.Remove(item);
                }

                await context.SaveChangesAsync(cancellationToken);
            }
        }
        //take the product id
        public async Task deleteProductInCart(int id)
        {
            using (var shoppingCartDbContext = new ShoppingCartDbContext())
            {
                var x      = shoppingCartDbContext.Products.Find(id);
                var cartId = shoppingCartDbContext.Carts.First(y => y.productId == x.productId);
                //if product added before,delete it
                if (shoppingCartDbContext.Carts.Any(y => y.productId == x.productId))
                {
                    shoppingCartDbContext.Carts.Remove(cartId);
                }
                //if product did not add before,do nothing

                await shoppingCartDbContext.SaveChangesAsync();
            }
        }
        public async Task RemoveOrderItemAsync(Guid orderItemId, CancellationToken cancellationToken = default)
        {
            using (ShoppingCartDbContext context = _dbContextFactory())
            {
                OrderItemEntity orderItem = context.OrderItems.SingleOrDefault(a => a.Id == orderItemId);

                if (orderItem == null)
                {
                    throw new OrderItemNotFoundException($"OrderItem with ID {orderItemId} not found");
                }

                context.Remove(orderItem);

                await context.SaveChangesAsync(cancellationToken);
            }
        }
        public async Task <OrderItem> UpdateOrderItemQuantityAsync(Guid orderItemId, int quantity, CancellationToken cancellationToken = default)
        {
            using (ShoppingCartDbContext context = _dbContextFactory())
            {
                OrderItemEntity orderItem = context.OrderItems.Include(a => a.Product)
                                            .SingleOrDefault(a => a.Id == orderItemId);

                if (orderItem == null)
                {
                    throw new OrderItemNotFoundException($"OrderItem with ID {orderItemId} not found");
                }

                orderItem.Quantity = quantity;

                await context.SaveChangesAsync(cancellationToken);

                return(BuildOrderItemFromEntity(orderItem));
            }
        }
 public async Task buy()
 {
     using (var shoppingCartDbContext = new ShoppingCartDbContext())
     {
         var cart         = shoppingCartDbContext.Carts;
         var productTable = shoppingCartDbContext.Products;
         //if cart is not empty, delete the bought items from stock.
         if (cart != null)
         {
             foreach (var x in cart)
             {
                 int id = x.productId;
                 var deletedCartProduct = cart.First(z => z.productId == id);
                 var y = productTable.Find(id);
                 y.stock -= x.piece;
                 cart.Remove(deletedCartProduct);
             }
         }
         await shoppingCartDbContext.SaveChangesAsync();
     }
 }
        public async Task <List <Cart> > productToCart(int id, int quantity)
        {
            using (var shoppingCartDbContext = new ShoppingCartDbContext())
            {
                var x = await shoppingCartDbContext.Products.FirstOrDefaultAsync(x => x.productId == id);

                if (x.stock > quantity)
                {
                    //if product added before increase the piece
                    if (shoppingCartDbContext.Carts.Any(y => y.productId == x.productId))
                    {
                        var prdctCart = shoppingCartDbContext.Carts.FirstOrDefault(y => y.productId == x.productId);

                        if (quantity == null || quantity <= 0)
                        {
                            quantity = 1;
                        }
                        prdctCart.piece += quantity;
                    }
                    else
                    {
                        Cart cart = new Cart
                        {
                            productId  = x.productId,
                            name       = x.name,
                            price      = x.price,
                            piece      = quantity,
                            totalPrice = quantity * x.price
                        };
                        shoppingCartDbContext.Carts.Add(cart);
                    }
                }

                await shoppingCartDbContext.SaveChangesAsync();

                return(await getCart());
            }
        }
 public async Task <bool> Save()
 {
     return((await _context.SaveChangesAsync()) > 0);
 }
示例#13
0
 public async Task <int> SaveChanges()
 {
     return(await Db.SaveChangesAsync());
 }
 public async Task UpdateAsync(ShoppingCartItem item)
 {
     _db.ShoppingCartItems.Update(item);
     await _db.SaveChangesAsync();
 }