public ActionResult Edit(int? id) { using (ProductManagementAssignmentEntities db = new ProductManagementAssignmentEntities()) { if (Session["Email"] != null && Session["Id"] != null) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Products product = db.Products.Find(id); Session["smallImage"] = product.SmallImage; Session["largeImage"] = product.LargeImage; if (product == null) { return HttpNotFound(); } ViewBag.id = id; log.Info("Get all the details of selected product for Edit their details inside (Get) method."); return View(product); }//End of if statement else { return RedirectToAction("Login", "Authentication"); } } //End of using statement }
public ActionResult Delete(int? id) { using (ProductManagementAssignmentEntities db = new ProductManagementAssignmentEntities()) { try { Products product = db.Products.Find(id); string smallImage_fileName = product.SmallImage; string smallImage_path = Request.MapPath("~" + smallImage_fileName); /*Check small image is available or not*/ if (System.IO.File.Exists(smallImage_path)) { System.IO.File.Delete(smallImage_path); } string largeImage_fileName = product.LargeImage; string largeImage_path = Request.MapPath("~" + largeImage_fileName); /*Check large image is available or not*/ if (System.IO.File.Exists(largeImage_path)) { System.IO.File.Delete(largeImage_path); } //Delete whole record of that perticular id in database table db.Products.Remove(product); db.SaveChanges(); TempData["message"] = product.Name + "Deleted successfully"; log.Info("The product is deleted with from database."); return RedirectToAction("ProductList", "Display"); } //End of try block. catch (Exception ex) { log.Error(ex, "Error while deleting data."); TempData["message"] = "Error - " + ex.Message; return View(); } } //End of using statement. }
/*For multiple delete (Delete selected item)*/ public ActionResult Selected_delete(String[] checkbox) { try { using (ProductManagementAssignmentEntities db = new ProductManagementAssignmentEntities()) { int[] getId = null; if (checkbox != null) { getId = new int[checkbox.Length]; int j = 0; foreach (string i in checkbox) { int.TryParse(i, out getId[j++]); } List<Products> getproductids = new List<Products>(); getproductids = db.Products.Where(x => getId.Contains(x.Id)).ToList(); /*Remove seleted items*/ foreach (var s in getproductids) { db.Products.Remove(s); } db.SaveChanges(); TempData["message"] = checkbox.Length + " Products Successfully Removed "; log.Info("All selected items are now deleted."); return RedirectToAction("ProductList", "Display"); }//End of if statement }//End of usng statement. } catch (Exception ex) { log.Error(ex, "Error during delet multiple items from database."); TempData["message"] = "Error - " + ex.Message; return View(); } return View(); }
public ActionResult Register(Users users) { if (ModelState.IsValid) { try { using (ProductManagementAssignmentEntities db = new ProductManagementAssignmentEntities()) { var data = db.Users.FirstOrDefault(m => m.Email == users.Email); if (data == null) { try { users.Password = GetMD5(users.Password); //Generate MD5 string for user password db.Users.Add(users); db.SaveChanges(); //All details saved into database. log.Info("User registration completed."); return(RedirectToAction("Login", "Authentication")); } catch (Exception ex) { log.Error(ex, "Error during new user registration."); TempData["message"] = "Error - " + ex.Message; return(View()); } } //End of inner if statement. else { TempData["message"] = "already registered with this email-id."; return(View()); } } // End of using statement. } //End of try block. catch (Exception ex) { log.Error(ex, "Error occured in database."); TempData["message"] = "Error - " + ex.Message; return(View()); } } //End of outer if statement return(View()); }
/*This mthod is used for display list of products that is added by perticular user by verifying it's user-id.*/ public List<Products> GetProducts(string search, string sort, string sortdir, int skip, int pageSize, out int totalRecords) { int id = (int)Session["Id"]; using (ProductManagementAssignmentEntities db = new ProductManagementAssignmentEntities()) { var v = (from a in db.Products where a.User_Id.Equals(id) && (a.Name.Contains(search) || a.Category.Contains(search)) select a); totalRecords = v.Count(); v = v.OrderBy(sort + " " + sortdir); if (pageSize > 0) { v = v.Skip(skip).Take(pageSize); } return v.ToList(); } }
public ActionResult Login(string email, string password) { if (ModelState.IsValid) { try { using (ProductManagementAssignmentEntities db = new ProductManagementAssignmentEntities()) { var mdPass = GetMD5(password); //Generate MD5 string of users password. var data = db.Users.FirstOrDefault(m => m.Email.Equals(email) && m.Password.Equals(mdPass)); if (data != null) { Session["Email"] = email; Session["Id"] = data.Id; log.Info("User log-in successful."); return(RedirectToAction("Home", "Display")); } else { TempData["message"] = "Invalid Email or password."; return(View()); } } //End of using block. } //End of try block. catch (Exception ex) { log.Error(ex, "Error occure during user log-in"); TempData["message"] = "Error - " + ex.Message; return(View()); } } //End of if statement. return(View()); }
public ActionResult AddProduct(HttpPostedFileBase smallImage, HttpPostedFileBase largeImage, Products products) { if (Session["Email"] != null && Session["Id"] != null) { if (ModelState.IsValid) { try { using (ProductManagementAssignmentEntities db = new ProductManagementAssignmentEntities()) { Random r = new Random(); /*Check small image is selected or not*/ if (smallImage != null && smallImage.ContentLength > 0) { /*Get extension of image and check with image extension and store in targeted folder*/ string extension1 = Path.GetExtension(smallImage.FileName); if (extension1.ToLower() == ".png" || extension1.ToLower() == ".jpg" || extension1.ToLower() == ".jpeg") { try { string filename = "_" + RandomString(10) + Path.GetFileName(smallImage.FileName); string path = Path.Combine(Server.MapPath("~/Images"), filename); smallImage.SaveAs(path); products.SmallImage = "/Images/" + filename; log.Info("Small image stored with its ImagePath."); } catch (Exception ex) { log.Error(ex, "Error occured during small image saving into database."); TempData["message"] = "Error - " + ex.Message; return View(); } } else { ViewBag.smallImage = "Please, Select Valid Image !"; return View(); } } //End of if statement else { ViewBag.smallImage_error = "You have not specified a Small Image."; return View(); } /*Check small image is selected or not*/ if (largeImage != null && largeImage.ContentLength > 0) { /*Get extension of image and check with image extension and store in targeted folder*/ string extension2 = Path.GetExtension(largeImage.FileName); if (extension2.ToLower() == ".png" || extension2.ToLower() == ".jpg" || extension2.ToLower() == ".jpeg") { try { string filename = "_" + RandomString(10) + Path.GetFileName(largeImage.FileName); string path = Path.Combine(Server.MapPath("~/Images"), filename); largeImage.SaveAs(path); products.LargeImage = "/Images/" + filename; log.Info("Large image stored with its ImagePath."); } catch (Exception ex) { log.Error(ex, "Error occured during large image saving into database."); TempData["message"] = "Error - " + ex.Message; return View(); } } else { ViewBag.largeImage = "Please, Select Valid Image !"; return View(); } } //End of if statement else { ViewBag.largeImage_error = "You have not specified a Small Image."; return View(); } products.User_Id = (int)Session["Id"]; db.Products.Add(products); db.SaveChanges(); TempData["message"] = products.Name + " Successfully Added ! "; log.Info("New product added by user."); return RedirectToAction("AddProduct", "Display"); } //End of Using statement } //End of try block catch (Exception ex) { log.Error(ex, "Error occured while adding new product."); TempData["message"] = "Error - " + ex.Message; return View(); } } //End of inner if statement }//End of outer if statement else { return RedirectToAction("Login", "Authentication"); } return View(); }
public ActionResult Edit(HttpPostedFileBase smallImage, HttpPostedFileBase largeImage, Products product) { // log.Info("Calling Edit (Post)method of \"Dahsboard\" controller"); using (ProductManagementAssignmentEntities db = new ProductManagementAssignmentEntities()) { if (ModelState.IsValid) { Random random = new Random(); /*This block will execute while both images are changed from previous images.*/ if ((smallImage != null && smallImage.ContentLength > 0) && (largeImage != null && largeImage.ContentLength > 0)) { try { string filename = "_" + RandomString(10) + Path.GetFileName(smallImage.FileName); string path = Path.Combine(Server.MapPath("~/Images"), filename); smallImage.SaveAs(path); product.SmallImage = "/Images/" + filename; string filename1 = "_" + RandomString(10) + Path.GetFileName(largeImage.FileName); string path1 = Path.Combine(Server.MapPath("~/Images"), filename1); largeImage.SaveAs(path1); product.LargeImage = "/Images/" + filename1; } catch (Exception ex) { log.Error(ex, "Error occured while update small and large images."); TempData["message"] = "ERROR:" + ex.Message; return View(); } product.User_Id = (int)Session["Id"]; db.Entry(product).State = EntityState.Modified; db.SaveChanges(); TempData["message"] = product.Name + " successfully Updated"; log.Info("Details of product updated into database."); return RedirectToAction("Edit", "Display"); } //End of inner if block /*This block will execute while only small image is changed and large image will remain same.*/ else if ((smallImage != null && smallImage.ContentLength > 0) && (largeImage == null)) { try { string filename = "_" + RandomString(10) + Path.GetFileName(smallImage.FileName); string path = Path.Combine(Server.MapPath("~/Images"), filename); smallImage.SaveAs(path); product.SmallImage = "/Images/" + filename; product.LargeImage = Session["largeImage"].ToString(); } catch (Exception ex) { log.Error(ex, "Error occured while update small image."); TempData["message"] = "ERROR:" + ex.Message; return View(); } product.User_Id = (int)Session["Id"]; db.Entry(product).State = EntityState.Modified; db.SaveChanges(); TempData["message"] = product.Name + " successfully Updated"; log.Info("Details of product updated into database."); return RedirectToAction("Edit", "Display"); } //End of else if block /*This block will execute while small image will remain same and large image is changed.*/ else if ((smallImage == null) && (largeImage != null && largeImage.ContentLength > 0)) { try { string filename1 = "_" + RandomString(10) + Path.GetFileName(largeImage.FileName); string path1 = Path.Combine(Server.MapPath("~/Images"), filename1); largeImage.SaveAs(path1); product.LargeImage = "/Images/" + filename1; product.SmallImage = Session["smallImage"].ToString(); } catch (Exception ex) { log.Error(ex, "Error occured while update large image."); TempData["message"] = "ERROR:" + ex.Message; return View(); } product.User_Id = (int)Session["Id"]; db.Entry(product).State = EntityState.Modified; db.SaveChanges(); TempData["message"] = product.Name + " successfully Updated"; log.Info("Details of product updated into database."); return RedirectToAction("Edit", "Display"); } //End of else if block /*Everything is valid and update into database*/ else { product.User_Id = (int)Session["Id"]; product.SmallImage = Session["smallImage"].ToString(); product.LargeImage = Session["largeImage"].ToString(); db.Entry(product).State = EntityState.Modified; db.SaveChanges(); TempData["message"] = product.Name + " successfully Updated"; log.Info("Details of product updated into database."); return RedirectToAction("Edit", "Display"); } //End of else block } //End of outer if block } //End of using statement return View(); }