public async Task <IActionResult> GetProfile(string username) { var stopWatch = Stopwatch.StartNew(); try { UserProfile profile = await profileStore.GetProfile(username); return(Ok(profile)); } catch (ProfileNotFoundException) { logger.LogInformation(Events.ProfileNotFound, "A profile was request for user {username} but was not found", username); return(NotFound()); } catch (StorageErrorException e) { logger.LogError(Events.StorageError, e, "Failed to retrieve profile of user {username}", username); return(StatusCode(503, "Failed to reach storage")); } catch (Exception e) { logger.LogError(e, "Error occured while retrieving a user profile"); return(StatusCode(500, "Failed to retrieve profile of user {username}")); } finally { GetProfileMetric.TrackValue(stopWatch.ElapsedMilliseconds); } }
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 <IActionResult> GetConversations(string username, string startCt, string endCt, int limit = 50) { var stopWatch = Stopwatch.StartNew(); try { var resultConversations = await store.GetConversations(username, startCt, endCt, limit); var converter = new Converter <Conversation, GetConversationsDto>( conversation => { var recipientUsername = GetConversationsDto.GetRecipient(username, conversation); var profile = profileStore.GetProfile(recipientUsername).Result; return(new GetConversationsDto(conversation.Id, profile, conversation.LastModifiedDateUtc)); }); logger.LogInformation(Events.ConversationsRequested, $"Conversations for {username} has been requested!", DateTime.UtcNow); var nextUri = NextConversationsUri(username, resultConversations.StartCt, limit); var previousUri = PreviousConversationsUri(username, resultConversations.EndCt, limit); var conversationsDto = new GetConversationsListDto(resultConversations.Conversations.ConvertAll(converter), nextUri, previousUri); return(Ok(conversationsDto)); } catch (StorageUnavailableException e) { logger.LogError(Events.StorageError, e, $"Storage was not available to obtain list of conversations for {username}", DateTime.UtcNow); return(StatusCode(503, "Failed to reach Storage")); } catch (Exception e) { logger.LogError(Events.InternalError, e, $"Failed to obtain list of conversations for {username}", DateTime.UtcNow); return(StatusCode(500, $"Failed to obtain list of conversations for {username}")); } finally { GetConversationMetric.TrackValue(stopWatch.ElapsedMilliseconds); } }
public async Task <IActionResult> AddConversation([FromBody] AddConversationDto conversationDto) { using (logger.BeginScope("This log is for {conversationId}", Conversation.GenerateId(conversationDto.Participants))) { var stopWatch = Stopwatch.StartNew(); try { var conversation = await store.AddConversation(new Conversation(conversationDto)); logger.LogInformation(Events.ConversationCreated, $"Conversation of Participants {conversationDto.Participants[0]} " + $"and {conversationDto.Participants[1]} was created", DateTime.UtcNow); await CreateConversationPayloadAndSend(conversation); var GetConversationDto = new GetConversationDto(conversation); return(Ok(GetConversationDto)); } catch (StorageUnavailableException e) { logger.LogError(Events.StorageError, e, "Storage was not available to add conversation of Participants " + $"{conversationDto.Participants[0]} and {conversationDto.Participants[1]}", DateTime.UtcNow); return(StatusCode(503, "Failed to reach Storage")); } catch (Exception e) { logger.LogError(Events.InternalError, e, $"Failed to add conversation of Participants {conversationDto.Participants[0]} " + $"and {conversationDto.Participants[1]}", DateTime.UtcNow); return(StatusCode(500, $"Failed to add conversation of Participants {conversationDto.Participants[0]} " + $"and {conversationDto.Participants[1]}")); } finally { PostConversationMetric.TrackValue(stopWatch.ElapsedMilliseconds); } } }
public async Task <IActionResult> CreateProfile([FromBody] CreateProfileDto request) { var stopWatch = Stopwatch.StartNew(); var profile = new UserProfile(request.Username, request.FirstName, request.LastName); try { await profileStore.AddProfile(profile); logger.LogInformation(Events.ProfileCreated, "A Profile has been added for user {username}", request.Username); } catch (StorageErrorException e) { logger.LogError(Events.StorageError, e, "Failed to create a profile for user {username}", request.Username); return(StatusCode(503, "Failed to reach storage")); } catch (DuplicateProfileException) { logger.LogInformation(Events.ProfileAlreadyExists, "The profile for user {username} cannot be created because it already exists", request.Username); return(StatusCode(409, "Profile already exists")); } catch (ArgumentException) { return(StatusCode(400, "Invalid or incomplete Request Body")); } catch (Exception e) { logger.LogError(Events.InternalError, e, "Failed to create a profile for user {username}", request.Username); return(StatusCode(500, "Failed to create profile")); } finally { PostProfileMetric.TrackValue(stopWatch.ElapsedMilliseconds); } return(Created(request.Username, profile)); }
public async Task <IActionResult> Get(string conversationId, string startCt, string endCt, int limit = 50) { using (logger.BeginScope("This log is for {conversationId}", conversationId)) { var stopWatch = Stopwatch.StartNew(); try { var resultMessages = await store.GetConversationMessages(conversationId, startCt, endCt, limit); var converter = new Converter <Message, GetMessageDto>(message => new GetMessageDto(message)); var nextUri = NextMessagesUri(conversationId, resultMessages.StartCt, limit); var previousUri = PreviousMessagesUri(conversationId, resultMessages.EndCt, limit); var messageDtos = new GetMessagesListDto(resultMessages.Messages.ConvertAll(converter), nextUri, previousUri); logger.LogInformation(Events.MessagesRequested, $"Conversation messages for {conversationId} has been requested!", DateTime.UtcNow); return(Ok(messageDtos)); } catch (StorageUnavailableException e) { logger.LogError(Events.StorageError, e, $"Storage was not available to obtain list of conversation messages for {conversationId}", DateTime.UtcNow); return(StatusCode(503, "Failed to reach Storage")); } catch (Exception e) { logger.LogError(Events.InternalError, e, $"Failed to obtain list of conversation messages for {conversationId}", DateTime.UtcNow); return(StatusCode(500, $"Failed to obtain list of conversation messages for {conversationId}")); } finally { GetMessagesMetric.TrackValue(stopWatch.ElapsedMilliseconds); } } }