public ActionResult Create(Entry entry, int[] selectedCategories, string huKeywords, string enKeywords) { KeywordsProcedure(entry, huKeywords, enKeywords); var attachments = TempData["Attachments"] as List<string>; var featuredImage = TempData["FeaturedImage"] as List<string>; if (ModelState.IsValid) { int userId = int.Parse((string)Session["UserId"]); entry.Creator = db.Users.Where(e => e.Id == userId).First(); entry.PublishedDate = DateTime.Now; entry.Categories.Clear(); foreach (var id in selectedCategories.ToList()) { Category category = db.Categories.Single(e => e.Id == id); entry.Categories.Add(category); } if (attachments != null) { foreach (var att in attachments) { WebApplication.File newAtt = new WebApplication.File { Entry = entry, Location = Guid.NewGuid().ToString(), Name = Path.GetFileName(att) }; db.Files.Add(newAtt); } } if (featuredImage != null && featuredImage.Count > 0) entry.FeaturedImage = featuredImage[0]; else entry.FeaturedImage = ""; db.Entries.Add(entry); try { db.SaveChanges(); } catch (DbEntityValidationException d) { ViewBag.Categories = db.Categories.Select(e => e); ViewBag.SelectedCategories = selectedCategories.ToList(); ViewBag.enKeywords = db.Keywords.Where(e => e.Type == true); ViewBag.huKeywords = db.Keywords.Where(e => e.Type == false); return View(entry); } TempData["Attachments"] = null; TempData["FeaturedImage"] = null; return RedirectToAction("Index"); } ViewBag.Categories = db.Categories.Select(e => e); ViewBag.SelectedCategories = selectedCategories.ToList(); ViewBag.enKeywords = db.Keywords.Where(e => e.Type == true); ViewBag.huKeywords = db.Keywords.Where(e => e.Type == false); return View(entry); }
private void KeywordsProcedure(Entry entry, string huKeywords, string enKeywords) { entry.Keywords.Clear(); string[] separators = { ", " }; var huKeywordsList = huKeywords.Split(separators, StringSplitOptions.RemoveEmptyEntries).ToList(); var enKeywordsList = enKeywords.Split(separators, StringSplitOptions.RemoveEmptyEntries).ToList(); foreach (var item in huKeywordsList) { var keyword = db.Keywords.SingleOrDefault(e => e.Value == item && !e.Type); if (keyword == null) { keyword = new Keyword { Type = false, Value = item }; db.Keywords.Add(keyword); } entry.Keywords.Add(keyword); } foreach (var item in enKeywordsList) { var keyword = db.Keywords.SingleOrDefault(e => e.Value == item && e.Type); if (keyword == null) { keyword = new Keyword { Type = true, Value = item }; db.Keywords.Add(keyword); } entry.Keywords.Add(keyword); } }
public ActionResult Edit(Entry entry, int[] selectedCategories, string huKeywords, string enKeywords, string toDeleteFiles) { var toEditEntry = db.Entries.Where(e => e.Id == entry.Id).First(); KeywordsProcedure(toEditEntry, huKeywords, enKeywords); var attachments = TempData["Attachments"] as List<string>; var featuredImage = TempData["FeaturedImage"] as List<string>; if (ModelState.IsValid) { toEditEntry.enContent = entry.enContent; toEditEntry.enIntroduction = entry.enIntroduction; toEditEntry.enTitle = entry.enTitle; toEditEntry.huContent = entry.huContent; toEditEntry.huIntroduction = entry.huIntroduction; toEditEntry.huTitle = entry.huTitle; toEditEntry.IsFeatured = entry.IsFeatured; toEditEntry.Published = entry.Published; toEditEntry.PublishedDate = entry.PublishedDate; toEditEntry.UserId = int.Parse((string)Session["UserId"]); toEditEntry.Categories.Clear(); foreach (var id in selectedCategories.ToList()) { Category category = db.Categories.Single(e => e.Id == id); toEditEntry.Categories.Add(category); } //Delete to delete List<string> fileNames = new List<string>(); foreach (var idString in toDeleteFiles.Split(',').Where(e => e != "")) { int id = int.Parse(idString); var file = db.Files.Where(e => e.Id == id).First(); fileNames.Add(file.Name); toEditEntry.Files.Remove(file); db.Files.Remove(file); } var ctrl = new UploadController(); ctrl.PathValue = "/Public/Files/"; ctrl.Remove(fileNames.ToArray()); //Add new ones if (attachments != null) { foreach (var att in attachments) { WebApplication.File newAtt = new WebApplication.File { Entry = toEditEntry, Location = Guid.NewGuid().ToString(), Name = Path.GetFileName(att) }; db.Files.Add(newAtt); } } if (featuredImage != null && featuredImage.Count > 0) { string[] fileName = { Path.GetFileName(entry.FeaturedImage) }; ctrl.PathValue = "/Public/Images/"; ctrl.Remove(fileName); toEditEntry.FeaturedImage = featuredImage[0]; } else if (string.IsNullOrEmpty(entry.FeaturedImage)) { toEditEntry.FeaturedImage = ""; } try { db.SaveChanges(); } catch (DbEntityValidationException e) { } TempData["Attachments"] = null; TempData["FeaturedImage"] = null; return RedirectToAction("Index"); } ViewBag.Categories = db.Categories.Select(e => e); ViewBag.SelectedCategories = selectedCategories.ToList(); ViewBag.enKeywords = db.Keywords.Where(e => e.Type == true); ViewBag.huKeywords = db.Keywords.Where(e => e.Type == false); return View(entry); }