public IActionResult PublishArticle(int id) { Logger.LogMessage($"PublishToggle: {id}"); using (var db = new FusekiContext()) { var article = db.Articles.First(el => el.Id == id); if (article == null) { return(new JsonResult(new { Message = "No article" })); } if (article.Published) { article.Published = false; db.SaveChanges(); } else { if (article.Deleted) { return(new JsonResult(new { Message = "Deleted alread" })); } article.Published = true; db.SaveChanges(); } return(new JsonResult(new { Message = "Success", Status = article.Published })); } }
public IActionResult UpdateArticle(ArticleModel model) { if (ModelState.IsValid) { using (var db = new FusekiContext()) { var article = db.Articles .Include(aa => aa.Tags) .First(el => el.Id == model.Id); //allow extended chars in title. But links and finding will strip them. article.Title = model.Title; var normalized = Renderer.Normalize(model.Body); article.Body = normalized; var now = DateTime.Now; article.Updated = now; if (article.Created == DateTime.MinValue) { article.Created = now; } //rectify tags. var empty = new List <string>(); var tags = model.Tags?.Split(",").Select(el => el.Trim()) ?? new string[0]; var newTagNames = tags .Select(el => el.Trim()) .Where(ee => !string.IsNullOrWhiteSpace(ee)) .ToHashSet(); var existingTags = article.Tags.Select(el => el.Name).ToHashSet(); var todelete = existingTags.Except(newTagNames); var toadd = newTagNames.Except(existingTags); foreach (var bad in todelete) { var tag = db.Tags.Where(el => el.ArticleId == article.Id && el.Name == bad).First(); db.Remove(tag); } foreach (var good in toadd) { var tag = CreateTag(article, good); db.Add(tag); } db.SaveChanges(); return(Redirect($"../{model.Title}")); } } else { return(View()); } }
public IActionResult SetTags(int id, string tagLine = "") { if (string.IsNullOrEmpty(tagLine)) { tagLine = ""; } var newTags = Helpers.TagLine2Tags(tagLine); var didSomething = false; var result = ""; using (var db = new FusekiContext()) { var image = db.Images .Include(el => el.ImageTags) .FirstOrDefault(el => el.Id == id); var exiTags = image.ImageTags ?? new List <ImageTag>(); //exiTags = new List<ImageTag>(); var toDelete = new List <ImageTag>(); foreach (var tag in newTags) { if (exiTags.FirstOrDefault(el => el.Name == tag) == null) { var newImageTag = new ImageTag(); newImageTag.Image = image; newImageTag.Name = tag; db.Add(newImageTag); didSomething = true; result += $"\ncreated new tag for image: {tag}"; } } foreach (var exiTag in exiTags) { if (newTags.FirstOrDefault(el => el == exiTag.Name) == null) { db.Remove(exiTag); didSomething = true; result += $"\nremoved old tag on image: {exiTag.Name}"; } } if (didSomething) { db.SaveChanges(); } } var arm = new ActionResultModel(); arm.NextLink = "/image/viewall"; arm.NextLinkDescription = "back to image list"; arm.SetResult(result); return(View("ActionResult", arm)); }
public IActionResult CreateArticle() { using (var db = new FusekiContext()) { var article = new Article(); var rnd = new System.Random(); article.Title = $"draft{rnd.Next(1000)}"; article.Body = ""; var a = db.Add(article); db.SaveChanges(); return(Redirect($"article/edit/{a.Entity.Title}")); } }
public IActionResult Delete(int id) { using (var db = new FusekiContext()) { var image = db.Images .Include(el => el.ImageTags) .FirstOrDefault(el => el.Id == id); if (image != null) { image.Deleted = true; } db.SaveChanges(); } return(RedirectToAction("ViewImages")); }
public IActionResult Publish() { var model = new PublicationResultModel(); var publishResult = Publisher.Publish(); var publication = new Publication(); publication.ArticleCount = publishResult.Count; publication.PublicationTime = DateTime.Now; using (var db = new FusekiContext()) { db.Add(publication); db.SaveChanges(); } model.Publication = publication; ViewData["Title"] = $"PublicationResult"; return(View("PublicationResult", model)); }
public IActionResult RestoreImages() { var images = System.IO.Directory.GetFiles(PublishConfiguration.ImageSource).Select(el => el.Replace(PublishConfiguration.ImageSource, "").Replace("\\", "")); using (var db = new FusekiContext()) { foreach (var imagefn in images) { var exi = db.Images.FirstOrDefault(el => el.Filename == imagefn); if (exi == null) { var image = new Image(); image.Filename = imagefn; db.Add(image); } } db.SaveChanges(); } return(null); }
public IActionResult UploadPost(UploadFileModel file, string tagLine) { //possibly accept the override filename var filename = file.Image.FileName; if (string.IsNullOrEmpty(file.Filename)) { file.Filename = file.Filename.ToLower(); } else { filename = file.Filename; } filename = CleanFilename(filename); if (!ValidateImageExtension(filename)) { var extFromImage = Path.GetExtension(file.Image.FileName); if (ValidateImageExtension(extFromImage)) { filename = filename + extFromImage; } else { throw new Exception("Invalid extension on filename" + filename); } } var targetPath = $"{PublishConfiguration.ImageSource}/{filename}"; if (System.IO.File.Exists(targetPath)) { throw new System.Exception("Already exists."); } using (var stream = System.IO.File.Create(targetPath)) { file.Image.CopyTo(stream); } using (var db = new FusekiContext()) { var image = new Image(); image.Filename = filename; db.Add(image); var tags = Helpers.TagLine2Tags(tagLine); foreach (var tag in tags) { var t = new ImageTag(); t.Image = image; t.Name = tag; db.Add(t); } db.SaveChanges(); } var arm = new ActionResultModel(); arm.NextLink = "/image/upload"; arm.NextLinkDescription = "Return to image upload"; arm.SetResult("Uploaded image"); return(View("ActionResult", arm)); }
public static void Process(string fp, bool published) { var lines = System.IO.File.ReadLines(fp); if (!lines.Any()) { return; } if (lines.Count() == 1) { return; } var title = lines.Skip(1)?.Take(1).First().Trim(); if (string.IsNullOrEmpty(title)) { return; } using (var db = new FusekiContext()) { var exi = db.Articles.FirstOrDefault(el => el.Title == title); if (exi != null) { return; } var tagline = lines.Last(); while (string.IsNullOrEmpty(tagline)) { lines = lines.Take(lines.ToList().Count - 1); tagline = lines.Last(); } if (!tagline.StartsWith("tags:")) { tagline = "tags:"; } var tags = Helpers.TagLine2Tags(tagline.Split("tags:")[1]); lines = lines.Skip(4); lines = lines.Take(lines.ToList().Count - 1); var now = DateTime.Now; var article = new Article(); article.Title = title; article.Body = string.Join("\n", lines); article.Created = now; article.Updated = now; article.Published = published; db.Add(article); db.SaveChanges(); var articleEntity = db.Articles.First(el => el.Title == title); foreach (var tagname in tags) { var tag = new Tag(); tag.Article = articleEntity; tag.Name = tagname; tag.Updated = now; tag.Created = now; db.Add(tag); } ; db.SaveChanges(); } }