public async Task <ConversationDto> AddConversation(CreateConversationDto createConversationDto) { try { HttpResponseMessage response = await httpClient.PostAsync("api/conversations", new StringContent(JsonConvert.SerializeObject(createConversationDto), Encoding.UTF8, "application/json")); if (!response.IsSuccessStatusCode) { throw new ChatServiceException("Failed to retrieve user profile", response.ReasonPhrase, response.StatusCode); } string content = await response.Content.ReadAsStringAsync(); return(JsonConvert.DeserializeObject <ConversationDto>(content)); } catch (JsonException e) { throw new ChatServiceException("Failed to deserialize the response", e, "Serialization Exception", HttpStatusCode.InternalServerError); } catch (Exception e) { throw new ChatServiceException("Failed to reach chat service", e, "Internal Server Error", HttpStatusCode.InternalServerError); } }
public async Task AddTwoMessagesWithTheSameMessageId() { string participant1 = RandomString(); string participant2 = RandomString(); var createConversationDto = new CreateConversationDto { Participants = new[] { participant1, participant2 } }; await Task.WhenAll( chatServiceClient.CreateProfile(new CreateProfileDto { Username = participant1, FirstName = "Participant", LastName = "1" }), chatServiceClient.CreateProfile(new CreateProfileDto { Username = participant2, FirstName = "Participant", LastName = "2" }) ); var conversationDto = await chatServiceClient.AddConversation(createConversationDto); var messageId = RandomString(); var message1 = new SendMessageDtoV2("Hello", participant1, messageId); var message2 = new SendMessageDtoV2("Hello", participant1, messageId); await chatServiceClient.SendMessage(conversationDto.Id, message1); await chatServiceClient.SendMessage(conversationDto.Id, message2); ListMessagesDto listMessagesDto = await chatServiceClient.ListMessages(conversationDto.Id); Assert.AreEqual(1, listMessagesDto.Messages.Count); }
public async Task <IActionResult> CreateConversation([FromBody] CreateConversationDto conversationDto) { try { return(await createConversationControllerTimeMetric.TrackTime(async() => { string id = GenerateConversationId(conversationDto); var currentTime = DateTime.UtcNow; Conversation conversation = new Conversation(id, conversationDto.Participants, currentTime); await conversationsStore.AddConversation(conversation); logger.LogInformation(Events.ConversationCreated, "Conversation with id {conversationId} was created"); var newConversationPayload = new NotificationPayload(currentTime, "ConversationAdded", id, conversationDto.Participants); await notificationService.SendNotificationAsync(newConversationPayload); return Ok(conversation); })); } catch (StorageErrorException e) { logger.LogError(Events.StorageError, e, "Could not reach storage to add user conversation"); return(StatusCode(503, "Could not reach storage to add user conversation")); } catch (Exception e) { logger.LogError(Events.InternalError, e, "Failed to add conversation"); return(StatusCode(500, "Failed to add conversation")); } }
public async Task <ConversationDto> Post([FromBody] CreateConversationDto createConversationDto) { await Task.Delay(1); var processor = new StatementProcessor(); var sentence = processor.ProcessEvent((Events)createConversationDto.BusinessEventId); var conversation = new Conversation(createConversationDto.Name, createConversationDto.Language); return(conversation.GetConversationDto(sentence)); }
public async Task <Conversation> Create(CreateConversationDto input) { var model = new Conversation { SenderId = input.SenderId, ReceiverId = input.ReceiverId }; await _conversionRepository.AddAsync(model); return(model); }
public async Task MessagesPaging() { string participant1 = RandomString(); string participant2 = RandomString(); var createConversationDto = new CreateConversationDto { Participants = new[] { participant1, participant2 } }; await Task.WhenAll( chatServiceClient.CreateProfile(new CreateProfileDto { Username = participant1, FirstName = "Participant", LastName = "1" }), chatServiceClient.CreateProfile(new CreateProfileDto { Username = participant2, FirstName = "Participant", LastName = "2" }) ); var conversationDto = await chatServiceClient.AddConversation(createConversationDto); var messages = new[] { new SendMessageDto("Hola what's up?", participant1), new SendMessageDto("Not much you?", participant2), new SendMessageDto("Writing some code!", participant1), new SendMessageDto("Cool! Are you taking EECE503E?", participant2) }; foreach (SendMessageDto message in messages) { await chatServiceClient.SendMessage(conversationDto.Id, message); } ListMessagesDto messagesPayload = await chatServiceClient.ListMessages(conversationDto.Id, 3); Assert.AreEqual(3, messagesPayload.Messages.Count); Assert.AreEqual(messages[3].Text, messagesPayload.Messages[0].Text); Assert.AreEqual(messages[2].Text, messagesPayload.Messages[1].Text); Assert.AreEqual(messages[1].Text, messagesPayload.Messages[2].Text); messagesPayload = await chatServiceClient.ListMessagesByUri(messagesPayload.PreviousUri); Assert.AreEqual(1, messagesPayload.Messages.Count); Assert.AreEqual(messages[0].Text, messagesPayload.Messages[0].Text); messagesPayload = await chatServiceClient.ListMessagesByUri(messagesPayload.PreviousUri); Assert.AreEqual(0, messagesPayload.Messages.Count); Assert.AreEqual(messagesPayload.PreviousUri, ""); Assert.AreEqual(messagesPayload.PreviousUri, ""); }
public async Task <IActionResult> Create(CreateConversationDto input) { var token = GetToken(); var userId = LoginHelper.GetClaim(token, "UserId"); if (input.SenderId != Guid.Parse(userId)) { return(Unauthorized()); } var result = await _conversationAppService.Create(input); return(Ok(result)); }
public async Task CreateConversationReturns500WhenUnknownExceptionIsThrown() { conversationsStoreMock.Setup(store => store.AddConversation(It.IsAny <Conversation>())) .ThrowsAsync(new UnknownException()); CreateConversationDto newConversation = new CreateConversationDto() { Participants = new[] { Guid.NewGuid().ToString(), Guid.NewGuid().ToString() } }; var conversationsController = new ConversationsController(conversationsStoreMock.Object, profileStoreMock.Object, loggerMock.Object, metricsMock.Object, notificationServiceMock.Object); IActionResult result = await conversationsController.CreateConversation( newConversation); TestUtils.AssertStatusCode(HttpStatusCode.InternalServerError, result); }
public async Task CreateConversationReturns503WhenStorageIsUnavailable() { conversationsStoreMock.Setup(store => store.AddConversation(It.IsAny <Conversation>())) .ThrowsAsync(new StorageErrorException("Test Failure")); CreateConversationDto newConversation = new CreateConversationDto() { Participants = new[] { Guid.NewGuid().ToString(), Guid.NewGuid().ToString() } }; var conversationsController = new ConversationsController(conversationsStoreMock.Object, profileStoreMock.Object, loggerMock.Object, metricsMock.Object, notificationServiceMock.Object); IActionResult result = await conversationsController.CreateConversation( newConversation); TestUtils.AssertStatusCode(HttpStatusCode.ServiceUnavailable, result); }
public async Task <IActionResult> CreateNewMessage([FromBody] CreateConversationDto dto) { var loggedInUserId = User.FindFirst(ClaimTypes.NameIdentifier).Value; var recipientId = await _context.UserClaims .Where(x => x.ClaimType == "email" && x.ClaimValue == dto.Recipient) .Select(x => x.UserId) .SingleOrDefaultAsync(); var conversationToDb = new Conversation { AuthorId = loggedInUserId, Content = dto.Content, RecipientId = recipientId }; await _context.Conversations.AddAsync(conversationToDb); await _context.SaveChangesAsync(); return(Ok(conversationToDb)); }
public async Task <IActionResult> CreateConversation([FromBody] CreateConversationDto conversationDto) { try { string id = GenerateConversationId(conversationDto); Conversation conversation = new Conversation(id, conversationDto.Participants, DateTime.UtcNow); await conversationsStore.AddConversation(conversation); logger.LogInformation(Events.ConversationCreated, "Conversation with id {conversationId} was created"); return(Ok(conversation)); } catch (StorageErrorException e) { logger.LogError(Events.StorageError, e, "Could not reach storage to add user conversation"); return(StatusCode(503)); } catch (Exception e) { logger.LogError(Events.InternalError, e, "Failed to add conversation"); return(StatusCode(500)); } }
public async Task CreateListConversations() { string participant1 = RandomString(); string participant2 = RandomString(); var createConversationDto = new CreateConversationDto { Participants = new[] { participant1, participant2 } }; await Task.WhenAll( chatServiceClient.CreateProfile(new CreateProfileDto { Username = participant1, FirstName = "Participant", LastName = "1" }), chatServiceClient.CreateProfile(new CreateProfileDto { Username = participant2, FirstName = "Participant", LastName = "2" }) ); await chatServiceClient.AddConversation(createConversationDto); ListConversationsDto participant1ConversationsDto = await chatServiceClient.ListConversations(participant1); Assert.AreEqual(1, participant1ConversationsDto.Conversations.Count); ListConversationsDto participant2ConversationsDto = await chatServiceClient.ListConversations(participant2); Assert.AreEqual(1, participant2ConversationsDto.Conversations.Count); ListConversationsItemDto participant1ConversationItemDto = participant1ConversationsDto.Conversations.First(); ListConversationsItemDto participant2ConversationItemDto = participant2ConversationsDto.Conversations.First(); Assert.AreEqual(participant1ConversationItemDto.Id, participant2ConversationItemDto.Id); Assert.AreEqual(participant1ConversationItemDto.LastModifiedDateUtc, participant2ConversationItemDto.LastModifiedDateUtc); Assert.AreEqual(participant1, participant2ConversationItemDto.Recipient.Username); Assert.AreEqual(participant2, participant1ConversationItemDto.Recipient.Username); }
public async Task AddListMessagesV2() { string participant1 = RandomString(); string participant2 = RandomString(); var createConversationDto = new CreateConversationDto { Participants = new[] { participant1, participant2 } }; await Task.WhenAll( chatServiceClient.CreateProfile(new CreateProfileDto { Username = participant1, FirstName = "Participant", LastName = "1" }), chatServiceClient.CreateProfile(new CreateProfileDto { Username = participant2, FirstName = "Participant", LastName = "2" }) ); var conversationDto = await chatServiceClient.AddConversation(createConversationDto); var message1 = new SendMessageDtoV2("Hello", participant1, RandomString()); var message2 = new SendMessageDtoV2("What's up?", participant1, RandomString()); var message3 = new SendMessageDtoV2("Not much!", participant2, RandomString()); await chatServiceClient.SendMessage(conversationDto.Id, message1); await chatServiceClient.SendMessage(conversationDto.Id, message2); await chatServiceClient.SendMessage(conversationDto.Id, message3); ListMessagesDto listMessagesDto = await chatServiceClient.ListMessages(conversationDto.Id); Assert.AreEqual(3, listMessagesDto.Messages.Count); Assert.AreEqual(message3.Text, listMessagesDto.Messages[0].Text); Assert.AreEqual(message2.Text, listMessagesDto.Messages[1].Text); Assert.AreEqual(message1.Text, listMessagesDto.Messages[2].Text); }
private static string GenerateConversationId(CreateConversationDto conversationDto) { return(string.Join("_", conversationDto.Participants.OrderBy(key => key))); }