public ActionResult Create([Bind(Include = "ProductID,ProductName,SupplierID,CategoryID,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Picture,Discontinued")] Product product)
        {
            if (ModelState.IsValid)
            {
                db.Products.Add(product);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", product.CategoryID);
            ViewBag.SupplierID = new SelectList(db.Suppliers, "SupplierID", "CompanyName", product.SupplierID);
            return(View(product));
        }
예제 #2
0
 public ActionResult Register([Bind(Include = "Email,Password,CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Fax")] CustomerRegister customerRegister)
 {
     // Add new customer to database
     using (Northwnd db = new Northwnd())
     {
         if (ModelState.IsValid)
         {
             // create customer
             Customer customer = customerRegister.MapToCustomer();
             // first, make sure the CompanyName is unique
             if (db.Customers.Any(c => c.CompanyName == customer.CompanyName))
             {
                 // duplicate CompanyName
                 return(View());
             }
             // Generate guid for this customer
             customer.UserGuid = System.Guid.NewGuid();
             // Hash & Salt the customer Password using SHA-1 algorithm
             customer.Password = UserAccount.HashSHA1(customer.Password + customer.UserGuid);
             // save customer to database
             db.Customers.Add(customer);
             db.SaveChanges();
             return(RedirectToAction(actionName: "Index", controllerName: "Home"));
         }
     }
     //validation error
     return(View());
 }
예제 #3
0
        public JsonResult AddToCart(CartDTO cartDTO)
        {
            //getting mapped by serializer
            //make sure its valid
            if (!ModelState.IsValid)
            {
                Response.StatusCode = 400;
                return(Json(new { }, JsonRequestBehavior.AllowGet));
            }

            // create cart item from Json object
            Cart sc = new Cart();

            sc.ProductID  = cartDTO.ProductID;
            sc.CustomerID = cartDTO.CustomerID;
            sc.Quantity   = cartDTO.Quantity;

            //database
            using (var db = new Northwnd())
            {
                // add the product to the customer’s cart
                //db.Carts.Add(sc);
                //db.SaveChanges();

                // if there is a duplicate product id in cart, simply update the quantity
                //if (db.Carts.Where(c=>c.ProductID == sc.ProductID && c.CustomerID == sc.CustomerID).Any())
                if (db.Carts.Any(c => c.ProductID == sc.ProductID && c.CustomerID == sc.CustomerID))
                {
                    // this product is already in the customer's cart,
                    // update the existing cart item's quantity
                    //Cart cart = db.Carts.Where(c => c.ProductID == sc.ProductID && c.CustomerID == sc.CustomerID).FirstOrDefault();
                    //gets the cart first
                    Cart cart = db.Carts.FirstOrDefault(c => c.ProductID == sc.ProductID && c.CustomerID == sc.CustomerID);
                    cart.Quantity += sc.Quantity;
                    sc             = new Cart()
                    {
                        CartID     = cart.CartID,
                        ProductID  = cart.ProductID,
                        CustomerID = cart.CustomerID,
                        Quantity   = cart.Quantity
                    };
                }
                else
                {
                    // this product is not in the customer's cart, add the product
                    db.Carts.Add(sc);
                }
                db.SaveChanges();
            }

            return(Json(sc, JsonRequestBehavior.AllowGet));
        }
예제 #4
0
        public ActionResult Account([Bind(Include = "CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Fax,Email")] CustomerEdit UpdatedCustomer)
        {
            // For future version, make sure that an authenticated user is a customer
            if (Request.Cookies["role"].Value != "customer")
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (Northwnd db = new Northwnd())
            {
                if (ModelState.IsValid)
                {
                    Customer customer = db.Customers.Find(UserAccount.GetUserID());
                    //customer.CompanyName = UpdatedCustomer.CompanyName;
                    // if the customer is changing their CompanyName
                    if (customer.CompanyName.ToLower() != UpdatedCustomer.CompanyName.ToLower())
                    {
                        // Ensure that the CompanyName is unique
                        if (db.Customers.Any(c => c.CompanyName == UpdatedCustomer.CompanyName))
                        {
                            // duplicate CompanyName
                            ModelState.AddModelError("CompanyName", "Duplicate Company Name");
                            return(View(UpdatedCustomer));
                        }
                        customer.CompanyName = UpdatedCustomer.CompanyName;
                    }
                    customer.Address      = UpdatedCustomer.Address;
                    customer.City         = UpdatedCustomer.City;
                    customer.ContactName  = UpdatedCustomer.ContactName;
                    customer.ContactTitle = UpdatedCustomer.ContactTitle;
                    customer.Country      = UpdatedCustomer.Country;
                    customer.Email        = UpdatedCustomer.Email;
                    customer.Fax          = UpdatedCustomer.Fax;
                    customer.Phone        = UpdatedCustomer.Phone;
                    customer.PostalCode   = UpdatedCustomer.PostalCode;
                    customer.Region       = UpdatedCustomer.Region;

                    db.SaveChanges();
                    return(RedirectToAction(actionName: "Index", controllerName: "Home"));
                }
                //validation error
                return(View(UpdatedCustomer));
            }
        }