Ejemplo n.º 1
0
        public async Task AddProfile(UserProfile profile)
        {
            ValidateArgument(profile);

            var entity = new ProfileTableEntity
            {
                PartitionKey = profile.Username,
                RowKey       = profile.Username,
                FirstName    = profile.FirstName,
                LastName     = profile.LastName,
            };

            var insertOperation = TableOperation.Insert(entity);

            try
            {
                await table.ExecuteAsync(insertOperation);
            }
            catch (StorageException e)
            {
                if (e.RequestInformation.HttpStatusCode == 409) // conflict
                {
                    throw new DuplicateProfileException($"Profile for user {profile.Username} already exists");
                }
                throw new StorageErrorException("Could not write to Azure Table", e);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Executes operation asynchronously.
        /// </summary>
        /// <param name="entity">Entity.</param>
        /// <param name="operation">Operation.</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <returns>Result entity.</returns>
        public Task <T> ExecuteAsync(T entity, Func <ITableEntity, TableOperation> operation, CancellationToken cancellationToken)
        {
            ITableEntity tableEntity = _entityConverter.GetEntity(entity);

            return(_cloudTable
                   .ExecuteAsync(operation(tableEntity), cancellationToken)
                   .Then(result =>
            {
                var value = (DynamicTableEntity)result.Result;
                return _entityConverter.GetEntity(value);
            }, cancellationToken));
        }
Ejemplo n.º 3
0
 public async Task AddOrUpdateProfile(string key, Profile profile)
 {
     var entity = new AzureTableEntity
     {
         PartitionKey = key,
         RowKey       = key,
         Id           = profile.Id,
         Enrollments  = profile.Enrollments
     };
     var createOperation = TableOperation.InsertOrReplace(entity);
     await Table.ExecuteAsync(createOperation);
 }
        public async Task AddMessage(string conversationId, string messageId, Message message)
        {
            if (string.IsNullOrWhiteSpace(message.SenderUsername))
            {
                throw new ArgumentNullException(nameof(message.SenderUsername));
            }

            MessageEntity entity = new MessageEntity
            {
                PartitionKey   = conversationId,
                RowKey         = DateTimeUtils.FromDateTimeToInvertedString(message.UtcTime),
                SenderUsername = message.SenderUsername,
                Text           = message.Text,
                MessageId      = messageId
            };

            var insertOperation = TableOperation.Insert(entity);

            try
            {
                await table.ExecuteAsync(insertOperation);
            }
            catch (StorageException e)
            {
                if (e.RequestInformation.HttpStatusCode == 409) // conflict
                {
                    // TODO: the caller should retry with a different timestamp
                    // The exception should be more explicit to allow the caller to distinguish between
                    // storage is down and a conflict
                    throw new StorageErrorException("Race conditions between messages", e);
                }
                throw new StorageErrorException("Could not write to Azure Table", e);
            }
        }
Ejemplo n.º 5
0
        private async Task <ConversationEntity> RetrieveConversationEntity(string username, string conversationId)
        {
            TableOperation retrieveOperation = TableOperation.Retrieve <ConversationEntity>(partitionKey: username, rowkey: ConversationEntity.ToRowKey(conversationId));

            try
            {
                TableResult tableResult = await table.ExecuteAsync(retrieveOperation);

                var entity = (ConversationEntity)tableResult.Result;

                if (entity == null)
                {
                    throw new ConversationNotFoundException($"Could not find a conversation with id {conversationId}");
                }

                return(entity);
            }
            catch (StorageException e)
            {
                throw new StorageErrorException($"Could not retrieve conversation {conversationId} from storage", e);
            }
        }
Ejemplo n.º 6
0
        public async Task <Message> AddMessage(string conversationId, Message message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (string.IsNullOrWhiteSpace(message.SenderUsername))
            {
                throw new ArgumentException(nameof(message));
            }

            if (string.IsNullOrWhiteSpace(message.Text))
            {
                throw new ArgumentException(nameof(message));
            }


            MessagesTableEntity messageEntity = new MessagesTableEntity
            {
                PartitionKey   = conversationId,
                RowKey         = ConversationUtils.DateTimeToRowKey(message.UtcTime),
                Text           = message.Text,
                SenderUsername = message.SenderUsername
            };

            //Define operations
            var insertOperation   = TableOperation.Insert(messageEntity);
            var retrieveOperation =
                TableOperation.Retrieve <UserConversationsIdRowEntity>(message.SenderUsername, conversationId);

            try
            {
                var result = await userConversationsTable.ExecuteAsync(retrieveOperation);

                if (result.HttpStatusCode == 404)
                {
                    var participantsFromId = Conversation.ParseId(conversationId);
                    if (!participantsFromId.Contains(message.SenderUsername))
                    {
                        throw new InvalidDataException("Sender not part of conversation");
                    }

                    throw new ConversationNotFoundException("Conversation not found");
                }

                await messageTable.ExecuteAsync(insertOperation);

                var entity = (UserConversationsIdRowEntity)result.Result;

                retrieveOperation =
                    TableOperation.Retrieve <UserConversationsIdRowEntity>(entity.Recipient, conversationId);
                var retrieveResult2 = await userConversationsTable.ExecuteAsync(retrieveOperation);

                var entity2 = (UserConversationsIdRowEntity)retrieveResult2.Result;

                await UpdateConversationTime(entity2.RowKey, entity2.PartitionKey, entity2.LastModifiedUtcTime, message.UtcTime);
                await UpdateConversationTime(entity.RowKey, entity.PartitionKey, entity.LastModifiedUtcTime, message.UtcTime);

                return(message);
            }
            catch (StorageException e)
            {
                throw new StorageUnavailableException($"Failed to reached storage {e.Message}");
            }
        }