public async Task <Response <Comments> > EditCommentById(EditCommentDTO dto, string id, User user)
        {
            Response <Comments> response = new Response <Comments>();
            var result = await GetCommentById(id);

            if (!result.Success)
            {
                response.Message = "Could not edit comment";
                response.Data    = result.Data;
            }

            result.Data.Comment    = dto.Comment;
            result.Data.Updated_at = DateTime.Now;
            result.Data.UserId     = user.Id;

            if (await _commentsRepository.Modify(result.Data))
            {
                response.Message = "Successfully edited comment";
                response.Success = true;
                return(response);
            }

            response.Message = "Something went wrong";
            return(response);
        }
Ejemplo n.º 2
0
        public static EditCommentDTO CommentEditViewModel_To_EditCommentDTO(CommentEditViewModel commentEditViewModel)
        {
            var            mapper         = new MapperConfiguration(cfg => cfg.CreateMap <CommentEditViewModel, EditCommentDTO>()).CreateMapper();
            EditCommentDTO editCommentDTO = mapper.Map <CommentEditViewModel, EditCommentDTO>(commentEditViewModel);

            return(editCommentDTO);
        }
Ejemplo n.º 3
0
        public void Should_Throw_If_Comment_To_Be_Edited_Does_Not_Exist()
        {
            // Setup
            var wall = new Wall {
                Id = 1, OrganizationId = 2
            };
            var post = new Post {
                Id = 1, Wall = wall, WallId = wall.Id
            };
            var comments = new List <Comment>
            {
                new Comment {
                    Id = 1, AuthorId = "user1", Post = post
                }
            };

            _commentsDbSet.SetDbSetData(comments.AsQueryable());

            var editCommentDto = new EditCommentDTO
            {
                Id             = 2,
                MessageBody    = "edited comment",
                UserId         = "user1",
                OrganizationId = 2
            };

            // Act
            // Assert
            var ex = Assert.Throws <ValidationException>(() => _commentService.EditComment(editCommentDto));

            Assert.AreEqual(ErrorCodes.ContentDoesNotExist, ex.ErrorCode);
        }
Ejemplo n.º 4
0
        public void EditComment(EditCommentDTO commentDto)
        {
            var comment = _commentsDbSet
                          .Include(x => x.Post.Wall)
                          .FirstOrDefault(c =>
                                          c.Id == commentDto.Id &&
                                          c.Post.Wall.OrganizationId == commentDto.OrganizationId);

            if (comment == null)
            {
                throw new ValidationException(ErrorCodes.ContentDoesNotExist, "Comment does not exist");
            }

            var isWallModerator = _wallModeratorsDbSet
                                  .Any(x => x.UserId == commentDto.UserId && x.WallId == comment.Post.WallId) || commentDto.UserId == comment.CreatedBy;

            var isAdministrator = _permissionService.UserHasPermission(commentDto, AdministrationPermissions.Post);

            if (!isAdministrator && !isWallModerator)
            {
                throw new UnauthorizedException();
            }

            comment.MessageBody = commentDto.MessageBody;
            comment.PictureId   = commentDto.PictureId;
            comment.LastEdit    = DateTime.UtcNow;
            _uow.SaveChanges(commentDto.UserId);
        }
Ejemplo n.º 5
0
        public string editComment(CommentEditViewModel commentEditViewModel)
        {
            commentEditViewModel.modified = DateTime.Now;
            EditCommentDTO editCommentDTO = MapperModule.CommentEditViewModel_To_EditCommentDTO(commentEditViewModel);

            commentService.EditComment(editCommentDTO);

            return(JsonConvert.SerializeObject(new { id = commentEditViewModel.id, content = commentEditViewModel.content, modified = commentEditViewModel.modified }));
        }
Ejemplo n.º 6
0
        public void EditComment(EditCommentDTO editCommentDTO)
        {
            Comment comment = Database.Comments.Get(editCommentDTO.id);

            comment.context  = editCommentDTO.content;
            comment.modified = editCommentDTO.modified;

            Database.Comments.Update(comment);
            Database.Save();
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> EditComment([FromRoute] int commentId, [FromBody] EditCommentDTO editCommentDto)
        {
            var userId = (await GetCurrentUserAsync()).Id;
            var result = await _commentService.EditComment(userId, commentId, editCommentDto);

            if (result == null)
            {
                return(BadRequest());
            }

            return(Ok(result));
        }
        public async Task <IActionResult> EditComment([FromRoute] string commentId, [FromBody] EditCommentDTO model)
        {
            var loggedInUser = await _userManager.GetUserAsync(User);

            var comment = await _commentsService.EditCommentById(model, commentId, loggedInUser);

            if (!comment.Success)
            {
                return(BadRequest(comment));
            }
            return(NoContent());
        }
Ejemplo n.º 9
0
        public async Task <CommentDTO> EditComment(string userId, int commentId, EditCommentDTO editCommentDto)
        {
            var comment = _mapper.Map <CommentDTO>(await _commentRepository.GetComment(commentId));

            if (comment.User.Id != userId)
            {
                return(null);
            }

            var editedComment = _mapper.Map <CommentDTO>(await _commentRepository.UpdateComment(commentId, editCommentDto));

            return(editedComment);
        }
Ejemplo n.º 10
0
        public async Task <Comment> UpdateComment(int commentId, EditCommentDTO commentDto)
        {
            var comment = await GetComment(commentId);

            if (comment != null)
            {
                comment.Text = commentDto.Text;
            }

            await _context.SaveChangesAsync();

            return(comment);
        }
Ejemplo n.º 11
0
        public void Should_Throw_If_User_Edits_Other_User_Comment()
        {
            // Setup
            var wall = new Wall {
                Id = 1, OrganizationId = 2
            };
            var post = new Post {
                Id = 1, Wall = wall, WallId = wall.Id
            };
            var comments = new List <Comment>
            {
                new Comment {
                    Id = 1, AuthorId = "user1", Post = post
                }
            };

            _commentsDbSet.SetDbSetData(comments.AsQueryable());

            var wallModerators = new List <WallModerator>
            {
                new WallModerator {
                    WallId = wall.Id, UserId = "user2"
                }
            };

            _wallModeratorsDbSet.SetDbSetData(wallModerators.AsQueryable());

            var editCommentDto = new EditCommentDTO
            {
                Id             = 1,
                MessageBody    = "edited comment",
                UserId         = "user3",
                OrganizationId = 2
            };

            _permissionService.UserHasPermission(editCommentDto, AdministrationPermissions.Post).Returns(false);

            // Act
            // Assert
            Assert.Throws <UnauthorizedException>(() => _commentService.EditComment(editCommentDto));
        }
        public async Task EditCommentTest()
        {
            //arrange
            string idFromRoute = "081450e9-40d3-484f-b4e9-4b8136f45ebd";

            var commentDto = new EditCommentDTO {
                Comment = "hey"
            };

            //act
            EditCommentByIdMockUp(true);
            var editCommentApiResult = await _feedController.EditComment(idFromRoute, commentDto) as NoContentResult;

            EditCommentByIdMockUp(false);
            var editCommentApiResult1 = await _feedController.EditComment("", commentDto) as BadRequestObjectResult;

            //assert
            Assert.NotNull(editCommentApiResult);
            Assert.IsInstanceOf <NoContentResult>(editCommentApiResult);
            Assert.IsInstanceOf <BadRequestObjectResult>(editCommentApiResult1);
        }