public ActionResult Create([Bind(Include = "PackageId,PkgName,PkgStartDate,PkgEndDate,PkgDesc,PkgBasePrice,PkgAgencyCommission")] Package package) { if (ModelState.IsValid) { db.Packages.Add(package); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(package)); }
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 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 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 } }
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()); } }