Exemple #1
0
        public ActionResult Create([Bind(Include = "IdOrder,IdCustomer,OrderDate")] Order order)
        {
            if (ModelState.IsValid)
            {
                db.Orders.Add(order);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.IdCustomer = new SelectList(db.Customers, "IdCustomer", "Nazwa", order.IdCustomer);
            return(View(order));
        }
        public IHttpActionResult PostProduct(ProductModel p)
        {
            using (var db = new SportsStoreDbContext()) {
                var product = db.Products.Find(p.Id);
                if (product == null)
                {
                    product = new Product {
                        Name        = p.Name,
                        Description = p.Description,
                        Category    = p.Category,
                        Price       = p.Price
                    };

                    db.Products.Add(product);
                }
                else
                {
                    product.Name        = p.Name;
                    product.Description = p.Description;
                    product.Category    = p.Category;
                    product.Price       = p.Price;
                }

                db.SaveChanges();

                return(Ok(new {
                    id = product.Id,
                    name = product.Name,
                    description = product.Description,
                    price = product.Price
                }));
            }
        }
Exemple #3
0
        public IHttpActionResult PlaceOrder(PlaceOrderRequest request)
        {
            using (var db = new SportsStoreDbContext()) {
                var order = new Order {
                    Name     = request.Name,
                    Street   = request.Street,
                    City     = request.City,
                    State    = request.State,
                    Zip      = request.Zip,
                    Country  = request.Country,
                    GiftWrap = request.GiftWrap
                };

                foreach (var item in request.Products)
                {
                    var product = db.Products.Find(item.Id);
                    if (product == null)
                    {
                        return(BadRequest(string.Format("订单中商品[{0}]不存在", item.Id)));
                    }

                    order.OrderProducts.Add(new OrderProduct {
                        ProductId = product.Id,
                        Count     = item.Count
                    });
                }

                db.Orders.Add(order);
                db.SaveChanges();

                return(Ok(new { id = order.Id }));
            }
        }
Exemple #4
0
        public void Save(Order order)
        {
            _db.AttachRange(order.CartLines.Select(c => c.Product));

            if (order.Id == 0)
            {
                _db.Orders.Add(order);
            }

            _db.SaveChanges();
        }
Exemple #5
0
        public void Save(Product product)
        {
            if (product.Id == 0)
            {
                _db.Products.Add(product);
            }
            else
            {
                Product currentProduct = _db.Products.FirstOrDefault(p => p.Id == product.Id);

                if (currentProduct != null)
                {
                    currentProduct.Name        = product.Name;
                    currentProduct.Description = product.Description;
                    currentProduct.Price       = product.Price;
                    currentProduct.Category    = product.Category;
                }
            }

            _db.SaveChanges();
        }
 public AddToCartResponse AddToCart(AddToCartRequest request)
 {
     _DbContext.ShoppingCarts.Add(new ShoppingCart
     {
         ProductId             = request.ProductId,
         OrderRegistrationDate = DateTime.Now
     });
     _DbContext.SaveChanges();
     return(new AddToCartResponse
     {
         NumberOfRecords = _DbContext.ShoppingCarts.Count()
     });
 }
        public IHttpActionResult DeleteProduct(int id)
        {
            using (var db = new SportsStoreDbContext()) {
                var p = db.Products.Find(id);

                if (p == null)
                {
                    return(NotFound());
                }
                db.Products.Remove(p);
                db.SaveChanges();
            }

            return(Ok());
        }
Exemple #8
0
        private async Task SeedProducts(SportsStoreDbContext db)
        {
            Product[] products =
            {
                new Product
                {
                    Name        = "Kayak",
                    Description = "A boat for one person",
                    Category    = "Watersports",
                    Price       = 275
                },
                new Product
                {
                    Name        = "Lifejacket",
                    Description = "Protective and fashionable",
                    Category    = "Watersports",
                    Price       = 48.95m
                },
                new Product
                {
                    Name        = "Soccer Ball",
                    Description = "FIFA-approved size and weight",
                    Category    = "Soccer",
                    Price       = 19.50m
                },
                new Product
                {
                    Name        = "Corner Flags",
                    Description = "Give your playing field a professional touch",
                    Category    = "Soccer",
                    Price       = 34.95m
                },
                new Product
                {
                    Name        = "Stadium",
                    Description = "Flat-packed 35,000-seat stadium",
                    Category    = "Soccer",
                    Price       = 79500
                },
                new Product
                {
                    Name        = "Thinking Cap",
                    Description = "Improve brain efficiency by 75%",
                    Category    = "Chess",
                    Price       = 16
                },
                new Product
                {
                    Name        = "Unsteady Chair",
                    Description = "Secretly give your opponent a disadvantage",
                    Category    = "Chess",
                    Price       = 29.95m
                },
                new Product
                {
                    Name        = "Human Chess Board",
                    Description = "A fun game for the family",
                    Category    = "Chess",
                    Price       = 75
                },
                new Product
                {
                    Name        = "Bling-Bling King",
                    Description = "Gold-plated, diamond-studded King",
                    Category    = "Chess",
                    Price       = 1200
                }
            };

            await db.Products.AddRangeAsync(products);

            db.SaveChanges();
        }