private void Update(Button btn, int value) { var id = int.Parse(btn.CommandArgument); var auto = _autoManager.GetById(id); auto.IsApproved = value; var user = Session["User"] as RollingRides.WebApp.Components.Datalayer.Models.User; _autoManager.AddUpdate(auto, user.UserType); }
protected void btnSave_Click(object sender, EventArgs e) { var id = 0; var auto = new Automobile(); var autos = _autoManager.GetByUserId(((RollingRides.WebApp.Components.Datalayer.Models.User)Session["User"]).Id); if (int.TryParse(lblId.Text, out id)) { auto = _autoManager.GetById(id); var auto2 = autos.SingleOrDefault(x => x.Id == auto.Id); if (auto2 == null) { lblError.Text = "The site is unable to process your request!"; return; } } if (auto.Images == null) { auto.Images = new List <Components.Datalayer.Models.Image>(); } var autoImgs = new List <RollingRides.WebApp.Components.Datalayer.Models.Image>(auto.Images); auto.Images = null; auto.IsUsed = cbxUsed.Checked ? 1 : 0; auto.Make = txtMake.Text; auto.Model = txtModel.Text; auto.PhoneNumber = txtPhoneNumber.Text; decimal d; auto.MinimumDownPayment = decimal.TryParse(txtMinDownPayment.Text, out d) ? d : (decimal?)null; var user = (RollingRides.WebApp.Components.Datalayer.Models.User)Session["User"]; auto.IsApproved = user.UserType == UserType.Admin || user.UserType == UserType.Corporate ? 1 : 0; if (autos.Count() > 4 && user.UserType == UserType.User) { lblError.Text = "You may not add anymore vehicles because you have a restriction as a basic user. Please <a href='/Contact.aspx'> Contact Us </a> to request a Corporate Account with unlimited vehicle advertisements!"; return; } decimal price; auto.Price = decimal.TryParse(txtPrice.Text, out price) ? price : (decimal)0.0; auto.UserId = user.Id; auto.Color = StringHelper.RemovePossibleXSS(txtColor.Text); auto.HasFinancing = cbxFianacing.Checked ? 1 : 0; auto.IsHighlight = 0; auto.Year = int.Parse(ddlYear.SelectedValue); if (cbxUserMyInfo.Checked) { auto.Street1 = user.Street1; auto.Street2 = user.Street2; auto.City = user.City; auto.State = user.State; auto.ZipCode = user.ZipCode; auto.PhoneNumber = user.PhoneNumber; auto.ContactName = user.FirstName + " " + user.LastName; } else { auto.Street1 = txtStreet1.Text; auto.Street2 = txtStreet2.Text; auto.City = txtCity.Text; auto.State = ddlState.SelectedValue; auto.ZipCode = txtZipCode.Text; auto.PhoneNumber = txtPhoneNumber.Text; auto.ContactName = txtContactName.Text; } auto.Description = txtDescription.Text; auto.Title = txtTitle.Text; var youtubeVid = txtYoutube.Text; var imgYoutube = new RollingRides.WebApp.Components.Datalayer.Models.Image { Url = youtubeVid, Type = (int)MediaType.Youtube }; try { if (auto.Images == null) { auto.Images = new List <Components.Datalayer.Models.Image>(); } if (!string.IsNullOrEmpty(imgYoutube.Url)) { var images = new List <RollingRides.WebApp.Components.Datalayer.Models.Image>(autoImgs); foreach (var image in images.Where(x => x.MediaType == MediaType.Youtube)) { try { _autoManager.DeleteImage(image.Id, auto.Id, user.Id); auto.Images.Remove(image); } catch (Exception ex) { } } auto.Images.Add(imgYoutube); } autoImgs = autoImgs.Where(x => (x.MediaType != MediaType.Image) || (x.IsMainImage == 1)).ToList(); foreach (var file in Request.Files.AllKeys.Select(fileStr => Request.Files[fileStr]).Where(file => file.ContentLength <= 10000000).Where(file => !string.IsNullOrEmpty(file.FileName) && file.FileName != fuMainImage.FileName)) { if (StringHelper.IsValidCarFax(file.FileName)) { if (!string.IsNullOrEmpty(auto.CarfaxReportPath) && File.Exists(Server.MapPath(auto.CarfaxReportPath))) { File.Delete(Server.MapPath(auto.CarfaxReportPath)); } var theG = Guid.NewGuid(); auto.CarfaxReportPath = ConfigurationManager.AppSettings["CarfaxPathUrl"] + theG.ToString() + StringHelper.MakeFileSafe(file.FileName); fuCarFax.SaveAs(Server.MapPath(ConfigurationManager.AppSettings["CarfaxPathUrl"]) + theG.ToString() + StringHelper.MakeFileSafe(file.FileName)); continue; } var theGuid = Guid.NewGuid(); var fileType = StringHelper.GetMediaType(file.FileName); var img = new RollingRides.WebApp.Components.Datalayer.Models.Image { Type = (int)fileType, IsMainImage = file.FileName == fuMainImage.FileName ? 1 : 0, Url = ConfigurationManager.AppSettings["imagesFolder"] + theGuid + StringHelper.MakeFileSafe(file.FileName) }; if (fileType == MediaType.Server) { if (!fuVideo.HasFile) { continue; } var serverVid = autoImgs.Where(x => x.Type == (int)MediaType.Server).ToList(); foreach (var image in serverVid) { _autoManager.DeleteImage(image.Id, auto.Id, user.Id); File.Delete(Server.MapPath(image.Url)); } fuVideo.SaveAs(Server.MapPath(ConfigurationManager.AppSettings["imagesFolder"]) + theGuid + StringHelper.MakeFileSafe(file.FileName)); var img1 = new RollingRides.WebApp.Components.Datalayer.Models.Image { Url = ConfigurationManager.AppSettings["imagesFolder"] + theGuid + StringHelper.MakeFileSafe(file.FileName), Type = (int)MediaType.Server, AutomobileId = auto.Id, IsMainImage = 0 }; auto.Images.Add(img1); } else { auto.Images.Add(img); file.SaveAs(Server.MapPath(ConfigurationManager.AppSettings["imagesFolder"]) + theGuid + StringHelper.MakeFileSafe(file.FileName)); } } if (fuMainImage.HasFile) { if (StringHelper.IsValidImage(fuMainImage.FileName)) { var mainImgs = autoImgs.Where(x => x.MediaType == MediaType.Image && x.IsMainImage == 1); var imgs1 = new List <RollingRides.WebApp.Components.Datalayer.Models.Image>(mainImgs); foreach (var mainImg in imgs1) { //var mainImage = mainImg; _autoManager.DeleteImage(mainImg.Id, auto.Id, user.Id); File.Delete(Server.MapPath(mainImg.Url)); //var imgSub = auto.Images.Single(x => x.Id == mainImage.Id); auto.Images.Remove(mainImg); } var mainImg2 = new RollingRides.WebApp.Components.Datalayer.Models.Image { Url = ConfigurationManager.AppSettings["imagesFolder"] + Guid.NewGuid() + fuMainImage.FileName, Type = (int)MediaType.Image, IsMainImage = 1, AutomobileId = auto.Id }; auto.Images.Add(mainImg2); fuMainImage.SaveAs(Server.MapPath(mainImg2.Url)); } } } catch (Exception ex) { lblError.Text = ex.Message; return; } if (auto.CarfaxReportPath == null) { auto.CarfaxReportPath = ""; } try { var temp = _autoManager.AddUpdate(auto, user.UserType); if (temp == null) { lblError.Text = "Failed to Save Vehicle Please Try Again."; } else { lblError.Text = "Vehicle Saved!"; Response.Redirect("/User/MyVehicles.aspx"); } } catch (Exception ex) { lblError.Text = "<strong>Missing Required Fields Failed To Save Vehicle</strong>" + ex; } //foreach (HttpPostedFile file in Request.Files) //{ // file.SaveAs(ConfigurationManager.AppSettings["imagesFolder"] + file.FileName); //} }