private List <AdvertisementPhoto> CreateAdvertisementPhotosModels(IEnumerable <string> advertisementPhotosNames) { var photosDbModelsList = new List <AdvertisementPhoto>(); foreach (var photoName in advertisementPhotosNames) { var model = new AdvertisementPhoto(); model.PhotoName = photoName; model.IsMainPhoto = photoName.StartsWith("mini"); photosDbModelsList.Add(model); } return(photosDbModelsList); }
public ActionResult EditPA(EditPAViewModel viewModel, IEnumerable <HttpPostedFileBase> uploads) { viewModel.Locations = db.Locations.ToList(); viewModel.Difficulties = db.Difficulties.ToList(); var selectedCategories = new List <Category>(); var postedCategoryIds = new string[0]; if (viewModel.PostedCategories == null) { viewModel.PostedCategories = new PostedCategories(); } if (viewModel.PostedCategories.CategoriesIds != null) { postedCategoryIds = viewModel.PostedCategories.CategoriesIds; } if (postedCategoryIds.Any()) { selectedCategories = db.Categories.Where(x => postedCategoryIds.Any(s => x.Id.ToString().Equals(s))).ToList(); } viewModel.AvaibleCategories = new List <CategoryViewModel>(); var categories = db.Categories; foreach (Category c in categories) { var cvm = new CategoryViewModel(); cvm.Id = c.Id; cvm.Name = c.Name; viewModel.AvaibleCategories.Add(cvm); } viewModel.SelectedCategories = new List <CategoryViewModel>(); foreach (Category c in selectedCategories) { var cvm = new CategoryViewModel(); cvm.Id = c.Id; cvm.Name = c.Name; viewModel.SelectedCategories.Add(cvm); } var adv = db.Advertisements.Find(viewModel.Advertisement.Id); try { if (ModelState.IsValid) { adv.Title = viewModel.Advertisement.Title; adv.Content = viewModel.Advertisement.Content; adv.DifficultyId = viewModel.Advertisement.DifficultyId; adv.LocationId = viewModel.Advertisement.LocationId; bool canDelAdvPhotos = false; foreach (var upload in uploads) { if (upload != null && upload.ContentLength > 0) { canDelAdvPhotos = true; } } if (canDelAdvPhotos) { db.AdvertisementPhotos.RemoveRange(adv.AdvertisementPhotos); } foreach (var upload in uploads) { if (upload != null && upload.ContentLength > 0) { var photo = new AdvertisementPhoto { FileName = System.IO.Path.GetFileName(upload.FileName), ContentType = upload.ContentType }; using (var reader = new System.IO.BinaryReader(upload.InputStream)) { photo.FileContent = reader.ReadBytes(upload.ContentLength); } adv.AdvertisementPhotos.Add(photo); } } adv.Categories.Clear(); foreach (var c in viewModel.SelectedCategories) { var cat = db.Categories.Find(c.Id); adv.Categories.Add(cat); } db.SaveChanges(); return(RedirectToAction("Index")); } } catch (DbEntityValidationException e) { var error = e.EntityValidationErrors.First().ValidationErrors.First(); this.ModelState.AddModelError(error.PropertyName, error.ErrorMessage); } return(View(viewModel)); }
public ActionResult CreateAdvertisement(CreateAdverstisementViewModel viewModel, IEnumerable <HttpPostedFileBase> uploads) { viewModel.Locations = db.Locations.ToList(); viewModel.Difficulties = db.Difficulties.ToList(); var selectedCategories = new List <Category>(); var postedCategoryIds = new string[0]; if (viewModel.PostedCategories == null) { viewModel.PostedCategories = new PostedCategories(); } if (viewModel.PostedCategories.CategoriesIds != null) { postedCategoryIds = viewModel.PostedCategories.CategoriesIds; } if (postedCategoryIds.Any()) { selectedCategories = db.Categories.Where(x => postedCategoryIds.Any(s => x.Id.ToString().Equals(s))).ToList(); } viewModel.AvaibleCategories = new List <CategoryViewModel>(); var categories = db.Categories; foreach (Category c in categories) { var cvm = new CategoryViewModel(); cvm.Id = c.Id; cvm.Name = c.Name; viewModel.AvaibleCategories.Add(cvm); } viewModel.SelectedCategories = new List <CategoryViewModel>(); foreach (Category c in selectedCategories) { var cvm = new CategoryViewModel(); cvm.Id = c.Id; cvm.Name = c.Name; viewModel.SelectedCategories.Add(cvm); } try { if (ModelState.IsValid) { foreach (var upload in uploads) { if (upload != null && upload.ContentLength > 0) { var photo = new AdvertisementPhoto { FileName = System.IO.Path.GetFileName(upload.FileName), ContentType = upload.ContentType }; using (var reader = new System.IO.BinaryReader(upload.InputStream)) { photo.FileContent = reader.ReadBytes(upload.ContentLength); } viewModel.Advertisement.AdvertisementPhotos.Add(photo); } } viewModel.Advertisement.AddDate = DateTime.Now; viewModel.Advertisement.PerformanceId = 1; viewModel.Advertisement.AuthorId = User.Identity.GetUserId(); viewModel.Advertisement.IsFinished = false; foreach (var c in viewModel.SelectedCategories) { var cat = db.Categories.Find(c.Id); viewModel.Advertisement.Categories.Add(cat); } db.Advertisements.Add(viewModel.Advertisement); db.SaveChanges(); return(RedirectToAction("Index")); } } catch (DbEntityValidationException e) { var error = e.EntityValidationErrors.First().ValidationErrors.First(); this.ModelState.AddModelError(error.PropertyName, error.ErrorMessage); } return(View(viewModel)); }