public Automobile AddUpdate(int Id, string make, string model, int year, string title, string description, string carfaxReportPath, byte[] carFaxContent, double price, string phoneNumber, List<string> fileNames, string mainImageName, List<byte[]> imageContents, string street1, string street2, bool isHighlight, bool isUsed, string city, string state, string zipCode, int userId, UserType utype, string contactName) { var auto = new Automobile (); if ((!string.IsNullOrEmpty (carfaxReportPath)) && (!StringHelper.IsValidCarFax (carfaxReportPath))) { throw new Exception ("Car Fax Report is not in the right format only pdfs and MS Word documents allowed!"); } auto.ContactName = StringHelper.RemovePossibleXSS(contactName); auto.CarfaxReportPath = carfaxReportPath; auto.City = StringHelper.RemovePossibleXSS (city); auto.Description = StringHelper.RemovePossibleXSS (description); auto.IsHighlight = isHighlight ? 1 : 0; auto.IsUsed = isUsed ? 1 : 0; auto.Make = StringHelper.RemovePossibleXSS (make); auto.Model = StringHelper.RemovePossibleXSS (model); auto.Price = (decimal) price; auto.UserId = userId; auto.Title = StringHelper.RemovePossibleXSS (title); auto.State = StringHelper.RemovePossibleXSS (state); auto.Street1 = StringHelper.RemovePossibleXSS (street1); if (!StringHelper.IsValidTelephone (phoneNumber)) throw new Exception ("Phone Number is Invalid!"); auto.PhoneNumber = phoneNumber; auto.ZipCode = StringHelper.RemovePossibleXSS (zipCode); auto = _autoRepo.AddUpdate (auto, utype); var imgs = new List<Image> (); var imgCount = 0; foreach (var imgFile in fileNames) { try { var type = StringHelper.GetMediaType (imgFile); var img = new Image { IsMainImage = imgFile.EndsWith(mainImageName) ? 1 : 0, Type = (int) type }; if (type != MediaType.Youtube) { img.Url = "/images/automobiles/" + auto.Id + "/" + StringHelper.MakeFileSafe (imgFile); } else { try { var uri = new Uri (imgFile); img.Url = imgFile; } catch { continue; } } img.AutomobileId = auto.Id; imgs.Add (img); //imgCount++; } catch { } } _autoRepo.AddImages (imgs); foreach (var img in imgs) { if (img.MediaType != MediaType.Youtube) { var lastSlash = img.Url.LastIndexOf('/'); var path = ConfigurationManager.AppSettings["MediaRootDir"] + img.Url.Substring(0, lastSlash); if (!Directory.Exists(path)) Directory.CreateDirectory(path); File.WriteAllBytes(ConfigurationManager.AppSettings["MediaRootDir"] + img.Url, imageContents[imgCount]); } imgCount++; } imgs = _autoRepo.GetImagesByAutoId (auto.Id, true); auto.Images = imgs; return auto; }
public Automobile AddUpdate(Automobile auto, UserType type) { FixNulls(auto); return _autoRepo.AddUpdate(auto, type); }
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); //} }
private static void FixNulls(Automobile auto) { auto.State = auto.State ?? "IL"; auto.Street1 = auto.Street1 ?? ""; auto.Street2 = auto.Street2 ?? ""; auto.PhoneNumber = auto.PhoneNumber ?? ""; auto.ZipCode = auto.ZipCode ?? ""; auto.Description = auto.Description ?? ""; auto.Color = auto.Color ?? "Not Given"; auto.ContactName = auto.ContactName ?? "Not Given"; }
protected void btnAdd_Click(object sender, EventArgs e) { Response.Redirect("~/User/MyAutoEdit.aspx", true); var auto = new Automobile(); }