예제 #1
0
        public async static Task <T> ReplaceAsync <T>(this IAzureTableStorage <AzureIndex> indexTableStorage, string indexPartitionKey,
                                                      string indexRowKey, IAzureTableStorage <T> tableStorage, Func <T, T> action) where T : class, ITableEntity, new()
        {
            var indexEntity = await indexTableStorage.GetDataAsync(indexPartitionKey, indexRowKey);

            return(await tableStorage.ReplaceAsync(indexEntity, action));
        }
        public static async Task <T> InsertOrModifyAsync <T>(this IAzureTableStorage <T> tableStorage, string partitionKey,
                                                             string rowKey, Func <T> createNew, Func <T, T> modify)
            where T : class, ITableEntity, new()
        {
            for (var i = 0; i < 100; i++)
            {
                try
                {
                    var result = await tableStorage.ReplaceAsync(partitionKey,
                                                                 rowKey, modify);

                    if (result != null)
                    {
                        return(result);
                    }

                    result = createNew();
                    await tableStorage.InsertAsync(result);

                    return(result);
                }
                catch (Exception)
                {
                }
            }

            throw new Exception("Can not insert or modify entity");
        }
        public static async Task <T> ModifyOrCreateAsync <T>(this IAzureTableStorage <T> tableStorage,
                                                             string partitionKey, string rowKey, Func <T> create, Action <T> update) where T : ITableEntity, new()
        {
            for (var i = 0; i < 15; i++)
            {
                try
                {
                    var entity = await tableStorage.ReplaceAsync(partitionKey, rowKey, itm =>
                    {
                        update(itm);
                        return(itm);
                    });

                    if (entity != null)
                    {
                        return(entity);
                    }

                    entity = create();
                    await tableStorage.InsertAsync(entity);

                    return(entity);
                }
                catch (Exception)
                {
                }
            }
            throw new Exception("Can not modify or update entity: " + PrintItem(create()));
        }
예제 #4
0
        public async static Task <T> ReplaceAsync <T>(this IAzureTableStorage <T> tableStorage, IAzureIndex index, Func <T, T> action) where T : class, ITableEntity, new()
        {
            if (index == null)
            {
                return(null);
            }

            return(await tableStorage.ReplaceAsync(index.PrimaryPartitionKey, index.PrimaryRowKey, action));
        }
예제 #5
0
        public Task UpdateAsync(IProjectVoteData projectVoteData)
        {
            var partitionKey = ProjectVoteEntity.GeneratePartitionKey(projectVoteData.ProjectId);
            var rowKey       = ProjectVoteEntity.GenerateRowKey(projectVoteData.VoterUserId);

            return(_projectVoteTableStorage.ReplaceAsync(partitionKey, rowKey, itm =>
            {
                itm.Update(projectVoteData);
                return itm;
            }));
        }
예제 #6
0
        public Task UpdateAsync(IProjectResultData resultData)
        {
            var partitionKey = ProjectResultEntity.GeneratePartitionKey(resultData.ProjectId);
            var rowKey       = ProjectResultEntity.GenerateRowKey(resultData.ParticipantId);

            return(_projectResultInfoTableStorage.ReplaceAsync(partitionKey, rowKey, itm =>
            {
                itm.Update(resultData);
                return itm;
            }));
        }
        public Task UpdateAsync(ICommentData projectCommentData)
        {
            var partitionKey = CommentEntity.GeneratePartitionKey(projectCommentData.ProjectId);
            var rowKey       = CommentEntity.GenerateRowKey(projectCommentData.Id);

            return(_projectCommentsTableStorage.ReplaceAsync(partitionKey, rowKey, itm =>
            {
                itm.Update(projectCommentData);
                return itm;
            }));
        }
예제 #8
0
        public Task ChangePasswordAsync(string id, string newPassword)
        {
            var partitionKey = UserEntity.GeneratePartitionKey();
            var rowKey       = UserEntity.GenerateRowKey(id);

            return(_tableStorage.ReplaceAsync(partitionKey, rowKey, itm =>
            {
                itm.SetPassword(newPassword);
                return itm;
            }));
        }
예제 #9
0
        public static async Task <string> GenerateIdAsync(this IAzureTableStorage <SetupEntity> tableStorage,
                                                          string partitionKey, string rowKey, int fromId)
        {
            while (true)
            {
                try
                {
                    var result = await tableStorage.ReplaceAsync(partitionKey, rowKey, itm =>
                    {
                        int i;

                        try
                        {
                            i = int.Parse(itm.Value);
                        }
                        catch (System.Exception)
                        {
                            i = fromId;
                        }

                        itm.Value = (i + 1).ToString(CultureInfo.InvariantCulture);
                        return(itm);
                    });

                    if (result != null)
                    {
                        return(result.Value);
                    }


                    var idEntity = SetupEntity.Create(partitionKey, rowKey,
                                                      fromId.ToString(CultureInfo.InvariantCulture));

                    await tableStorage.InsertAsync(idEntity);
                }

                catch (StorageException e)
                {
                    if (e.RequestInformation.HttpStatusCode != TableStorageUtils.Conflict)
                    {
                        throw;
                    }
                }
            }
        }
 public static Task <T> ReplaceAsync <T>(this IAzureTableStorage <T> tableStorage, T item,
                                         Func <T, T> updateAction) where T : ITableEntity, new()
 {
     return(tableStorage.ReplaceAsync(item.PartitionKey, item.RowKey, updateAction));
 }