public IActionResult Create(Product product)
        {
            if (ModelState.IsValid)
            {
                db.Products.Add(product);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(product));
        }
Пример #2
0
        public IActionResult Create(Recall recall)
        {
            if (ModelState.IsValid)
            {
                db.Recalls.Add(recall);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(recall));
        }
Пример #3
0
        public IActionResult Add(int productId, int quantity = 1)
        {
            var userId = userManager.GetUserId(User);

            var product = db.Products
                          .SingleOrDefault(p => p.ProductId == productId);

            if (product == null)
            {
                return(new StatusCodeResult(404));
            }

            var item = db.CartItems
                       .SingleOrDefault(i => i.ProductId == product.ProductId &&
                                        i.UserId == userId);

            if (item != null)
            {
                item.Quantity       += quantity;
                item.PricePerUnit    = product.CurrentPrice;
                item.PriceCalculated = DateTime.Now.ToUniversalTime();
            }
            else
            {
                item = new CartItem
                {
                    ProductId       = product.ProductId,
                    PricePerUnit    = product.CurrentPrice,
                    Quantity        = quantity,
                    UserId          = userId,
                    PriceCalculated = DateTime.Now.ToUniversalTime()
                };
                db.CartItems.Add(item);
            }

            db.SaveChanges();

            return(RedirectToAction("Index", new { message = IndexMessage.ItemAdded }));
        }
Пример #4
0
        public void GetPendingOrders()
        {
            var builder = new DbContextOptionsBuilder <UnicornStoreContext>();

            builder.UseInMemoryStore(persist: true);
            var options = builder.Options;

            using (var context = new UnicornStoreContext(options))
            {
                var orders = new List <Order>
                {
                    new Order {
                        State = OrderState.CheckingOut, ShippingDetails = new OrderShippingDetails()
                    },
                    new Order {
                        State = OrderState.Placed, ShippingDetails = new OrderShippingDetails()
                    },
                    new Order {
                        State = OrderState.Filling, ShippingDetails = new OrderShippingDetails()
                    },
                    new Order {
                        State = OrderState.ReadyToShip, ShippingDetails = new OrderShippingDetails()
                    },
                    new Order {
                        State = OrderState.Shipped, ShippingDetails = new OrderShippingDetails()
                    },
                    new Order {
                        State = OrderState.Delivered, ShippingDetails = new OrderShippingDetails()
                    },
                    new Order {
                        State = OrderState.Cancelled, ShippingDetails = new OrderShippingDetails()
                    },
                };

                context.AddRange(orders);
                context.AddRange(orders.Select(o => o.ShippingDetails));
                context.SaveChanges();
            }

            using (var context = new UnicornStoreContext(options))
            {
                var controller = new ShippingController(context);
                var orders     = controller.PendingOrders();
                Assert.Equal(1, orders.Count());
            }
        }