public Task <bool> Handle(UpdateCommentCommand request, CancellationToken cancellationToken) { if (!request.IsValid()) { NotifyValidationErrors(request); return(Task.FromResult(false)); } var comment = new Comment(request.CommentId, request.CommentContent, request.Product, request.UserId); var existingComment = _commentRepository.GetById(comment.CommentId); if (existingComment != null && existingComment.CommentId == comment.CommentId) { if (!existingComment.Equals(comment)) { _bus.RaiseEvent(new DomainNotification(request.MessageType, "The comment has already been created.")); return(Task.FromResult(false)); } } _commentRepository.Update(comment); if (Commit()) { _bus.RaiseEvent(new CommentUpdatedEvent(comment.CommentId, comment.CommentContent, comment.Product, comment.UserId)); } return(Task.FromResult(true)); }
public IActionResult UpdateComment([FromBody] UpdateCommentCommand model, [FromRoute] int postID, [FromRoute] int commentID) { if (_context.Posts.Find(postID) == null) { return(BadRequest("Post with this id does not exist")); } if (model.Text == null) { return(BadRequest("Text property cannot be null")); } var comment = _context.Comments.Find(commentID); if (comment == null) { return(BadRequest("Comment with this commentId does not exist")); } try { comment.Text = model.Text; _context.SaveChanges(); return(Ok()); } catch (ApplicationException ex) { return(BadRequest(new { message = ex.Message })); } }
public IHttpActionResult PutComment(int id, [FromBody] UpdateCommentCommand command) { command.intIdComment = id; var resposta = _mediator.Send(command); return(Ok(resposta)); }
public async Task Handler_GivenValidData_ShouldUpdateComment() { // Arrange var comment = new CommentDTO { Id = 1, PostId = 99, Text = "Comment_new", AuthorId = 99, Date = new DateTime(2020, 05, 01), }; var command = new UpdateCommentCommand { Model = comment }; // Act var handler = new UpdateCommentCommand.UpdateCommentCommandHandler(Context); await handler.Handle(command, CancellationToken.None); var entity = Context.Comments.Find(comment.Id); // Assert entity.ShouldNotBeNull(); entity.Text.ShouldBe(command.Model.Text); entity.PostId.ShouldNotBe(command.Model.PostId); entity.AuthorId.ShouldNotBe(command.Model.AuthorId); entity.Date.ShouldNotBe(command.Model.Date); }
public async Task UpdateComment( [FromRoute] Guid commentId, [FromBody] UpdateCommentCommand command) { command.CommentId = commentId; await Mediator.Send(command); }
public async Task <ActionResult <CommentDetailVm> > Update(int id, [FromBody] UpdateCommentCommand command) { command.Id = id; var res = await Mediator.Send(command); return(Ok(await Mediator.Send(new GetCommentDetailQuery { Id = res }))); }
/// <summary> /// Update comment command handler async. /// </summary> /// <param name="commentCommand">Update comment command.</param> /// <param name="commentRepository">Comment repository.</param> public async Task HandleUpdateCommentAsync(UpdateCommentCommand commentCommand) { commentCommand.UpdateCommentInfo = new CommentDto { Text = commentCommand.Text }; await commentRepository.UpdateCommentAsync(commentCommand.CommentId, commentCommand.UpdateCommentInfo); }
public async Task <ActionResult> Update(int id, CommentRequest commentRequest) { var updateCommentCommand = new UpdateCommentCommand { Id = id, Content = commentRequest.Content }; await Mediator.Send(updateCommentCommand); return(NoContent()); }
public async Task <ActionResult> Update(int id, UpdateCommentCommand command) { if (id != command.Id && id != Author.Id) { return(BadRequest()); } await _mediator.Send(command); return(NoContent()); }
public async Task <IActionResult> UpdateCommentAsync([FromRoute] Guid id, [FromBody] UpdateCommentCommand request) { if (id != request.CommentId) { return(BadRequest()); } await Mediator.Send(request); return(NoContent()); }
public async Task <IActionResult> Approve(long id) { if (await this.authService.CheckIfBanned(this.User).ConfigureAwait(false)) { return(this.Forbid()); } var command = new UpdateCommentCommand() { Id = id, Approved = true }; return(this.Ok(await this.mediator.Send(command).ConfigureAwait(false))); }
public ResultDto UpdateComment(long postId, long commentId, CommentDto commentDto) { return(Result(() => { var command = new UpdateCommentCommand { PostId = postId, CommentId = commentId, Content = commentDto.Body, Status = commentDto.Status, }; CommandDispatcher.Send(command); })); }
public async Task UpdatesComment() { var request = new UpdateCommentCommand() { Id = this.comment.Id, Text = "My New Text", Rating = 3, Approved = true }; await this.sut.Handle(request, CancellationToken.None).ConfigureAwait(false); var dbComment = await this.Context.Comments.FindAsync(this.comment.Id).ConfigureAwait(false); Assert.Equal(request.Text, dbComment.Text); Assert.Equal(request.Rating, dbComment.Rating); Assert.Equal(request.Approved, dbComment.Approved); }
public async Task <IActionResult> CommentLiked(Guid?parentId, Guid?id, [FromBody] CommentViewModel comment) { var command = new UpdateCommentCommand { ParentId = parentId.GetValueOrDefault(), Id = id.GetValueOrDefault(), ParentVersion = (comment.ParentVersion?.Value).GetValueOrDefault(), CommentLikedChildCmd = new CommentLikedChildCmd() }; var result = await _mediator.Send(command); return(result.Success ? Ok(ToViewModel(result.Model)) : CreateBadRequestResult(result)); }
public ICommandResult Handle(UpdateCommentCommand command) { //Fail Fast Validation command.Validate(); if (command.Invalid) { return(new GenericCommandResult(false, Messages.Ex_ExceptionGeneric, command.Notifications)); } var comment = _repository.GetById(command.Id); comment.UpdateTitle(command.Title); _repository.Update(comment); return(new GenericCommandResult(true, Messages.Act_Update, comment)); }
/// <summary> /// Edit comment. /// </summary> /// <param name="id">Comment identifier.</param> /// <param name="text">Comment new text.</param> /// <returns>Post page with edited comment.</returns> public async Task <IActionResult> Edit(int id, string text) { if (id != default) { var model = new CommentDTO { Id = id, Text = text, }; var commentCommand = new UpdateCommentCommand { Model = model }; await _mediator.Send(commentCommand); } return(Ok()); }
public async Task GivenValidData_ReturnOkStatusCode() { var client = await _factory.GetAuthenticatedClientAsync("User"); var command = new UpdateCommentCommand { Text = "Comment text", UserId = 3 }; var content = ClientUtilities.GetRequestContent(command); var validId = 1; var response = await client.PutAsync($"/api/comments/{validId}", content); Assert.Equal(HttpStatusCode.OK, response.StatusCode); }
public async Task GivenInvalidCommentId_ReturnNotFoundStatusCode() { var client = await _factory.GetAuthenticatedClientAsync("Moderator"); var command = new UpdateCommentCommand { Text = "Comment text", UserId = 2 }; var content = ClientUtilities.GetRequestContent(command); var invalidId = 10; var response = await client.PutAsync($"/api/comments/{invalidId}", content); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); }
public async Task GivenAuthorizedUserIsNotCommentCreatorAndNotAdmin_ReturnUnauthorizedStatusCode() { var client = await _factory.GetAuthenticatedClientAsync("Moderator"); var command = new UpdateCommentCommand { Text = "Comment text", UserId = 4 }; var content = ClientUtilities.GetRequestContent(command); var validId = 1; var response = await client.PutAsync($"/api/comments/{validId}", content); Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); }
public async Task GivenUnauthorizedUser_ReturnUnauthorizedStatusCode() { var client = await _factory.GetAnonymousClient(); var command = new UpdateCommentCommand { Text = "Comment text", UserId = 2 }; var content = ClientUtilities.GetRequestContent(command); var validId = 1; var response = await client.PutAsync($"/api/comments/{validId}", content); Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); }
public async Task <ActionResult> Update(int id, [FromBody] UpdateCommentCommand command) { try { if (command.Id == 0) { command.Id = id; } await Mediator.Send(command); return(Ok()); } catch (UnauthorizedException ex) { return(Unauthorized(ex.Message)); } catch (NotFoundException ex) { return(NotFound(ex.Message)); } }
public async Task Handle_GivenInvalidCommentData_ThrowsException() { // Arrange var comment = new CommentDTO { Id = 99, PostId = 99, Text = "Comment_test", AuthorId = 99, Date = new DateTime(2020, 05, 01), }; var command = new UpdateCommentCommand { Model = comment }; // Act var handler = new UpdateCommentCommand.UpdateCommentCommandHandler(Context); // Assert await Should.ThrowAsync <NotFoundException>(() => handler.Handle(command, CancellationToken.None)); }
public async Task UpdateCommentAsync(long commentId, [FromBody] UpdateCommentCommand command) { command.CommentId = commentId; await pipelineService.HandleCommandAsync(command); }
public async Task <ActionResult <string> > UpdateComment([FromBody] UpdateCommentCommand command) { var result = await Mediator.Send(command); return(new JsonResult(result)); }
public async Task <IActionResult> UpdateComment([FromBody] UpdateCommentCommand command) { await Mediator.Send(command); return(Ok()); }
public GenericCommandResult Update([FromBody] UpdateCommentCommand command, [FromServices] CommentHandler handler) { return((GenericCommandResult)handler.Handle(command)); }