public async Task <IActionResult> Save(Article article) { if (!ModelState.IsValid) { return(View("Edit", new Article())); } var existing = _repo.GetPost(article.Id); if (existing == null) { _repo.AddPost(article); // BackgroundJob.Enqueue(() => emailService.SendEmailAsync(_config["user:email"], "You successfully added a post to your blog", "Article: " + article.Excerpt, _config)); } else { existing.Title = article.Title.Trim(); existing.Slug = !string.IsNullOrWhiteSpace(article.Slug) ? article.Slug.Trim() : Article.CreateSlug(article.Title); existing.Content = article.Content.Trim(); existing.Excerpt = article.Excerpt.Trim(); _repo.UpdatePost(existing); } await _repo.SaveChangesAsync(); return(Redirect(article.GetLink())); }
public Response AddPost(Post post) { var userNameRegex = new Regex(REGEX_USER_NAME); if (post.UserName.Length == 0 || post.UserName.Length > MAX_SIZE_USER_NAME || !userNameRegex.IsMatch(post.UserName)) { return new Response { IsSuccess = false, UncorrectParam = "UserName" } } ; var textRegex = new Regex(REGEX_TEXT); if (post.Text.Length == 0 || post.Text.Length > MAX_SIZE_TEXT || textRegex.IsMatch(post.Text)) { return new Response { IsSuccess = false, UncorrectParam = "Text" } } ; var homepageRegex = new Regex(REGEX_HOMEPAGE); if (post.Homepage.Length > MAX_SIZE_HOMEPAGE || !homepageRegex.IsMatch(post.Homepage) && post.Homepage.Length != 0) { return new Response { IsSuccess = false, UncorrectParam = "Homepage" } } ; var emailRegex = new Regex(REGEX_EMAIL); if (!emailRegex.IsMatch(post.Email) || post.Email.Length > MAX_SIZE_EMAIL) { return new Response { IsSuccess = false, UncorrectParam = "Email" } } ; post.Homepage = WebUtility.UrlEncode(post.Homepage); var ip = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString(); var browser = _httpContextAccessor.HttpContext.Request.Headers["User-Agent"].ToString(); post.Date = DateTime.Now; _dbService.AddPost(post, ip, browser); return(new Response { IsSuccess = true }); }