public ActionResult Search() { ViewBag.searchTerm = Request["q"] ?? ""; string searchTerm = ViewBag.searchTerm; List <ArticleVersionViewModel> avVMs = new List <ArticleVersionViewModel>(); var articles = from article in db.Articles join version in db.Versions on article.CurrentVersionId equals version.Id where version.Title.ToUpper().Contains(searchTerm.ToUpper()) select new { art = article, ver = version }; var articlesList = articles.ToList(); foreach (var article_version in articlesList) { ArticleVersionViewModel av = new ArticleVersionViewModel(); av.ArticleId = article_version.art.ArticleId; av.VersionId = article_version.art.CurrentVersionId; av.Title = article_version.ver.Title; av.Content = System.IO.File.ReadAllText(article_version.ver.ContentPath, System.Text.Encoding.UTF8); av.Domains = article_version.art.Domains; av.DatePublished = article_version.art.DatePublished; avVMs.Add(av); } return(View(avVMs)); }
// GET: Articles/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Article article = db.Articles.Find(id); if (article == null) { return(HttpNotFound()); } Version v = db.Versions.Find(article.CurrentVersionId); ArticleVersionViewModel av = new ArticleVersionViewModel(); av.Title = v.Title; av.ArticleId = article.ArticleId; av.VersionId = article.CurrentVersionId; av.Comments = article.Comments; av.DatePublished = article.DatePublished; av.LastEdit = v.LastEdit; av.Domains = article.Domains; if (System.IO.File.Exists(v.ContentPath)) { string content = System.IO.File.ReadAllText(v.ContentPath, System.Text.Encoding.UTF8); ViewBag.content = Markdown.ToHtml(content, this.pipeline); return(View(av)); } return(View(av)); }
public ActionResult SetVersion(int id, int reference) { Article a = db.Articles.Find(id); Version v = db.Versions.Find(reference); if (a == null || v == null) { return(HttpNotFound()); } a.CurrentVersionId = v.Id; if (TryValidateModel(a)) { db.SaveChanges(); return(RedirectToAction("Details", new { id = a.ArticleId })); } ArticleVersionViewModel av = new ArticleVersionViewModel(); av.Title = v.Title; av.ArticleId = a.ArticleId; av.VersionId = v.Id; ViewBag.currentVersionId = a.CurrentVersionId; av.AllVersions = GetVersions(a.ArticleId); if (System.IO.File.Exists(v.ContentPath)) { string content = System.IO.File.ReadAllText(v.ContentPath, System.Text.Encoding.UTF8); ViewBag.content = Markdown.ToHtml(content, this.pipeline); return(View(av)); } return(View(av)); }
// GET: Articles/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Article article = db.Articles.Find(id); if (article == null) { return(HttpNotFound()); } Version v = db.Versions.Find(article.CurrentVersionId); string content = ""; if (System.IO.File.Exists(v.ContentPath)) { content = System.IO.File.ReadAllText(v.ContentPath, System.Text.Encoding.UTF8); } ArticleVersionViewModel avViewModel = new ArticleVersionViewModel(); avViewModel.Title = v.Title; avViewModel.Domains = article.Domains; avViewModel.Content = content; avViewModel.AllDomains = GetDomains(); ViewBag.articleId = id; return(View(avViewModel)); }
// GET: Articles/Create public ActionResult Create() { ArticleVersionViewModel avVM = new ArticleVersionViewModel(); avVM.AllDomains = GetDomains(); if (!avVM.AllDomains.Any()) { TempData["errorMessage"] = "Create at least one domain first."; return(RedirectToAction("Create", "Domains")); } return(View(avVM)); }
// GET: Articles/Delete/5 public ActionResult Delete(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Article article = db.Articles.Find(id); Version v = db.Versions.Find(article.CurrentVersionId); ArticleVersionViewModel av = new ArticleVersionViewModel(); av.Title = v.Title; av.ArticleId = article.ArticleId; av.VersionId = article.CurrentVersionId; if (article == null) { return(HttpNotFound()); } return(View(av)); }
// GET: Articles public ActionResult Index() { var articles = from article in db.Articles join version in db.Versions on article.CurrentVersionId equals version.Id select new { art = article, ver = version }; List <ArticleVersionViewModel> avs = new List <ArticleVersionViewModel>(); var articlesList = articles.ToList(); foreach (var article_version in articlesList) { ArticleVersionViewModel av = new ArticleVersionViewModel(); Version v = db.Versions.Find(article_version.art.CurrentVersionId); av.ArticleId = article_version.art.ArticleId; av.VersionId = article_version.art.CurrentVersionId; av.Title = article_version.ver.Title; av.Content = System.IO.File.ReadAllText(article_version.ver.ContentPath, System.Text.Encoding.UTF8); av.Domains = article_version.art.Domains; av.DatePublished = article_version.art.DatePublished; avs.Add(av); } return(View(avs)); }
// GET: Domains/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } string orderby = ""; string asc = ""; Domain domain = db.Domains .Include("Articles") .FirstOrDefault(d => d.Id == id); if (domain == null) { return(HttpNotFound()); } DomainArticleViewModel da = new DomainArticleViewModel(); da.DomainId = domain.Id; da.DomainName = domain.Title; da.DomainDescription = domain.Description; da.articles = new List <ArticleVersionViewModel>(); da.LastArticlePublishDate = DateTime.MinValue; if (domain.Articles != null) { List <Article> articleList = new List <Article>(); if (Request["orderby"] != null && Request["orderby"] != "") { orderby = Request["orderby"]; asc = (Request["asc"] != null && Request["asc"] != "") ? Request["asc"] : "asc"; if (orderby == "title") { if (asc == "asc") { articleList = domain.Articles?.OrderBy(a => GetVersionTitle(a.CurrentVersionId)).ToList(); ViewBag.asc = "asc"; } else { articleList = domain.Articles?.OrderByDescending(a => GetVersionTitle(a.CurrentVersionId)).ToList(); ViewBag.asc = "desc"; } ViewBag.orderby = "title"; } else { if (asc == "asc") { articleList = domain.Articles?.OrderBy(a => a.DatePublished).ToList(); ViewBag.asc = "asc"; } else { articleList = domain.Articles?.OrderByDescending(a => a.DatePublished).ToList(); ViewBag.asc = "desc"; } ViewBag.orderby = "date"; } } else { articleList = domain.Articles?.OrderByDescending(a => a.DatePublished).ToList(); ViewBag.asc = "desc"; ViewBag.orderby = "date"; } foreach (var article in articleList) { ArticleVersionViewModel av = new ArticleVersionViewModel(); av.ArticleId = article.ArticleId; av.VersionId = article.CurrentVersionId; av.Domains = article.Domains; Models.Version v = db.Versions.Find(article.CurrentVersionId); av.Title = v.Title; if (article.DatePublished > da.LastArticlePublishDate) { da.LastArticlePublishDate = article.DatePublished; } av.DatePublished = article.DatePublished; av.LastEdit = v.LastEdit; da.articles.Add(av); } } return(View(da)); }
public ActionResult Edit(int id, ArticleVersionViewModel avViewModel) { avViewModel.AllDomains = GetDomains(); bool shouldUpdate = false; bool addedDomain = false; Article article = db.Articles.Find(id); if (article == null) { return(HttpNotFound()); } Version v = db.Versions.Find(article.CurrentVersionId); // domains var ds = db.Domains.Where(d => avViewModel.DomainIds.Contains(d.Id)).ToList(); if (avViewModel.DomainIds != null && avViewModel.DomainIds.Any()) { if (ds != null && ds.Any()) { if (article.Domains != null) { //List<Domain> l = ds.Where(p => !article.Domains.Contains(p)); List <Domain> l = ds.Where(p => !article.Domains.Contains(p)).ToList(); foreach (var dom in l) { article.Domains.Add(dom); addedDomain = true; } } else { article.Domains = ds.ToList(); } } else { ViewBag.Message = "Couldn't find selected domains."; return(View(avViewModel)); } } if (v.Title != avViewModel.Title) { shouldUpdate = true; } string newContents = ""; if (avViewModel.ContentFile != null && avViewModel.ContentFile.ContentLength > 0) { newContents = ReadStream(avViewModel.ContentFile.InputStream, avViewModel.ContentFile.ContentLength); } else if (avViewModel.Content != null && avViewModel.Content.Length > 0) { newContents = avViewModel.Content; } if (System.IO.File.Exists(v.ContentPath) && newContents != System.IO.File.ReadAllText(v.ContentPath, System.Text.Encoding.UTF8)) { shouldUpdate = true; } if (shouldUpdate) { Version newV = new Version(); newV.Title = avViewModel.Title; newV.LastEdit = DateTime.Now; if (avViewModel.ContentFile != null && avViewModel.ContentFile.ContentLength > 0) { try { string content = ReadStream(avViewModel.ContentFile.InputStream, avViewModel.ContentFile.ContentLength); newV.ContentPath = WriteFile(content); } catch (Exception e) { ViewBag.Message = "Error saving the file."; return(View(avViewModel)); } } else if (avViewModel.Content != null && avViewModel.Content.Length > 0) { try { newV.ContentPath = WriteFile(avViewModel.Content); } catch (Exception e) { ViewBag.Message = "Error saving the file."; return(View(avViewModel)); } } else { ModelState.AddModelError("OneOfTwoFieldsShouldBeFilled", "Either you upload a file or type your article in the box. You can't have an empty article."); return(View(avViewModel)); } if (TryValidateModel(newV)) { db.Versions.Attach(newV); newV.Article = article; db.Versions.Add(newV); db.SaveChanges(); article.CurrentVersionId = newV.Id; db.SaveChanges(); return(RedirectToAction("Index")); } System.IO.File.Delete(v.ContentPath); return(View(avViewModel)); } if (addedDomain) { if (TryValidateModel(article)) { db.SaveChanges(); return(RedirectToAction("Index")); } } return(View(avViewModel)); }
public ActionResult Create(ArticleVersionViewModel avViewModel) { avViewModel.AllDomains = GetDomains(); Article a = new Article(); Version v = new Version(); // domains var ds = db.Domains.Where(d => avViewModel.DomainIds.Contains(d.Id)); if (avViewModel.DomainIds != null && avViewModel.DomainIds.Any()) { if (ds != null && ds.Any()) { a.Domains = ds.ToList(); } else { ViewBag.Message = "Couldn't find selected domains."; return(View(avViewModel)); } } // write file contents if (avViewModel.ContentFile != null && avViewModel.ContentFile.ContentLength > 0) { string content = ReadStream(avViewModel.ContentFile.InputStream, avViewModel.ContentFile.ContentLength); try { v.ContentPath = WriteFile(content); } catch (Exception e) { ViewBag.Message = "Error saving the file."; return(View(avViewModel)); } } else if (avViewModel.Content != null && avViewModel.Content.Length > 0) { try { v.ContentPath = WriteFile(avViewModel.Content); } catch (Exception e) { ViewBag.Message = "Error saving the file."; return(View(avViewModel)); } } else { ModelState.AddModelError("OneOfTwoFieldsShouldBeFilled", "Either you upload a file or type your article in the box. You can't have an empty article."); return(View(avViewModel)); } v.Title = avViewModel.Title; a.ProtectFromEditing = false; v.LastEdit = DateTime.Now; a.DatePublished = DateTime.Now; if (TryValidateModel(a)) { db.Articles.Add(a); db.SaveChanges(); v.Article = a; db.Versions.Add(v); db.SaveChanges(); a.CurrentVersionId = v.Id; db.Entry(a).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } // if we're here, something failed System.IO.File.Delete(v.ContentPath); // cleanup unused files return(View(avViewModel)); }