public async Task <IActionResult> CreateBlog(NewPostDto postDto) { //var post= _mapper.Map<Post>(postDto); Guid postId = await BlogService.CreatePost(postDto.Title, postDto.Content, postDto.Username); return(Ok(postId)); }
public async Task NotifyAsync(NewPostDto postModel, NewlyCreatedPostDto createdPost, UserAndOrganizationHubDto userHubDto) { await _postNotificationService.NotifyAboutNewPostAsync(createdPost); var membersToNotify = await _wallService.GetWallMembersIdsAsync(postModel.WallId, postModel); await NotificationHub.SendWallNotificationAsync(postModel.WallId, membersToNotify, createdPost.WallType, userHubDto); }
public async Task NewPost(NewPostDto dto) { var memberInfo = await _memberRepository.FirstOrDefaultAsync(t => t.Id == dto.MemberId); if (memberInfo == null) { throw new UserFriendlyException("请先登录账户!"); } var plateInfo = await _plateRepository.FirstOrDefaultAsync(t => t.Id == dto.PlateId); if (plateInfo == null) { throw new UserFriendlyException("请选择发帖版块!"); } if (dto.Title.Length < 5) { throw new UserFriendlyException("标题不能少于5个字"); } if (dto.Content.Length < 5) { throw new UserFriendlyException("帖子内容不能少于5个字"); } ForumPost forumPost = new ForumPost(); forumPost.MemberId = dto.MemberId; forumPost.PlateId = dto.PlateId; forumPost.Title = dto.Title; forumPost.Content = dto.Content; if (dto.Content.Length <= 100) { forumPost.Summary = dto.Content; } else { forumPost.Summary = dto.Content.Substring(0, 100); } forumPost.LastCommentTime = DateTime.Now; var postId = await _forumPostRepository.InsertAndGetIdAsync(forumPost); if (dto.Images != null && dto.Images.Count > 0) { foreach (var image in dto.Images) { ForumImage forumImage = new ForumImage(); forumImage.Type = ForumImageType.Posts; forumImage.Image = image; forumImage.OwnerId = postId; await _forumImageRepository.InsertAsync(forumImage); } } }
public async Task <NewlyCreatedPostDto> CreateNewPostAsync(NewPostDto newPostDto) { await _postDeleteLock.WaitAsync(); try { var wall = await _wallsDbSet.FirstOrDefaultAsync(x => x.Id == newPostDto.WallId && x.OrganizationId == newPostDto.OrganizationId); if (wall == null) { throw new ValidationException(ErrorCodes.ContentDoesNotExist, "Wall not found"); } var post = new Post { AuthorId = newPostDto.UserId, Created = DateTime.UtcNow, LastEdit = DateTime.UtcNow, CreatedBy = newPostDto.UserId, MessageBody = newPostDto.MessageBody, PictureId = newPostDto.PictureId, SharedEventId = newPostDto.SharedEventId, LastActivity = DateTime.UtcNow, WallId = newPostDto.WallId, Likes = new LikesCollection() }; _postsDbSet.Add(post); await _uow.SaveChangesAsync(newPostDto.UserId); _postWatchers.Add(new PostWatcher { PostId = post.Id, UserId = newPostDto.UserId }); await _uow.SaveChangesAsync(newPostDto.UserId); var postCreator = await _usersDbSet.SingleAsync(user => user.Id == newPostDto.UserId); var postCreatorDto = MapUserToDto(postCreator); var newlyCreatedPostDto = MapNewlyCreatedPostToDto(post, postCreatorDto, wall.Type, newPostDto.MentionedUserIds); return(newlyCreatedPostDto); } finally { _postDeleteLock.Release(); } }
public async Task <IActionResult> AddPost([FromBody] NewPostDto newPostDto) { var post = _mapper.Map <Posts>(newPostDto); post.CreatedDate = DateTime.UtcNow; await _iPostsRepository.Create(post); if (await _iPostsRepository.SaveAll()) { return(Ok(_mapper.Map <NewPostDto>(post))); } throw new Exception("Error adding the category"); }
public void Should_Create_New_Wall_Post() { // Setup var walls = new List <Wall> { new Wall { Id = 1, OrganizationId = 2 } }; _wallsDbSet.SetDbSetDataForAsync(walls.AsQueryable()); // ReSharper disable once CollectionNeverUpdated.Local var posts = new List <Post>(); _postsDbSet.SetDbSetDataForAsync(posts.AsQueryable()); var users = new List <ApplicationUser> { new ApplicationUser { Id = _userId } }; _usersDbSet.SetDbSetDataForAsync(users.AsQueryable()); var newPostDto = new NewPostDto { MessageBody = "test", OrganizationId = 2, PictureId = "pic", UserId = _userId, WallId = walls.First().Id }; // Act _postService.CreateNewPostAsync(newPostDto); // Assert _postsDbSet.Received().Add( Arg.Is <Post>(x => x.MessageBody == newPostDto.MessageBody && x.WallId == newPostDto.WallId && x.AuthorId == newPostDto.UserId)); }
public async Task <IActionResult> AddPost(int userId, NewPostDto newPostDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var user = await usersService.GetUser(userId); newPostDto.BlogId = user.Blog.Id; var post = mapper.Map <Post>(newPostDto); usersService.Add <Post>(post); if (await usersService.SaveAll()) { return(Ok()); } return(BadRequest("Failed to add post")); }
public void Should_Throw_If_There_Is_No_Wall_To_Add_Posts_To() { // Setup // ReSharper disable once CollectionNeverUpdated.Local var walls = new List <Wall>(); _wallsDbSet.SetDbSetDataForAsync(walls.AsQueryable()); // ReSharper disable once CollectionNeverUpdated.Local var posts = new List <Post>(); _postsDbSet.SetDbSetDataForAsync(posts.AsQueryable()); var users = new List <ApplicationUser> { new ApplicationUser { Id = "testUser" } }; _usersDbSet.SetDbSetDataForAsync(users.AsQueryable()); var newPostDto = new NewPostDto { MessageBody = "test", OrganizationId = 2, PictureId = "pic", UserId = "testUser", WallId = 1 }; // Act // Assert var ex = Assert.ThrowsAsync <ValidationException>(async() => await _postService.CreateNewPostAsync(newPostDto)); Assert.AreEqual(ErrorCodes.ContentDoesNotExist, ex.ErrorCode); }