public ActionResult SupplierIndex()
        {
            ViewBag.BackOffice = true; // tell the view to use the backoffice navbar
            List <Supplier> suppliers = new TravelExpertsEntities1().Suppliers.ToList();

            return(View(suppliers));
        }
        public ActionResult PackageEdit(int id)
        {
            ViewBag.BackOffice = true; // tell the view to use the backoffice navbar
            using (TravelExpertsEntities1 db = new TravelExpertsEntities1())
            {
                Package package = db.Packages.Where(p => p.PackageId == id).SingleOrDefault();
                ViewBag.Products = db.Products.OrderBy(p => p.ProdName).ToList();                                    // pass a list of all products to the view

                Dictionary <string, List <Supplier> > prodSupp        = new Dictionary <string, List <Supplier> >(); // somewhere to put all the suppliers for each product
                Dictionary <string, Supplier>         currentProdSupp = new Dictionary <string, Supplier>();         // somewhere to put all the selected suppliers for the products it already has

                foreach (Product p in (List <Product>)ViewBag.Products)                                              // for each product, get the list of suppliers
                {
                    List <Supplier> s = db.Suppliers.Where(supp =>
                                                           // suppliers where supplier ID is in the list of product_suppliers with p's product ID
                                                           (from ps in db.Products_Suppliers where ps.ProductId == p.ProductId select ps.SupplierId).ToList().Contains(supp.SupplierId)).ToList();
                    prodSupp.Add(p.ProdName, s); // add it to the dictionary
                }

                foreach (Products_Suppliers ps in (package.Products_Suppliers.ToList()))
                {
                    Supplier s = db.Suppliers.Where(supp => supp.SupplierId == ps.SupplierId).SingleOrDefault();
                    currentProdSupp.Add(ps.Product.ProdName, s);
                }
                ViewBag.ProdSupp    = prodSupp;
                ViewBag.CurProdSupp = currentProdSupp;
                return(View(package));
            }
        }
        public ActionResult PackageDetails(int id)
        {
            ViewBag.BackOffice = true; // tell the view to use the backoffice navbar
            Package package = new TravelExpertsEntities1().Packages.Where(p => p.PackageId == id).SingleOrDefault();

            return(View(package));
        }
        public ActionResult SupplierDetails(int id)
        {
            ViewBag.BackOffice = true; // tell the view to use the backoffice navbar
            Supplier supplier = new TravelExpertsEntities1().Suppliers.Where(s => s.SupplierId == id).SingleOrDefault();

            return(View(supplier));
        }
        public ActionResult ProductEdit(int id)
        {
            ViewBag.BackOffice = true; // tell the view to use the backoffice navbar
            Product product = new TravelExpertsEntities1().Products.Where(p => p.ProductId == id).SingleOrDefault();

            return(View(product));
        }
        public ActionResult PackageIndex()
        {
            ViewBag.BackOffice = true; // tell the view to use the backoffice navbar
            List <Package> packages = new TravelExpertsEntities1().Packages.ToList();

            return(View(packages));
        }
        public ActionResult ProductIndex()
        {
            ViewBag.BackOffice = true; // tell the view to use the backoffice navbar
            List <Product> products = new TravelExpertsEntities1().Products.ToList();

            return(View(products));
        }
예제 #8
0
        public ActionResult Login(Customer cust)
        {
            TravelExpertsEntities1 db = new TravelExpertsEntities1();
            bool     confirmPass;
            Customer temp;

            try
            {
                temp = (from c in db.Customers
                        where c.CustEmail == cust.CustEmail
                        select c).Single();

                confirmPass     = temp.VerifyPassword(cust.Password);
                cust.CustomerId = temp.CustomerId;
            }
            catch
            {
                TempData["Status"] = "No user exists";
                return(View());
            }

            if (confirmPass)
            {
                Session["Authenticated"] = true;
                Session["UserName"]      = cust.CustEmail;
                Session["CustID"]        = cust.CustomerId;

                return(RedirectToAction("Index"));
            }
            else
            {
                TempData["Status"] = "Username or password is incorrect";
                return(View());
            }
        }
        public ActionResult SupplierEdit(int id, FormCollection collection)
        {
            try
            {
                using (TravelExpertsEntities1 db = new TravelExpertsEntities1())
                {
                    Supplier editedSupp = db.Suppliers.Where(s => s.SupplierId == id).SingleOrDefault(); // get a reference to the record in the DB for editing

                    editedSupp.SupName = collection["SupName"];                                          // update the supplier name

                    // get a list of their current products
                    List <Products_Suppliers> current = db.Products_Suppliers.Where(ps => ps.SupplierId == id).ToList();

                    // get the list of the products that were selected
                    List <Products_Suppliers> selected = new List <Products_Suppliers>(); // create a placeholder list
                    foreach (Product p in db.Products.ToList())                           // go through each possible product
                    {
                        if (Convert.ToInt32(collection[p.ProdName]) == p.ProductId)       // look for whether it was selected in the form
                        {
                            Products_Suppliers ps = new Products_Suppliers();             // if so, make a new products_supplier
                            ps.ProductId  = p.ProductId;                                  // populate it with product ID
                            ps.SupplierId = id;                                           // and supplier ID
                            selected.Add(ps);                                             // add it to the list
                        }
                    }

                    // for each current product
                    foreach (Products_Suppliers ps in current)
                    {
                        // if it's not in the new list
                        if (!selected.Contains(ps))
                        {
                            // delete it from the DB
                            db.Products_Suppliers.Remove(ps);
                        }
                    }

                    // for each selected product
                    foreach (Products_Suppliers ps in selected)
                    {
                        // if it's not in the old list
                        if (!current.Contains(ps))
                        {
                            // add it
                            db.Products_Suppliers.Add(ps);
                        }
                    }
                    db.SaveChanges();                      // commit
                }
                return(RedirectToAction("SupplierIndex")); // go back to index
            }
            catch                                          // if there's any problem, go back to the form so they can try again
            {
                return(View());
            }
        }
 public ActionResult SupplierCreate()
 {
     ViewBag.BackOffice = true; // tell the view to use the backoffice navbar
     using (TravelExpertsEntities1 db = new TravelExpertsEntities1())
     {
         TempData["Status"] = "";
         ViewBag.Products   = db.Products.ToList(); // pass a list of all products to the view
         ViewBag.NextId     = (from suppliers in db.Suppliers select suppliers.SupplierId).Max() + 1;
         return(View());                            // open the page
     }
 }
        public ActionResult SupplierEdit(int id)
        {
            ViewBag.BackOffice = true; // tell the view to use the backoffice navbar
            using (TravelExpertsEntities1 db = new TravelExpertsEntities1())
            {
                Supplier supplier  = db.Suppliers.Where(s => s.SupplierId == id).SingleOrDefault();                              // get this supplier
                int?[]   offerings = (from ps in db.Products_Suppliers where ps.SupplierId == id select ps.ProductId).ToArray();
                ViewBag.Products  = db.Products.ToList();                                                                        // pass a list of all products to the view
                ViewBag.Offerings = (from prods in db.Products where offerings.Contains(prods.ProductId) select prods).ToList(); // pass a list of products they already have

                return(View(supplier));                                                                                          // display the page
            }
        }
예제 #12
0
        /// <summary>
        /// Go to the register page:
        /// If logged in, pass the current customer object
        /// If not, pass null
        /// (The view will then display as either a new registration or an account edit)
        /// Author: TH
        /// </summary>
        public ActionResult Register()
        {
            Customer cust = null;

            ViewBag.Message = "Customer registration page.";
            if (Session["Authenticated"] != null && (bool)Session["Authenticated"]) // if a customer is logged in
            {
                // get the username which is logged in
                string username = (string)Session["Username"];
                // get the customer from the DB that has the same email as the logged in customer
                cust = new TravelExpertsEntities1().Customers.Where(c => c.CustEmail == username).Single();
            }

            return(View(cust));
        }
예제 #13
0
        /// <summary>
        /// Go to the contact page, passing in the agency and agent info from the DB
        /// Author: TH
        /// </summary>
        public ActionResult Contact()
        {
            //get a list of agencies
            List <Agency> agencies = new TravelExpertsEntities1().Agencies.ToList();

            foreach (Agency a in agencies) // convert to nice looking phone numbers for display
            {                              // credit to teo van kot @ https://stackoverflow.com/questions/32727346/format-string-phone-number-mvc-razor-without-parsing-to-decimal
                a.AgncyPhone = String.Format("({0}) {1}-{2}", a.AgncyPhone.Substring(0, 3), a.AgncyPhone.Substring(3, 3),
                                             a.AgncyPhone.Substring(6, a.AgncyPhone.Length - 6));
                a.AgncyFax = String.Format("({0}) {1}-{2}", a.AgncyFax.Substring(0, 3), a.AgncyFax.Substring(3, 3),
                                           a.AgncyFax.Substring(6, a.AgncyFax.Length - 6));
            }

            //pass it to the contact page
            return(View(agencies));
        }
 public ActionResult ProductCreate(Product prod)
 {
     try
     {
         using (TravelExpertsEntities1 db = new TravelExpertsEntities1())
         {
             db.Products.Add(prod);                // add the new product to the database
             db.SaveChanges();                     // commit
         }
         return(RedirectToAction("ProductIndex")); // go back to the product listing
     }
     catch
     {
         return(View());
     }
 }
        public ActionResult PackageCreate(Package pkg, FormCollection collection)
        {
            TempData["Status"] = ""; // reset any error message

            // first: make sure dates are valid
            DateTime?start = Convert.ToDateTime(collection["PkgStartDate"]);  // get the dates
            DateTime?end   = Convert.ToDateTime(collection["PkgEndDate"]);

            // new packages must have dates after today
            if (start < DateTime.Now || end < DateTime.Now)
            {
                TempData["Status"] = "Start and end dates must be in the future";
                return(RedirectToAction("PackageCreate")); // go back to the page for correction
            }

            // if the start is after the end
            if (start > end)
            {
                TempData["Status"] = "Start date must come before end date";
                return(RedirectToAction("PackageCreate")); // go back to the page for correction
            }

            try
            {
                using (TravelExpertsEntities1 db = new TravelExpertsEntities1())
                {
                    foreach (Product p in db.Products.ToList())                 // for each product, see whether it was selected on the form
                    {
                        int?prodSupp = Convert.ToInt32(collection[p.ProdName]); // get the supplier selection from the dropdown
                        if (prodSupp > 0)                                       // if it was actually selected
                        {
                            // get the database record in products_suppliers that matches this productID and supplierID and add it to the package
                            pkg.Products_Suppliers.Add(db.Products_Suppliers.Where(ps => ps.ProductId == p.ProductId && ps.SupplierId == prodSupp).SingleOrDefault());
                        }
                    }

                    db.Packages.Add(pkg);                 // add the new package to the database
                    db.SaveChanges();                     // commit
                }
                return(RedirectToAction("PackageIndex")); // go back to the package listing
            }
            catch
            {
                return(View());
            }
        }
        public ActionResult ProductEdit(Product prod)
        {
            try
            {
                using (TravelExpertsEntities1 db = new TravelExpertsEntities1())
                {
                    Product editedProd = db.Products.Where(p => p.ProductId == prod.ProductId).SingleOrDefault();

                    editedProd.ProdName = prod.ProdName;

                    db.SaveChanges();
                }
                return(RedirectToAction("ProductIndex"));
            }
            catch
            {
                return(View());
            }
        }
        public ActionResult PackageCreate()
        {
            ViewBag.BackOffice = true; // tell the view to use the backoffice navbar
            using (TravelExpertsEntities1 db = new TravelExpertsEntities1())
            {
                ViewBag.Products = db.Products.OrderBy(p => p.ProdName).ToList();                             // pass a list of all products to the view

                Dictionary <string, List <Supplier> > prodSupp = new Dictionary <string, List <Supplier> >(); // somewhere to put all the suppliers for each product

                foreach (Product p in (List <Product>)ViewBag.Products)                                       // for each product, get the list of suppliers
                {
                    List <Supplier> s = db.Suppliers.Where(supp =>
                                                           // suppliers where supplier ID is in the list of product_suppliers with p's product ID
                                                           (from ps in db.Products_Suppliers where ps.ProductId == p.ProductId select ps.SupplierId).ToList().Contains(supp.SupplierId)).ToList();
                    prodSupp.Add(p.ProdName, s); // add it to the dictionary
                }
                ViewBag.ProdSupp = prodSupp;
                return(View()); // open the page
            }
        }
        public ActionResult SupplierCreate(Supplier supp, FormCollection collection)
        {
            try
            {
                using (TravelExpertsEntities1 db = new TravelExpertsEntities1())
                {
                    db.Suppliers.Add(supp);                                               // add the new supplier to the database
                    List <Products_Suppliers> prodlist = new List <Products_Suppliers>(); // a list to hold their product offerings

                    foreach (Product prod in db.Products.ToList())                        // for each potential product that could exist
                    {
                        if (Convert.ToInt32(collection[prod.ProdName]) == prod.ProductId) // see if its box was checked in the form
                        {
                            Products_Suppliers offering = new Products_Suppliers();       // make a new object to hold the offering details
                            offering.SupplierId = supp.SupplierId;
                            offering.ProductId  = prod.ProductId;
                            db.Products_Suppliers.Add(offering); // add it to the database
                        }
                    }

                    db.SaveChanges(); // commit
                }
                TempData["Status"] = "";
                return(RedirectToAction("SupplierIndex")); // go back to the supplier listing
            }
            catch (Exception ex)
            {                                                                                           // this isn't being caught as an "SqlException" for some reason, so have to resort to this
                if (ex.GetBaseException().GetType().ToString() == "System.Data.SqlClient.SqlException") // if they tried to use a duplicate primary key
                {
                    TempData["Status"] = "That supplier ID is already in use. Please choose another number.";
                }
                else // otherwise...
                {
                    TempData["Status"] = ex.GetBaseException().GetType().ToString() + ": " + ex.GetBaseException().Message;
                }
                return(View()); // go back to the creation page for them to try again
            }
        }
예제 #19
0
        public ActionResult Register(Customer cust)
        {
            // setup a DataAccess object
            TravelExpertsEntities1 db = new TravelExpertsEntities1();

            TempData["Status"] = "";                       // This If block author: TH
            if (!ModelState.IsValid)                       // the model will be invalid if this is an edit because the password wasn't entered
            {                                              // so we have to validate the password here
                if (cust.Password != cust.ComparePassword) // if the passwords aren't the same
                {
                    TempData["Status"] = "Passwords do not match. Please try again.";
                    return(View(cust));
                }
                else if (cust.Password == "" || cust.Password == null) // if nothing was entered, they either didn't click the change button
                {                                                      // or decided not to change it. so we'll plop in their current password & continue
                    string   username = (string)Session["UserName"];
                    Customer oldCust  = db.Customers.Where(c => c.CustEmail == username).Single();
                    cust.Password        = oldCust.Password;
                    cust.ComparePassword = cust.Password;
                }
                else // otherwise the password must not be a valid password
                {
                    TempData["Status"] = "Invalid password entered. Please try again.";
                    return(View(cust));
                }
            }

            if (cust != null) // make sure the passed info exists
            {
                // Checks DB for existing record that matches First and Last names and Email address
                int found = Convert.ToInt32((from c in db.Customers
                                             where c.CustEmail == cust.CustEmail
                                             select c.CustomerId).SingleOrDefault());

                if (found == 0 || cust.CustEmail == (string)Session["UserName"]) // if there is no conflict with existing email
                {
                    // if block Author: TH (for Task 2)
                    // if the account is already logged in, then this is an edit not a create
                    if (Session["Authenticated"] != null && (bool)Session["Authenticated"]) // if already logged in
                    {
                        // get the username
                        string username = (string)Session["UserName"];

                        // get the record with the old username
                        Customer custRecord = (from c in db.Customers
                                               where c.CustEmail == username
                                               select c).SingleOrDefault();

                        //update each field
                        custRecord.Password        = cust.EncryptPassword(cust.Password);
                        custRecord.ComparePassword = custRecord.Password;
                        custRecord.CustAddress     = cust.CustAddress;
                        custRecord.CustBusPhone    = cust.CustBusPhone;
                        custRecord.CustCity        = cust.CustCity;
                        custRecord.CustCountry     = cust.CustCountry;
                        custRecord.CustEmail       = cust.CustEmail;
                        custRecord.CustFirstName   = cust.CustFirstName;
                        custRecord.CustHomePhone   = cust.CustHomePhone;
                        custRecord.CustLastName    = cust.CustLastName;
                        custRecord.CustPostal      = cust.CustPostal;
                        custRecord.CustProv        = cust.CustProv;

                        // commit
                        db.SaveChanges();

                        Session["UserName"] = cust.CustEmail;                     // in case they updated their email
                        TempData["Status"]  = "User Account Successfully Edited"; // set the Result status
                        return(View());                                           // we're done here
                    }
                    else // this is a new user
                    {
                        //Encrypt the password using BCrypt
                        cust.Password = cust.EncryptPassword(cust.Password);
                        // set ComparePassword to encrypted password so validation passes
                        // we've already verified they were the same before submit
                        cust.ComparePassword = cust.Password;
                        db.Customers.Add(cust);                               // add the Customer record
                        db.SaveChanges();                                     // comit the changes
                        TempData["Status"]       = "Registration Successful"; // set the Result status
                        Session["Authenticated"] = true;
                        Session["UserName"]      = cust.CustEmail;
                        Session["CustID"]        = Convert.ToInt32((from c in db.Customers where c.CustEmail == cust.CustEmail select c.CustomerId).Single());

                        return(RedirectToAction("Index")); // go back to 'Home'
                    }
                }
                else // email already in use

                {
                    TempData["Status"] = "That email is already in our system. Please contact us if you forgot your password"; // set the Result status
                    return(View());
                }
            }
            else
            {
                return(View());// no data present, return to Regisrtation form
            }
        }
        public ActionResult PackageEdit(int id, FormCollection collection)
        {
            TempData["Status"] = ""; // reset any error message

            // first: make sure dates are valid
            DateTime?start = Convert.ToDateTime(collection["PkgStartDate"]);  // get the dates
            DateTime?end   = Convert.ToDateTime(collection["PkgEndDate"]);

            // if the start is after the end
            if (start > end)
            {
                TempData["Status"] = "Start date must come before end date";
                return(RedirectToAction("PackageEdit")); // go back to the page for correction
            }

            // then update the DB
            try
            {
                using (TravelExpertsEntities1 db = new TravelExpertsEntities1())
                {
                    Package editedPkg = db.Packages.Where(p => p.PackageId == id).SingleOrDefault(); // get this record from the DB

                    // update all the fields
                    editedPkg.PkgAgencyCommission = Convert.ToDecimal(collection["PkgAgencyCommission"]);
                    editedPkg.PkgBasePrice        = Convert.ToDecimal(collection["PkgBasePrice"]);
                    editedPkg.PkgDesc             = collection["PkgDesc"];
                    editedPkg.PkgEndDate          = Convert.ToDateTime(collection["PkgEndDate"]);
                    editedPkg.PkgImageFile        = collection["PkgImageFile"];
                    editedPkg.PkgName             = collection["PkgName"];
                    editedPkg.PkgStartDate        = Convert.ToDateTime(collection["PkgStartDate"]);

                    List <Products_Suppliers> changes = new List <Products_Suppliers>();// somewhere to put the changes we're making below

                    // any product_supplier that's in the original package but not in the form selections, remove
                    foreach (Products_Suppliers ps in editedPkg.Products_Suppliers)
                    {
                        int suppID = Convert.ToInt32(collection[ps.Product.ProdName]); // get the drop down matching that product name in the form
                        if (!(suppID > 0))                                             // if a supplier wasn't selected
                        {
                            changes.Add(ps);                                           // put on the list of things to remove
                        }
                    }

                    foreach (Products_Suppliers ps in changes)// remove them from the db reference
                    {
                        editedPkg.Products_Suppliers.Remove(ps);
                    }

                    changes.Clear(); // clear the list for the next part

                    // anything that's in the form selections but not in the original package, add
                    foreach (Product p in db.Products.ToList())
                    {
                        int suppID = Convert.ToInt32(collection[p.ProdName]); // get the drop down matching that product name in the form

                        if (suppID > 0)                                       // if a supplier was selected
                        {
                            // get that product_supplier from the DB
                            Products_Suppliers testCase = db.Products_Suppliers.Where(ps => ps.SupplierId == suppID && ps.ProductId == p.ProductId).Single();
                            if (!editedPkg.Products_Suppliers.Contains(testCase)) // if it's not in the original list
                            {
                                changes.Add(testCase);                            // add it to the list of changes
                            }
                        }
                    }

                    foreach (Products_Suppliers ps in changes)
                    {
                        editedPkg.Products_Suppliers.Add(ps); // add it to the db reference
                    }

                    db.SaveChanges(); // commit
                }
                return(RedirectToAction("PackageIndex"));
            }
            catch (Exception ex) // if something went wrong, return them to this page to try again
            {
                TempData["Status"] = ex.GetType().ToString() + ": " + ex.Message;
                return(View());
            }
        }