public MessagesController ( IMessageLinkBuilder linkBuilder, ActionExecutor actionExecutor, ListMessagesQuery listMessagesQuery, GetMessageQuery getMessageQuery, CreateMessageCommand createMessageCommand, UpdateMessageCommand updateMessageCommand, DeleteMessageCommand deleteMessageCommand ) { _linkBuilder = linkBuilder ?? throw new ArgumentNullException(nameof(linkBuilder)); _actionExecutor = actionExecutor ?? throw new ArgumentNullException(nameof(actionExecutor)); _listMessagesQuery = listMessagesQuery ?? throw new ArgumentNullException(nameof(listMessagesQuery)); _getMessageQuery = getMessageQuery ?? throw new ArgumentNullException(nameof(getMessageQuery)); _deleteMessageCommand = deleteMessageCommand ?? throw new ArgumentNullException(nameof(deleteMessageCommand)); _createMessageCommand = createMessageCommand ?? throw new ArgumentNullException(nameof(createMessageCommand)); _updateMessageCommand = updateMessageCommand ?? throw new ArgumentNullException(nameof(updateMessageCommand)); }
public async Task Should_succeed_with_status_updated_when_update_is_valid() { // Arrange var message = MockedMessageBuilder .SetId(1) .SetMessage("The message") .SetClientId(Guid.NewGuid().ToString()) .Build() .Object; _mockedRepository.Setup(x => x.GetAsync(It.IsAny <int>())).ReturnsAsync(message); _mockedRepository.Setup(x => x.UnitOfWork).Returns(_mockedUnitOfWork.Object); _mockedUnitOfWork.Setup(x => x.SaveChangesAsync(It.IsAny <CancellationToken>())).ReturnsAsync(1); var command = new UpdateMessageCommand(message.Id, "Successful update", message.ClientId); var handler = new UpdateMessageCommandHandler(_mockedRepository.Object); // Act var result = await handler.Handle(command, default); // Assert Assert.True(result.Succeeded); Assert.IsType <Updated>(result.StatusCode); Assert.Equal(command.MessageId, message.Id); Assert.Equal(command.Message, message.Message); _mockedRepository.Verify(x => x.GetAsync(command.MessageId), Times.Once); _mockedRepository.Verify(x => x.Update(message), Times.Once); _mockedUnitOfWork.Verify(x => x.SaveChangesAsync(default), Times.Once);
public ActionResult UpdateMessage([FromRoute] int messageId, [FromBody] UpdateMessageCommand model) { var message = _context.Messages.Find(messageId); if (message == null) { return(NotFound("Message with this ID does not exist")); } message.Text = model.Text; _context.SaveChanges(); return(Ok()); }
public async Task Should_fail_with_status_bad_request_when_message_is_null_or_empty(string message) { // Arrange var command = new UpdateMessageCommand(1, message, Guid.NewGuid().ToString()); var handler = new UpdateMessageCommandHandler(_mockedRepository.Object); // Act var result = await handler.Handle(command, default); // Assert Assert.True(result.Failed); Assert.IsType <BadRequest>(result.StatusCode); }
private async Task OnMessageUpdated(DiscordClient client, MessageUpdateEventArgs eventArgs) { try { var message = eventArgs.Message; var channel = eventArgs.Channel; var guild = eventArgs.Guild; if (message == null) { throw new Exception($"{nameof(eventArgs.Message)} is null"); } // issues caused by Threads feature if (message.Author == null) { return; } if (!IsReleventMessage(message)) { return; } if (IsPrivateMessageChannel(channel)) { return; } var todoMessage = new MessageCommandPayload(message, guild); var command = new UpdateMessageCommand(todoMessage); _emoteCommandQueue.Enqueue(command); } catch (Exception ex) { _logger.LogError(ex, nameof(OnMessageUpdated)); var eventContextError = new EventErrorContext() { EventName = nameof(OnMessageUpdated), User = eventArgs.Author, Channel = eventArgs.Channel, Guild = eventArgs.Guild, Message = eventArgs.Message }; await _discordErrorLogger.LogDiscordError(eventContextError, ex.ToString()); } }
public async Task Should_fail_with_status_not_found_when_updating_message_that_does_not_exist() { // Arrange _mockedRepository.Setup(x => x.GetAsync(It.IsAny <int>())).ReturnsAsync((BoardMessage)null); var command = new UpdateMessageCommand(1, "The message", Guid.NewGuid().ToString()); var handler = new UpdateMessageCommandHandler(_mockedRepository.Object); // Act var result = await handler.Handle(command, default); // Assert Assert.True(result.Failed); Assert.IsType <NotFound>(result.StatusCode); _mockedRepository.Verify(x => x.GetAsync(command.MessageId), Times.Once); }
public async Task Should_fail_with_status_bad_request_when_attempting_to_update_message_created_by_another_client() { // Arrange var message = MockedMessageBuilder .SetId(1) .SetMessage("The message") .SetClientId(Guid.NewGuid().ToString()) .Build() .Object; _mockedRepository.Setup(x => x.GetAsync(It.IsAny <int>())).ReturnsAsync(message); var command = new UpdateMessageCommand(message.Id, "Attempt to update another client's message", Guid.NewGuid().ToString()); var handler = new UpdateMessageCommandHandler(_mockedRepository.Object); // Act var result = await handler.Handle(command, default); // Assert Assert.True(result.Failed); Assert.IsType <BadRequest>(result.StatusCode); _mockedRepository.Verify(x => x.GetAsync(command.MessageId), Times.Once); }
public void MySetup() => _command = new UpdateMessageCommand(Clock, Logger, State);