Example #1
0
        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());
            }
        }
Example #2
0
        public IActionResult EditArticle(string title)
        {
            using (var db = new FusekiContext())
            {
                var article = db.Articles
                              .Include(el => el.Tags)
                              .First(el => el.Title.StartsWith(title));
                //startswith to allow titles with ? marks in them. Doh.

                var normalized = Renderer.Normalize(article.Body);
                article.Body = normalized;
                var liveUrl = string.Format(Settings.LiveUrlTemplate, article.MakeFilename(false), true);
                var editUrl = string.Format(Settings.EditUrlTemplate, article.Title, false);
                var model   = new ArticleModel(article, liveUrl, editUrl, Renderer.ToHtml(normalized));
                ViewData["Title"] = $"Editing {article.Title}";
                return(View("EditArticle", model));
            }
        }