using Raven.Client.Documents;
using Raven.Client.Documents.Indexes;
using Raven.Client.Documents.Linq;
using Raven.Client.Documents.Session;
namespace zero.Spaces;
public class SpaceService : SpaceStore, ISpaceService
{
///
public ISpaceTypeService Types { get; protected set; }
public SpaceService(IStoreContext store, ISpaceTypeService spaceTypeService) : base(store)
{
Types = spaceTypeService;
}
///
public async Task Load(string id, string changeVector = null) where T : Space, new()
{
return await Load(id, changeVector) as T;
}
///
public async Task> Load(string spaceType, IEnumerable ids) where T : Space, new()
{
ids = ids.Distinct().ToArray();
Dictionary models = await Session.LoadAsync(ids);
Dictionary result = new();
foreach (string id in ids)
{
models.TryGetValue(id, out T model);
if (!typeof(T).IsAssignableFrom(model.GetType()))
{
result.Add(id, null);
}
else
{
result.Add(id, WhenActive(model) as T);
}
}
return result;
}
///
public async Task> Load(string spaceType, int pageNumber, int pageSize, Func, IQueryable> expression = null) where T : Space, new()
{
IRavenQueryable queryable = Session.Query().Where(x => x.Flavor == spaceType).Statistics(out QueryStatistics statistics);
expression ??= x => x;
List result = await expression(queryable).Paging(pageNumber, pageSize).ToListAsync();
return new Paged(result, statistics.TotalResults, pageNumber, pageSize);
}
///
public async Task> Load(string spaceType, int pageNumber, int pageSize, Func, IQueryable> expression = null)
where T : Space, new()
where TIndex : AbstractCommonApiForIndexes, new()
{
IRavenQueryable queryable = Session.Query().Where(x => x.Flavor == spaceType).Statistics(out QueryStatistics statistics);
expression ??= x => x;
List result = await expression(queryable).Paging(pageNumber, pageSize).ToListAsync();
return new Paged(result, statistics.TotalResults, pageNumber, pageSize);
}
}
public interface ISpaceService : ISpaceStore
{
///
/// Service for retrieving space types
///
ISpaceTypeService Types { get; }
///
/// Get editor item for a space
///
Task Load(string id, string changeVector = null) where T : Space, new();
///
/// Get editor items for a space
///
Task> Load(string spaceType, IEnumerable ids) where T : Space, new();
///
/// Get space items by query
///
Task> Load(string spaceType, int pageNumber, int pageSize, Func, IQueryable> expression = default) where T : Space, new();
///
/// Get space items by query (by using the specified index)
///
Task> Load(string spaceType, int pageNumber, int pageSize, Func, IQueryable> expression = default) where T : Space, new() where TIndex : AbstractCommonApiForIndexes, new();
}