Пример #1
0
 public ActionResult Add(User user)
 {
     try
     {
         if (ModelState.IsValid)
         {
             using (ECommerceEntitiesNew Ece = new ECommerceEntitiesNew())
             {
                 var userDetails = Ece.Users.Where(x => x.Name == user.Name).FirstOrDefault();
                 if (userDetails != null)
                 {
                     Console.Write("Username already Exists");
                     return(View("Register"));
                 }
                 else
                 {
                     var newUser = Ece.Users.Create();
                     newUser.Name     = user.Name;
                     newUser.Password = user.Password;
                     newUser.RoleId   = 2;
                     Ece.Users.Add(newUser);
                     Ece.SaveChanges();
                     return(RedirectToAction("Index", "Home"));
                 }
             }
         }
     }
     catch (Exception ex)
     {
         Console.Write(ex.Message);
     }
     ModelState.Clear();
     return(RedirectToAction("Index", "Home"));
 }
Пример #2
0
 public ActionResult Add(Product product, string cid)
 {
     try
     {
         if (ModelState.IsValid)
         {
             using (ECommerceEntitiesNew Ece = new ECommerceEntitiesNew())
             {
                 var productDetails = Ece.Users.Where(x => x.Name == product.Name).FirstOrDefault();
                 if (productDetails != null)
                 {
                     Console.Write("Product already Exists");
                     return(View("New"));
                 }
                 else
                 {
                     var newProduct = Ece.Products.Create();
                     newProduct.Name         = product.Name;
                     newProduct.Price        = product.Price;
                     newProduct.Category     = cid;
                     newProduct.Discontinued = false;
                     Ece.Products.Add(newProduct);
                     Ece.SaveChanges();
                 }
             }
         }
     }
     catch (Exception ex)
     {
         Console.Write(ex.Message);
     }
     return(RedirectToAction("Inventory", "Admin"));
 }
Пример #3
0
        //DELETE /api/customers/1
        public ActionResult DeleteProduct(int id)
        {
            ECommerceEntitiesNew Ece = new ECommerceEntitiesNew();
            var ProductInDb          = Ece.Products.SingleOrDefault(p => p.Id == id);

            Console.Write("Inside delete function");
            if (ProductInDb == null)
            {
                Console.Write("Product not found");
            }
            //Ece.Products.Remove(ProductInDb);
            ProductInDb.Discontinued = true;
            Ece.SaveChanges();
            return(View("Inventory"));
        }
Пример #4
0
        public ActionResult PlaceOrder()
        {
            ECommerceEntitiesNew Ece = new ECommerceEntitiesNew();
            var CartElements         = Ece.Carts;

            foreach (var Element in CartElements)
            {
                if (Element.UId == Convert.ToInt32(Session["userID"]))
                {
                    var newOrder = Ece.Orders.Create();
                    newOrder.UserId    = Element.UId;
                    newOrder.ProductId = Element.PId;
                    newOrder.Quantity  = Element.Quantity.ToString();
                    Ece.Orders.Add(newOrder);
                    Ece.Carts.Remove(Element);
                }
            }
            Ece.SaveChanges();
            Session["Total"] = 0;
            return(View("Cart"));
        }
Пример #5
0
        public ActionResult AddToCart(int id)
        {
            int UserId    = Convert.ToInt32(Session["userID"]);
            int count     = 0;
            int quantity  = 0;
            int ProductId = 0;
            int temp      = id;

            while (temp != 0)
            {
                // n = n/10
                temp /= 10;
                ++count;
            }
            if (count == 5)
            {
                ProductId = id / 10;
                quantity  = id % 10;
            }
            else if (count == 6)
            {
                ProductId = id / 100;
                quantity  = id % 100;
            }
            else
            {
                ProductId = id / 1000;
                quantity  = id % 1000;
            }
            //////////////////////////////////////////////////////////////////////
            try
            {
                if (ModelState.IsValid)
                {
                    using (ECommerceEntitiesNew Ece = new ECommerceEntitiesNew())
                    {
                        var CartDetails = Ece.Carts.Where(x => x.UId == UserId).FirstOrDefault();
                        if (CartDetails != null)
                        {
                            var ProductInDb = Ece.Carts.SingleOrDefault(p => p.PId == ProductId);
                            if (ProductInDb != null && CartDetails.PId == ProductId)
                            {
                                ProductInDb.Quantity = ProductInDb.Quantity + quantity;
                                Ece.SaveChanges();
                                return(View("Shop"));
                            }
                            else
                            {
                                var newCart = Ece.Carts.Create();
                                newCart.UId      = UserId;
                                newCart.PId      = ProductId;
                                newCart.Quantity = quantity;
                                newCart.Status   = false;
                                Ece.Carts.Add(newCart);
                                Ece.SaveChanges();
                            }
                        }
                        else
                        {
                            var newCart = Ece.Carts.Create();
                            newCart.UId      = UserId;
                            newCart.PId      = ProductId;
                            newCart.Quantity = quantity;
                            newCart.Status   = false;
                            Ece.Carts.Add(newCart);
                            Ece.SaveChanges();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
            return(View("Shop"));
            ///////////////////////////////////////////
        }