Ejemplo n.º 1
0
        public static void AddProductToCart(int productID, int customerID, int quantity)
        {
            using (var db = new ShopModel())
            {
                //LINQ & Lambda
               var cart =
                    db.ShoppingCarts.Where(s => s.CustomerID == customerID).FirstOrDefault();
                if (cart == null)
                {
                    // Did not find a cart
                    cart = new ShoppingCart
                    {
                        CustomerID = customerID
                    };
                    db.ShoppingCarts.Add(cart);
                }
                var product = db.Products.Where(p => p.ProductID == productID).FirstOrDefault();
                if (product != null)
                {
                    var detailEntry = new ShoppingCartDetail
                    {
                        ShoppingCartID = cart.ShoppingCartID,
                        ProductId = product.ProductID,
                        Quantity = quantity,
                        SubTotal = quantity * product.Price
                    };
                    db.ShoppingCartDetails.Add(detailEntry);
                }

                db.SaveChanges();
            }
        }
Ejemplo n.º 2
0
 // public static OrderHeader CreateOrder
 public static void AddProductToCart(int productId, int customerId, int quantity)
  {
      using (var db = new ShopModel())
      {
          //LING (language integrated Query) & LAMBDA
          var cart = db.ShoppingCarts.Where(s => s.CustomerId == customerId).FirstOrDefault();
          if (cart == null)
          {
              //did not find a cart - create one
              cart = new ShoppingCart
              {
                  CustomerId = customerId
              };
              db.ShoppingCarts.Add(cart);
          }
          var product = db.Products.Where(p => p.ProductId == productId).FirstOrDefault();
          if (product != null)
          {
              var detailEntry = new ShoppingCartDetail
              {
                  ShoppingCartId = cart.ShoppingCartId,
                  ProductId = product.ProductId,
                  Quantity = quantity,
                  SubTotal = quantity * product.Price
              };
              db.ShoppingCartDetails.Add(detailEntry);                 
          }
          db.SaveChanges();
      }
  }
Ejemplo n.º 3
0
        public static IEnumerable<ShoppingCartDetail> GetShoppingCartDetails(int customerId)
        {
            var db = new ShopModel();
            var shoppingCart = db.ShoppingCarts.Where(s => s.CustomerId == customerId).FirstOrDefault();
            if (shoppingCart == null)
            {
                throw new ArgumentException("Shopping cart not found for the customer");
            }

            return db.ShoppingCartDetails.Where(d => d.ShoppingCartId == shoppingCart.ShoppingCartId);
        }
Ejemplo n.º 4
0
        public static Customer CreateCustomer(string name, int ssn, string address, string emailAdd)
        {
            using (var db = new ShopModel())
            {
                var cust = new Customer();      //same        //Customer customer = new Customer()
                cust.Name = name;
                cust.SSN = ssn;
                cust.Address = address;
                cust.EmailAddress = emailAdd;

                db.Customers.Add(cust);
                db.SaveChanges();
                return cust;
            }
        }
Ejemplo n.º 5
0
        public static Customer CreateCustomer(string name, int ssn, string address, string email)
        {
            using (var db = new ShopModel())
            {
                var c = new Customer();
                c.Name = name;
                c.SSN = ssn;
                c.Address = address;
                c.EmailAddress = email;

                db.Customers.Add(c);
                db.SaveChanges();
                return c;
            }
        }
Ejemplo n.º 6
0
        public static Product CreateProduct(string name, string description, decimal price, decimal weight, string image)
        {
            using (var db = new ShopModel())
            {
                var p = new Product
                {
                    Name = name,
                    Description = description,
                    Weight = weight,
                    Price = price,
                    Image = image
                };
                db.Products.Add(p);
                db.SaveChanges();

                return p;
            }
        }
Ejemplo n.º 7
0
 public static IEnumerable<Product> GetAllProducts()
 {
     var db = new ShopModel();
     return db.Products;
 }
Ejemplo n.º 8
0
 public static IEnumerable<Product> SearchProducts(decimal priceValue)
 {
     var db =new ShopModel();
     return db.Products.Where(p => p.Price <= priceValue);
 }
Ejemplo n.º 9
0
        public static void UpdateProduct(Product updatedProduct)
        {
            using (var db = new ShopModel())
            {
                var product = db.Products.
                    Where(p => p.ProductId == updatedProduct.ProductId).FirstOrDefault();
                if (product == null)
                    return;

                var originalProduct = product;
                product.Name = updatedProduct.Name;
                product.Price = updatedProduct.Price;
                product.Weight = updatedProduct.Weight;
                product.Image = updatedProduct.Image;
                product.Description = updatedProduct.Description;

                db.Entry(originalProduct).CurrentValues.SetValues(product);
                db.SaveChanges();

            }
        }
Ejemplo n.º 10
0
 public static Product GetProductById(int id)
 {
     var db = new ShopModel();
     return db.Products.Where(p => p.ProductId == id).FirstOrDefault();
 }
Ejemplo n.º 11
0
 public static void DeleteProduct(Product product)
 {
     using (var db = new ShopModel())
     {
         db.Products.Remove(product);
         db.SaveChanges();
     }
 }