public UserPopUpViewModel() { isreadonly = true; closeCommand = new UserPopUpCloseCommand(); SearchCommandProperty = new searchCommand(); editMessageCommand = new EditMessageCommand(); userFriendsList = new ObservableCollection <FriendsModel>(); }
public async Task <ApiResponse> EditMessage([FromBody] EditMessageCommand command) { var userId = User.FindFirst(ClaimTypes.NameIdentifier).Value; command.ModifiedById = userId; command.ModifiedDate = DateTime.UtcNow; return(await _mediator.Send(command)); }
/// <inheritdoc /> public async Task UpdateAsync(EditMessageCommand command, CancellationToken cancellationToken) { ChatMessageData message = _chatMessages.FirstOrDefault(m => m.Id == command.MessageId); if (message == null) { return; } message.Content = command.Content; }
public async Task <ActionResult> EditMessage([FromRoute] int messageId, [FromBody] EditMessageBody body, CancellationToken cancellationToken = default) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // Check if the message exists MessageExistsQuery existsQuery = new MessageExistsQuery { MessageId = messageId }; bool exists = await _mediator.Send(existsQuery, cancellationToken); if (!exists) { return(NotFound(new ErrorResource { StatusCode = StatusCodes.Status404NotFound, Message = $"Message with ID '{messageId}' does not exist" })); } // Check if the user is the author of the message and thus allowed to update it IsAuthorOfMessageQuery isAuthorQuery = new IsAuthorOfMessageQuery { MessageId = messageId }; bool isAuthor = await _mediator.Send(isAuthorQuery, cancellationToken); if (!isAuthor) { return(StatusCode(StatusCodes.Status403Forbidden, new ErrorResource { StatusCode = StatusCodes.Status403Forbidden, Message = "Only the author of a message is allowed to update a message" })); } // Update the message EditMessageCommand editCommand = new EditMessageCommand { MessageId = messageId, HtmlContent = body.HtmlContent }; await _mediator.Send(editCommand, cancellationToken); return(NoContent()); }
public async Task <IActionResult> Put([FromBody] EditMessageCommand command, CancellationToken cancellationToken) { ChatMessageData message = await _messagesData.GetByIdAsync(command.MessageId, cancellationToken); if (message == null) { return(this.NotFound(new ProblemDetails() { Detail = $"Message with id {command.MessageId} not found." })); } await _messagesData.UpdateAsync(command, cancellationToken); return(this.Ok()); }
public async Task EditMessageCommandHandler_ShouldUpdateMessagesContent() { // Arrange EditMessageCommand request = new EditMessageCommand { MessageId = 1, HtmlContent = "<p>hello world</p>" }; IQueryable <Message> databaseMessage = new[] { new Message { MessageId = 1, HtmlContent = "<p>original</p>", IsEdited = false } } .AsQueryable() .BuildMock() .Object; _unitOfWorkMock .Setup(m => m.Messages.GetById(request.MessageId)) .Returns(databaseMessage); Message passedMessage = null; _unitOfWorkMock .Setup(m => m.Messages.Update(It.IsAny <Message>())) .Callback <Message>(m => passedMessage = m); _unitOfWorkMock .Setup(m => m.CommitAsync(It.IsAny <CancellationToken>())) .ReturnsAsync(1); EditMessageCommand.Handler handler = new EditMessageCommand.Handler(_unitOfWorkMock.Object); // Act await handler.Handle(request); // Assert _unitOfWorkMock.Verify(m => m.Messages.Update(It.IsAny <Message>()), Times.Once); _unitOfWorkMock.Verify(m => m.CommitAsync(It.IsAny <CancellationToken>()), Times.AtLeastOnce); Assert.NotNull(passedMessage); Assert.Equal(request.HtmlContent, passedMessage.HtmlContent); Assert.True(passedMessage.IsEdited); }