Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
using System;
using AutoMapper;
using TableTopInc.API.Engine.AzureStorage.Models.General;
using TableTopInc.API.Engine.Models.Base;
using TableTopInc.API.Engine.Models.General;

namespace TableTopInc.API.Engine.AzureStorage.Helpers
{
public static class TableEntityMappingHelper
internal static class TableEntityMappingHelper
{
static TableEntityMappingHelper()
private static readonly Lazy<IMapper> ObjMapper = new Lazy<IMapper>(() =>
{
Mapper.Initialize(config =>
var config = new MapperConfiguration(cfg =>
{
config.CreateMap<IGameModel, GameTableEntity>();
cfg.CreateMap<IGameModel, GameTableEntity>();
cfg.CreateMap<IGameDesignerRoleModel, GameDesignerRoleTableEntity>();
});
}

public static GameTableEntity ToTableEntity(this IGameModel model)
return config.CreateMapper();
});

internal static T ToTableEntity<T>(this IEntityModel model)
{
return Mapper.Map<GameTableEntity>(model);
return ObjMapper.Value.Map<T>(model);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace TableTopInc.API.Engine.AzureStorage.Models.Base
public abstract class EntityBase : TableEntity, IEntityModel
{
public const string DefaultPartitionKey = "__shared";

[IgnoreProperty]
public string Id
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
using TableTopInc.API.Engine.AzureStorage.Models.Base;
using TableTopInc.API.Engine.AzureStorage.Helpers;
using TableTopInc.API.Engine.AzureStorage.Models.Base;
using TableTopInc.API.Engine.Models.General;

namespace TableTopInc.API.Engine.AzureStorage.Models.General
{
public class GameDesignerRoleTableEntity : EntityBase, IGameDesignerRoleModel
{
public static GameDesignerRoleTableEntity Create(IGameDesignerRoleModel model)
{
return model.ToTableEntity<GameDesignerRoleTableEntity>();
}

public string Title { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class GameTableEntity : EntityBase, IGameModel
{
public static GameTableEntity Create(IGameModel model)
{
return model.ToTableEntity();
return model.ToTableEntity<GameTableEntity>();
}

public string Title { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage.Table;
using TableTopInc.API.Engine.AzureStorage.Helpers;
using TableTopInc.API.Engine.AzureStorage.Models.Base;
using TableTopInc.API.Engine.Models.Base;
using TableTopInc.API.Engine.Services.Base;

namespace TableTopInc.API.Engine.AzureStorage.Azure
namespace TableTopInc.API.Engine.AzureStorage.Services.Base
{
public class AzureTableService<T> where T : TableEntity, new()
public abstract class AzureTableService<TableModel, EntityModel> : IEntityService<EntityModel>
where TableModel : EntityBase, ITableEntity
where EntityModel : class, IEntityModel
{
public const int BatchMaxSize = 100;
public const int QueryMaxParams = 15;
Expand All @@ -18,56 +24,65 @@ namespace TableTopInc.API.Engine.AzureStorage.Azure
protected readonly CloudTable Table;
protected readonly string EntitiesOwnerId;

internal AzureTableService(CloudTable table, string entitiesOwnerId)
internal AzureTableService(CloudTable table, string entitiesOwnerId = EntityBase.DefaultPartitionKey)
{
Table = table;
EntitiesOwnerId = entitiesOwnerId;
}

#region public

public static string ToRowKey(Guid id)
public async Task<IEnumerable<EntityModel>> SaveAsync(params EntityModel[] entities)
{
return id.ToString("N");
}
foreach (var entity in entities.Where(x => string.IsNullOrWhiteSpace(x.Id)))
{
entity.Id = ToRowKey(Guid.NewGuid());
}

await ExecuteBatchAsync(entities.Select(x => x.ToTableEntity<TableModel>()), TableOperation.InsertOrReplace);

public async Task<IEnumerable<T>> GetAllAsync()
{
return await GetByFilterAsync(string.Empty);
return entities;
}
public virtual async Task<IEnumerable<T>> GetByIdsAsync(params Guid[] ids)

public async Task DeleteByIdsAsync(params string[] ids)
{
var keys = ids
.Select(x => new KeyValuePair<string, string>(
EntitiesOwnerId,
ToRowKey(x)))
x))
.ToArray();

return await GetByKeysAsync(keys);
await DeleteByKeysAsync(keys);
}

public virtual async Task DeleteByIdsAsync(params Guid[] ids)

public async Task<IEnumerable<EntityModel>> GetAllAsync()
{
return (await GetByFilterAsync(string.Empty))
.Cast<EntityModel>();
}

public async Task<IEnumerable<EntityModel>> GetByIdsAsync(params string[] ids)
{
var keys = ids
.Select(x => new KeyValuePair<string, string>(
EntitiesOwnerId,
ToRowKey(x)))
x))
.ToArray();

await DeleteByKeysAsync(keys);
}

public async Task SaveAsync(params T[] entities)
{
await ExecuteBatchAsync(entities, TableOperation.InsertOrReplace);
return (await GetByKeysAsync(keys))
.Cast<EntityModel>();
}

#endregion

#region protected

protected async Task ExecuteBatchAsync(IEnumerable<T> entities, Func<T, TableOperation> func)
protected static string ToRowKey(Guid id)
{
return id.ToString("N");
}

protected async Task ExecuteBatchAsync(IEnumerable<TableModel> entities, Func<TableModel, TableOperation> func)
{
var groups = entities
.GroupBy(x => x.PartitionKey, (key, items) => items.ToList());
Expand Down Expand Up @@ -97,12 +112,12 @@ protected async Task DeleteByKeysAsync(params KeyValuePair<string, string>[] key
await ExecuteBatchAsync(entities, TableOperation.Delete);
}

protected async Task<IEnumerable<T>> GetByKeysAsync(params KeyValuePair<string, string>[] keys)
protected async Task<IEnumerable<TableModel>> GetByKeysAsync(params KeyValuePair<string, string>[] keys)
{
var partitions = keys
.GroupBy(x => x.Key, (key, values) => values.ToList());

var tasks = new List<Task<IEnumerable<T>>>();
var tasks = new List<Task<IEnumerable<TableModel>>>();
foreach (var rowKeys in partitions)
{
var chunks = Math.Ceiling((decimal)rowKeys.Count / QueryMaxParamsForSinglePartition);
Expand Down Expand Up @@ -134,23 +149,24 @@ protected async Task<IEnumerable<T>> GetByKeysAsync(params KeyValuePair<string,
return tasks.SelectMany(x => x.Result);
}

protected async Task<IEnumerable<T>> GetByFilterAsync(string filter)
protected async Task<IEnumerable<TableModel>> GetByFilterAsync(string filter)
{
var query = new TableQuery<T>().Where(filter);
var results = new List<T>();
var query = new TableQuery<TableEntity>().Where(filter);
var results = new List<TableModel>();

TableContinuationToken token = null;
do
{
var segment = await Table.ExecuteQuerySegmentedAsync(query, token);
token = segment.ContinuationToken;
results.AddRange(segment.Results);
results.AddRange(segment.Results.Cast<TableModel>());
}
while (token != null);

return results;
}

#endregion

}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using Microsoft.WindowsAzure.Storage.Table;
using TableTopInc.API.Engine.AzureStorage.Models.General;
using TableTopInc.API.Engine.AzureStorage.Services.Base;
using TableTopInc.API.Engine.Models.General;
using TableTopInc.API.Engine.Services.General;

namespace TableTopInc.API.Engine.AzureStorage.Services.General
{
public class GameDesignerRoleService : EntityServiceBase<GameDesignerRoleTableEntity>
public class GameDesignerRoleService : AzureTableService<GameDesignerRoleTableEntity, IGameDesignerRoleModel>, IGameDesignerRoleService
{
public const string TableName = "GameDesignerRoles";

public GameDesignerRoleService(CloudTable table)
: base(table)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Microsoft.WindowsAzure.Storage.Table;
using TableTopInc.API.Engine.AzureStorage.Models.General;
using TableTopInc.API.Engine.AzureStorage.Services.Base;
using TableTopInc.API.Engine.Models.General;
using TableTopInc.API.Engine.Services.General;

namespace TableTopInc.API.Engine.AzureStorage.Services.General
{
public class GameDesignerService : EntityServiceBase<GameDesignerTableEntity>
public class GameDesignerService : AzureTableService<GameDesignerTableEntity, IGameDesignerModel>, IGameDesignerService
{
public GameDesignerService(CloudTable table)
: base(table)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage.Table;
using TableTopInc.API.Engine.AzureStorage.Models.General;
using TableTopInc.API.Engine.AzureStorage.Services.Base;
using TableTopInc.API.Engine.Models.General;
using TableTopInc.API.Engine.Services.General;

namespace TableTopInc.API.Engine.AzureStorage.Services.General
{
public class GameService : EntityServiceBase<GameTableEntity>
public class GameService : AzureTableService<GameTableEntity, IGameModel>, IGameService
{
public const string TableName = "Games";

public GameService(CloudTable table)
: base(table)
{
}

public async Task<IEnumerable<IGameModel>> GetGamesByTagsAsync(params string[] tags)
{
throw new NotImplementedException();
}

public async Task<IEnumerable<IGameModel>> GetGamesByGameDesignersAsync(params string[] authors)
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Microsoft.WindowsAzure.Storage.Table;
using TableTopInc.API.Engine.AzureStorage.Models.General;
using TableTopInc.API.Engine.AzureStorage.Services.Base;
using TableTopInc.API.Engine.Models.General;
using TableTopInc.API.Engine.Services.General;

namespace TableTopInc.API.Engine.AzureStorage.Services.General
{
public class TagService : EntityServiceBase<TagTableEntity>
public class TagService : AzureTableService<TagTableEntity, ITagModel>, ITagService
{
public TagService(CloudTable table)
: base(table)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ namespace TableTopInc.API.Engine.Services.Base
{
public interface IEntityService<T> where T : IEntityModel
{
Task SaveAsync(params T[] entities);
Task DeleteByIdsAsync(params Guid[] ids);
Task<IEnumerable<T>> SaveAsync(params T[] entities);
Task DeleteByIdsAsync(params string[] ids);

Task<IEnumerable<T>> GetAllAsync();
Task<IEnumerable<T>> GetByIdsAsync(params Guid[] ids);
Task<IEnumerable<T>> GetByIdsAsync(params string[] ids);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace TableTopInc.API.Engine.Services.General
{
public interface IGameDesignerRoleService : IEntityService<IGameDesignerModel>
public interface IGameDesignerRoleService : IEntityService<IGameDesignerRoleModel>
{

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ namespace TableTopInc.API.Engine.Services.General
public interface IGameService : IEntityService<IGameModel>
{
Task<IEnumerable<IGameModel>> GetGamesByTagsAsync(params string[] tags);
Task<IEnumerable<IGameModel>> GetGamesByGameDesignersAsync(params Guid[] authors);
Task<IEnumerable<IGameModel>> GetGamesByGameDesignersAsync(params string[] authors);
}
}
Loading