public async Task <IActionResult> CreatePost(CreatePostIndexModel model) { var currentUser = await this.userManager.GetUserAsync(this.User); var isBlocked = this.blogService.IsBlocked(currentUser); if (isBlocked) { this.TempData["Error"] = ErrorMessages.YouAreBlock; return(this.RedirectToAction("Index", "Blog")); } var isInRole = await this.blogService.IsInBlogRole(currentUser); if (!isInRole) { this.TempData["Error"] = string.Format(ErrorMessages.NotInBlogRoles, Roles.Contributor); return(this.RedirectToAction("Index", "Blog")); } if (this.ModelState.IsValid) { var tuple = await this.blogService.CreatePost(model, currentUser); this.TempData[tuple.Item1] = tuple.Item2; return(this.RedirectToAction("Index", "Blog")); } else { this.TempData["Error"] = ErrorMessages.InvalidInputModel; } return(this.RedirectToAction("Index", "Blog", model)); }
public async Task <IActionResult> CreatePost() { var currentUser = await this.userManager.GetUserAsync(this.User); var isBlocked = this.blogService.IsBlocked(currentUser); if (isBlocked) { this.TempData["Error"] = ErrorMessages.YouAreBlock; return(this.RedirectToAction("Index", "Blog")); } var isInRole = await this.blogService.IsInBlogRole(currentUser); if (!isInRole) { this.TempData["Error"] = string.Format(ErrorMessages.NotInBlogRoles, Roles.Contributor); return(this.RedirectToAction("Index", "Blog")); } var model = new CreatePostIndexModel { Categories = await this.blogService.ExtractAllCategoryNames(), Tags = await this.blogService.ExtractAllTagNames(), PostInputModel = new CreatePostInputModel(), }; return(this.View(model)); }
public async Task <IActionResult> CreatePost() { var model = new CreatePostIndexModel { Categories = await this.blogService.ExtractAllCategoryNames(), Tags = await this.blogService.ExtractAllTagNames(), PostInputModel = new CreatePostInputModel(), }; return(this.View(model)); }
public async Task <IActionResult> CreatePost(CreatePostIndexModel model) { if (this.ModelState.IsValid) { var currentUser = await this.userManager.GetUserAsync(this.User); var tuple = await this.blogService.CreatePost(model, currentUser); this.TempData[tuple.Item1] = tuple.Item2; return(this.RedirectToAction("Index", "Blog")); } else { this.TempData["Error"] = ErrorMessages.InvalidInputModel; return(this.View(model)); } }
public async Task <Tuple <string, string> > CreatePost(CreatePostIndexModel model, ApplicationUser user) { var category = await this.db.Categories .FirstOrDefaultAsync(x => x.Name.ToUpper() == model.PostInputModel.CategoryName.ToUpper()); if (category == null) { return(Tuple.Create("Error", ErrorMessages.InvalidInputModel)); } var contentWithoutTags = Regex.Replace(model.PostInputModel.SanitizeContent, "<.*?>", string.Empty); var post = new Post { Title = model.PostInputModel.Title, CategoryId = category.Id, Content = model.PostInputModel.SanitizeContent, CreatedOn = DateTime.UtcNow, ShortContent = contentWithoutTags.Length <= GlobalConstants.BlogPostShortContentMaxLength ? contentWithoutTags : $"{contentWithoutTags.Substring(0, GlobalConstants.BlogPostShortContentMaxLength)}...", ApplicationUserId = user.Id, }; var imageUrl = await ApplicationCloudinary.UploadImage( this.cloudinary, model.PostInputModel.CoverImage, string.Format(GlobalConstants.CloudinaryPostCoverImageName, post.Id), GlobalConstants.PostBaseImageFolder); var canUploadPostImages = (await this.userManager.IsInRoleAsync(user, GlobalConstants.AdministratorRole)) || (await this.userManager.IsInRoleAsync(user, GlobalConstants.EditorRole)) || (await this.userManager.IsInRoleAsync(user, GlobalConstants.AuthorRole)); if (canUploadPostImages) { for (int i = 0; i < model.PostInputModel.PostImages.Count; i++) { var image = model.PostInputModel.PostImages.ElementAt(i); var postImage = new PostImage { PostId = post.Id, Name = string.Format(GlobalConstants.BlogPostImageNameTemplate, i + 1), }; var postImageUrl = await ApplicationCloudinary.UploadImage( this.cloudinary, image, string.Format(GlobalConstants.CloudinaryPostImageName, postImage.Id), GlobalConstants.PostBaseImagesFolder); postImage.Url = postImageUrl ?? string.Empty; post.PostImages.Add(postImage); } } if (imageUrl != null) { post.ImageUrl = imageUrl; } foreach (var tagName in model.PostInputModel.TagsNames) { var tag = await this.db.Tags .FirstOrDefaultAsync(x => x.Name.ToUpper() == tagName.ToUpper()); if (tag != null) { post.PostsTags.Add(new PostTag { PostId = post.Id, TagId = tag.Id, }); } } var adminRole = await this.roleManager.FindByNameAsync(Roles.Administrator.ToString()); var editorRole = await this.roleManager.FindByNameAsync(Roles.Editor.ToString()); var allAdminIds = this.db.UserRoles .Where(x => x.RoleId == adminRole.Id) .Select(x => x.UserId) .ToList(); var allEditorIds = this.db.UserRoles .Where(x => x.RoleId == editorRole.Id) .Select(x => x.UserId) .ToList(); var specialIds = allAdminIds.Union(allEditorIds).ToList(); if (await this.userManager.IsInRoleAsync(user, Roles.Administrator.ToString()) || await this.userManager.IsInRoleAsync(user, Roles.Editor.ToString()) || await this.userManager.IsInRoleAsync(user, Roles.Author.ToString())) { post.PostStatus = PostStatus.Approved; var followerIds = this.db.FollowUnfollows .Where(x => x.ApplicationUserId == user.Id && !specialIds.Contains(x.FollowerId)) .Select(x => x.FollowerId) .ToList(); specialIds = specialIds.Union(followerIds).ToList(); specialIds.Remove(user.Id); } else { post.PostStatus = PostStatus.Pending; this.db.PendingPosts.Add(new PendingPost { ApplicationUserId = post.ApplicationUserId, PostId = post.Id, IsPending = true, }); } foreach (var specialId in specialIds) { var toUser = await this.userManager.FindByIdAsync(specialId); string notificationId = await this.notificationService.AddBlogPostNotification(toUser, user, post.ShortContent, post.Id); var count = await this.notificationService.GetUserNotificationsCount(toUser.UserName); await this.notificationHubContext .Clients .User(toUser.Id) .SendAsync("ReceiveNotification", count, true); var notification = await this.notificationService.GetNotificationById(notificationId); await this.notificationHubContext.Clients.User(toUser.Id) .SendAsync("VisualizeNotification", notification); } this.db.Posts.Add(post); this.nonCyclicActivity.AddUserAction( new CreatePostUserAction { ApplicationUser = user, ApplicationUserId = user.Id, PostId = post.Id, Post = post, }); await this.db.SaveChangesAsync(); return(Tuple.Create("Success", SuccessMessages.SuccessfullyCreatedPost)); }
public async Task <Tuple <string, string> > CreatePost(CreatePostIndexModel model, ApplicationUser user) { var category = this.db.Categories.FirstOrDefault(x => x.Name == model.PostInputModel.CategoryName); var contentWithoutTags = Regex.Replace(model.PostInputModel.SanitizeContent, "<.*?>", string.Empty); var post = new Post { Title = model.PostInputModel.Title, CategoryId = category.Id, Content = model.PostInputModel.SanitizeContent, CreatedOn = DateTime.UtcNow, UpdatedOn = DateTime.UtcNow, ShortContent = contentWithoutTags.Length <= 347 ? contentWithoutTags : $"{contentWithoutTags.Substring(0, 347)}...", ApplicationUserId = user.Id, Likes = 0, }; var imageUrl = await ApplicationCloudinary.UploadImage( this.cloudinary, model.PostInputModel.CoverImage, string.Format( GlobalConstants.CloudinaryPostCoverImageName, post.Id)); if (imageUrl != null) { post.ImageUrl = imageUrl; } foreach (var tagName in model.PostInputModel.TagsNames) { var tag = this.db.Tags.FirstOrDefault(x => x.Name.ToLower() == tagName.ToLower()); post.PostsTags.Add(new PostTag { PostId = post.Id, TagId = tag.Id, }); } var adminRole = await this.db.Roles.FirstOrDefaultAsync(x => x.Name == Roles.Administrator.ToString()); var editorRole = await this.db.Roles.FirstOrDefaultAsync(x => x.Name == Roles.Editor.ToString()); var allAdminIds = this.db.UserRoles .Where(x => x.RoleId == adminRole.Id) .Select(x => x.UserId) .ToList(); var allEditorIds = this.db.UserRoles .Where(x => x.RoleId == editorRole.Id) .Select(x => x.UserId) .ToList(); var specialIds = allAdminIds.Union(allEditorIds).ToList(); if (await this.userManager.IsInRoleAsync(user, Roles.Administrator.ToString()) || await this.userManager.IsInRoleAsync(user, Roles.Editor.ToString()) || await this.userManager.IsInRoleAsync(user, Roles.Author.ToString())) { post.PostStatus = PostStatus.Approved; var followerIds = this.db.FollowUnfollows .Where(x => x.PersonId == user.Id && !specialIds.Contains(x.FollowerId)) .Select(x => x.FollowerId) .ToList(); specialIds = specialIds.Union(followerIds).ToList(); specialIds.Remove(user.Id); } else { post.PostStatus = PostStatus.Pending; this.db.PendingPosts.Add(new PendingPost { ApplicationUserId = post.ApplicationUserId, PostId = post.Id, IsPending = true, }); } foreach (var specialId in specialIds) { var toUser = await this.db.Users.FirstOrDefaultAsync(x => x.Id == specialId); string notificationId = await this.notificationService.AddBlogPostNotification(toUser, user, post.ShortContent, post.Id); var count = await this.notificationService.GetUserNotificationsCount(toUser.UserName); await this.notificationHubContext .Clients .User(toUser.Id) .SendAsync("ReceiveNotification", count, true); var notification = await this.notificationService.GetNotificationById(notificationId); await this.notificationHubContext.Clients.User(toUser.Id) .SendAsync("VisualizeNotification", notification); } this.db.Posts.Add(post); this.db.BlockedPosts.Add(new BlockedPost { ApplicationUserId = post.ApplicationUserId, PostId = post.Id, IsBlocked = false, }); this.nonCyclicActivity.AddUserAction(user, post, UserActionsType.CreatePost, user); await this.db.SaveChangesAsync(); return(Tuple.Create("Success", SuccessMessages.SuccessfullyCreatedPost)); }
public async Task <Tuple <string, string> > CreatePost(CreatePostIndexModel model, ApplicationUser user) { var category = this.db.Categories.FirstOrDefault(x => x.Name == model.PostInputModel.CategoryName); var contentWithoutTags = Regex.Replace(model.PostInputModel.SanitizeContent, "<.*?>", string.Empty); var post = new Post { Title = model.PostInputModel.Title, CategoryId = category.Id, Content = model.PostInputModel.SanitizeContent, CreatedOn = DateTime.UtcNow, UpdatedOn = DateTime.UtcNow, ShortContent = contentWithoutTags.Length <= 347 ? contentWithoutTags : $"{contentWithoutTags.Substring(0, 347)}...", ApplicationUserId = user.Id, Likes = 0, }; var imageUrl = await ApplicationCloudinary.UploadImage( this.cloudinary, model.PostInputModel.CoverImage, string.Format( GlobalConstants.CloudinaryPostCoverImageName, post.Id)); if (imageUrl != null) { post.ImageUrl = imageUrl; } foreach (var tagName in model.PostInputModel.TagsNames) { var tag = this.db.Tags.FirstOrDefault(x => x.Name.ToLower() == tagName.ToLower()); post.PostsTags.Add(new PostTag { PostId = post.Id, TagId = tag.Id, }); } if (await this.userManager.IsInRoleAsync(user, Roles.Administrator.ToString()) || await this.userManager.IsInRoleAsync(user, Roles.Editor.ToString()) || await this.userManager.IsInRoleAsync(user, Roles.Author.ToString())) { post.PostStatus = PostStatus.Approved; } else { post.PostStatus = PostStatus.Pending; this.db.PendingPosts.Add(new PendingPost { ApplicationUserId = post.ApplicationUserId, PostId = post.Id, IsPending = true, }); } this.db.Posts.Add(post); this.db.BlockedPosts.Add(new BlockedPost { ApplicationUserId = post.ApplicationUserId, PostId = post.Id, IsBlocked = false, }); this.nonCyclicActivity.AddUserAction(user, post, UserActionsType.CreatePost, user); await this.db.SaveChangesAsync(); return(Tuple.Create("Success", SuccessMessages.SuccessfullyCreatedPost)); }