public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } var existingArticle = await _Repo.GetArticleById(Article.Id); Article.ViewCount = existingArticle.ViewCount; Article.Version = existingArticle.Version + 1; //check if the slug already exists in the database. var slug = UrlHelpers.URLFriendly(Article.Topic); if (String.IsNullOrWhiteSpace(slug)) { ModelState.AddModelError("Article.Topic", "The Topic must contain at least one alphanumeric character."); return(Page()); } if (!await _Repo.IsTopicAvailable(slug, Article.Id)) { ModelState.AddModelError("Article.Topic", "This Title already exists."); return(Page()); } var articlesToCreateFromLinks = (await ArticleHelpers.GetArticlesToCreate(_Repo, Article, createSlug: true)) .ToList(); Article.Published = _clock.GetCurrentInstant(); Article.Slug = slug; Article.AuthorId = Guid.Parse(this.User.FindFirstValue(ClaimTypes.NameIdentifier)); Article.AuthorName = User.Identity.Name; if (!string.Equals(Article.Slug, existingArticle.Slug, StringComparison.InvariantCulture)) { await _SlugRepo.AddToHistory(existingArticle.Slug, Article); } //AddNewArticleVersion(); try { await _Repo.Update(Article); } catch (ArticleNotFoundException) { return(new ArticleNotFoundResult()); } if (articlesToCreateFromLinks.Count > 0) { return(RedirectToPage("CreateArticleFromLink", new { id = slug })); } return(Redirect($"/{(Article.Slug == "home-page" ? "" : Article.Slug)}")); }
public async Task <Article> Update(int id, string topic, string content, Guid authorId, string authorName) { var article = new Article { Topic = topic }; if (string.IsNullOrWhiteSpace(article.Slug)) { throw new InvalidTopicException("The topic must contain at least one alphanumeric character."); } var existingArticle = await _repository.GetArticleBySlug(article.Slug); if (existingArticle != null && existingArticle.Id != id) { throw new InvalidTopicException("The topic conflicts with an existing article."); } existingArticle = await _repository.GetArticleById(id); if (!Changed(existingArticle.Topic, topic) && !Changed(existingArticle.Content, content)) { throw new NoContentChangedException(); } var oldSlug = existingArticle.Slug; existingArticle.Topic = topic; existingArticle.Content = content; existingArticle.AuthorId = authorId; existingArticle.AuthorName = authorName; existingArticle.Version++; existingArticle.Published = _clock.GetCurrentInstant(); await _repository.Update(existingArticle); if (Changed(oldSlug, existingArticle.Slug)) { await _slugHistoryRepository.AddToHistory(oldSlug, existingArticle); } await _mediator.Publish(new ArticleEditedNotification(existingArticle)); return(existingArticle); }