public async Task <IActionResult> Post(string conversationId, [FromBody] AddMessageDto dto)
        {
            using (logger.BeginScope("This log is for {conversationId}", conversationId))
            {
                var stopWatch = Stopwatch.StartNew();
                try
                {
                    var message = await store.AddMessage(conversationId, new Message(dto));
                    await CreateMessagePayloadAndSend(conversationId, message);

                    logger.LogInformation(Events.MessageAdded, "Message Added Successfully", DateTime.UtcNow);
                    var messageDto = new GetMessageDto(message);
                    return(Ok(messageDto));
                }
                catch (StorageUnavailableException e)
                {
                    logger.LogError(Events.StorageError, e, $"Storage was not available to add message",
                                    DateTime.UtcNow);
                    return(StatusCode(503, "Failed to reach Storage"));
                }
                catch (ConversationNotFoundException e)
                {
                    logger.LogError(Events.ConversationNotFound, e,
                                    $"Conversation of Id = {conversationId} was not found", DateTime.UtcNow);
                    return(NotFound($"Conversation of Id = {conversationId} was not found"));
                }
                catch (InvalidDataException e)
                {
                    logger.LogError(Events.UsernameNotFound, e, $"Username is not in participants", DateTime.UtcNow);
                    return(StatusCode(400, "Sender Username doesn't exist in conversation"));
                }
                catch (Exception e)
                {
                    logger.LogError(Events.InternalError, e, $"Failed to send messages for {conversationId}",
                                    DateTime.UtcNow);
                    return(StatusCode(500, $"Failed to send message"));
                }
                finally
                {
                    PostMessageMetric.TrackValue((double)stopWatch.ElapsedMilliseconds);
                }
            }
        }
 public async Task <Message> AddMessage(string conversationId, Message message)
 {
     return(await resiliencyPolicy.ExecuteAsync(() => conversationStore.AddMessage(conversationId, message)));
 }
Beispiel #3
0
 public Task <Message> AddMessage(string conversationId, Message message)
 {
     return(AddMessageMetric.TrackTime(() => store.AddMessage(conversationId, message)));
 }