public async Task <Post> UpdatePost(EditPostDTO newPost) { var post = new Post { IdPost = newPost.IdPost, Title = newPost.Title, Content = newPost.Content, Attachment = newPost.Attachment }; post = await _postRepository.UpdatePost(post); var tagsList = await _postRepository.GetAllPostTagByPostId(post.IdPost); List <string> nonExistingPostTag = new List <string>(); foreach (string tag in newPost.Tags) { var existingPostTag = tagsList.Find(posttag => posttag.TagNavigation.Name.Equals(tag)); if (existingPostTag == null) { nonExistingPostTag.Add(tag); } } await mappingTag(nonExistingPostTag, post.IdPost); return(post); }
public void EditPost(EditPostDTO editPostDto) { var post = _postsDbSet .Include(x => x.Wall) .FirstOrDefault(x => x.Id == editPostDto.Id && x.Wall.OrganizationId == editPostDto.OrganizationId); if (post == null) { throw new ValidationException(ErrorCodes.ContentDoesNotExist, "Post not found"); } var isWallModerator = _moderatorsDbSet .Any(x => x.UserId == editPostDto.UserId && x.WallId == post.WallId) || post.CreatedBy == editPostDto.UserId; var isAdministrator = _permissionService.UserHasPermission(editPostDto, AdministrationPermissions.Post); if (!isAdministrator && !isWallModerator) { throw new UnauthorizedException(); } post.MessageBody = editPostDto.MessageBody; post.PictureId = editPostDto.PictureId; post.LastEdit = DateTime.UtcNow; _uow.SaveChanges(editPostDto.UserId); }
public Post EditPost(EditPostDTO editPostDTO, Post post) { post.Content = editPostDTO.Content + " " + "(Edited)"; post.Id = editPostDTO.Id; _Context.SaveChanges(); return(post); }
public async Task <PostDTO> EditPost(EditPostDTO postDto) { var editPost = _context.Posts .Include(post => post.Author) .ThenInclude(author => author.Avatar) .Where(post => post.Id == postDto.Id) .FirstOrDefault(); editPost.Body = postDto.Body; if (editPost.Preview == null) { editPost.Preview = new Image(); editPost.Preview.URL = postDto.PreviewImage; } else { editPost.Preview.URL = postDto.PreviewImage; } await _context.SaveChangesAsync(); var editedPostDTO = _mapper.Map <PostDTO>(editPost); await _postHub.Clients.All.SendAsync("Edit", editedPostDTO); return(editedPostDTO); }
public IActionResult Edit(EditPostDTO editPostDTO) { try { var existingPost = _postStore.GetById(editPostDTO.Id); if (ModelState.IsValid) { _postStore.EditPost(editPostDTO, existingPost); return(RedirectToAction("AllPosts")); } return(View(editPostDTO)); } catch (Exception ex) { _logger.LogError(ex.Message); return(RedirectToAction(ActionName.ServerError, ControllerName.Accounts)); } }
public void Should_Throw_If_Post_To_Be_Edited_Does_Not_Exist() { // Setup var posts = new List <Post>(); _postsDbSet.SetDbSetData(posts.AsQueryable()); var editPostDto = new EditPostDTO { Id = 1, MessageBody = "edited post", UserId = "user3", OrganizationId = 2 }; // Act // Assert var ex = Assert.Throws <ValidationException>(() => _postService.EditPost(editPostDto)); Assert.AreEqual(ErrorCodes.ContentDoesNotExist, ex.ErrorCode); }
public void Should_Throw_If_User_Edits_Other_User_Post() { // Setup var wall = new Wall { Id = 1, OrganizationId = 2 }; var posts = new List <Post> { new Post { Id = 1, Wall = wall, MessageBody = "post", AuthorId = "user1" } }; _postsDbSet.SetDbSetData(posts.AsQueryable()); var wallModerators = new List <WallModerator> { new WallModerator { WallId = wall.Id, UserId = "user2" } }; _wallModeratorsDbSet.SetDbSetData(wallModerators.AsQueryable()); var editPostDto = new EditPostDTO { Id = 1, MessageBody = "edited post", UserId = "user3", OrganizationId = 2 }; _permissionService.UserHasPermission(editPostDto, AdministrationPermissions.Post).Returns(false); // Act // Assert Assert.Throws <UnauthorizedException>(() => _postService.EditPost(editPostDto)); }
public async Task <IActionResult> UpdatePost(EditPostDTO newPostDTO) { await _postService.UpdatePost(newPostDTO); return(StatusCode(200, newPostDTO)); }
public async Task Put(int id, [FromBody] EditPostDTO item) { var post = _mapper.Map <Post>(item); await _postService.UpdatePost(post, id); }
public async Task <ActionResult <PostDTO> > EditPost([FromBody] EditPostDTO dto) { return(Ok(await _postService.EditPost(dto))); }
public async Task <PostDTO> Update(int postId, EditPostDTO editPost) { if (editPost.EndDate <= editPost.StartDate) { throw new InvalidOperationException("EndDateTime should be greater than StartDateTime"); } var currentUser = _userContext.CurrentUser; if (currentUser.RoleId != (int)Roles.Moderator) { var defaultEndDateTime = _postDefaultValuesOptions.Value.DisplayPeriodInDaysForUsers; editPost.EndDate = editPost.StartDate.AddDays(defaultEndDateTime); } var oldEntity = await _db.Posts.GetAsync(postId); if (oldEntity == null) { throw new InvalidOperationException($"Post by id: {postId} not found."); } if (oldEntity.Priority == PostPriority.Normal && editPost.Priority != oldEntity.Priority && currentUser.RoleId != Role.ModeratorRoleId) { throw new InvalidOperationException("This user has no rights to set high priority for the post"); } oldEntity.Title = editPost.Title; oldEntity.Content = editPost.Content; oldEntity.DurationInSeconds = editPost.DurationInSeconds; oldEntity.VideoUrl = editPost.VideoUrl; oldEntity.ModifiedByUserId = _userContext.CurrentUser.Id; oldEntity.ModifiedDate = DateTime.Now; oldEntity.Priority = editPost.Priority; oldEntity.StartDateTime = editPost.StartDate; oldEntity.EndDateTime = editPost.EndDate; if (currentUser.RoleDTO.Name == "User" && oldEntity.IsPublic) { oldEntity.IsPublic = false; } var newFiles = FileMapper.ConvertToPostFileCollection(editPost.Files); var filesToRemove = (await _fileService.GetFilesForPost(oldEntity.Id)) .Where(oldfile => !newFiles.Exists(newFile => newFile.File.Id == oldfile.Id)).ToList(); foreach (var file in filesToRemove) { await _fileService.DeleteAsync(file.Id); } oldEntity.PostFiles = newFiles; var updatedPost = await _db.Posts.Update(oldEntity); await _db.SaveAsync(); var updatedPostDTO = PostMapper.ConvertToPostDTO(updatedPost); updatedPostDTO.UserDTO = UserMapper.ConvertToUserDTO(updatedPost.User); return(updatedPostDTO); }