예제 #1
0
        public void Execute(EditPostCommentDto request)
        {
            if (_context.Posts.Find(request.PostId) == null)
            {
                throw new EntityNotFoundException(request.PostId, typeof(Post));
            }

            var comment = _context.Comments.Where(c => !c.IsDeleted && c.Id == request.Id).FirstOrDefault();

            if (comment == null)
            {
                throw new EntityNotFoundException(request.Id, typeof(Comment));
            }

            if (comment.CreatedBy != _actor.Id && _actor.RoleType != RoleType.Administrator && _actor.RoleType != RoleType.Moderator)
            {
                throw new NotAllowedException(UseCase.getUseCase(this.Id), _actor, $"You can only edit comments created by you.");
            }

            _validator.ValidateAndThrow(request);

            _mapper.Map <EditPostCommentDto, Comment>(request, comment);

            _context.SaveChanges(_actor.Id);
        }
 public IActionResult Put(int id, int commentId, [FromBody] EditPostCommentDto request, [FromServices] IEditPostCommentCommand command)
 {
     request.PostId = id;
     request.Id     = commentId;
     _executor.ExecuteCommand(command, request);
     return(StatusCode(StatusCodes.Status204NoContent));
 }