using FluentValidation; using Raven.Client.Documents; using Raven.Client.Documents.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using zero.Core.Entities; using zero.Core.Extensions; using zero.Core.Options; namespace zero.Core.Collections { public abstract class CollectionConfigBase : CollectionBase, IFixedCollectionBase, IDisposable where T : IZeroConfigEntity { public CollectionConfigBase(IZeroContext context, ICollectionInterceptorHandler interceptorHandler = null, IValidator validator = null) : base(context, interceptorHandler, validator) { } protected abstract IEnumerable GetDefinedTypes(); /// public virtual async Task GetByType() where TSpecific : T, new() { OptionsType type = GetDefinedTypes().FirstOrDefault(x => x.ContentType == typeof(TSpecific)); return await GetEntity(type); } /// public virtual async Task GetByType(string alias) where TSpecific : T, new() { OptionsType type = GetDefinedTypes().FirstOrDefault(x => x.ContentType == typeof(TSpecific) && x.Alias.Equals(alias, StringComparison.InvariantCultureIgnoreCase)); return await GetEntity(type); } /// public virtual async Task GetByAlias(string alias) { OptionsType type = GetDefinedTypes().FirstOrDefault(x => x.Alias.Equals(alias, StringComparison.InvariantCultureIgnoreCase)); return type != null ? await Session.Query().FirstOrDefaultAsync(x => x.TypeAlias == type.Alias) : default; } ///// //public override async Task> GetByQuery(ListQuery query) //{ // ListResult list = await Session.Query().OrderByDescending(x => x.CreatedDate).ToQueriedListAsync(query); //} /// /// Get data from database /// async Task GetEntity(OptionsType type) where TSpecific : T, new() { if (type == null) { return default; } TSpecific model = await Session.Query().FirstOrDefaultAsync(x => x.TypeAlias == type.Alias); if (model == null) //&& type.IsAutoActivated) { return new TSpecific(); } return model; } } public interface IFixedCollectionBase : IDisposable where T : IZeroEntity { /// /// Guid for this instance /// Guid Guid { get; } /// /// The database to operate on. /// Is null by default, which uses the database from the resolved application. /// string Database { get; set; } /// /// Returns a new document queryable /// IRavenQueryable Query { get; } /// /// Applies the scope to the service instance /// void ApplyScope(string scope); /// /// Get an entity by alias /// //Task GetByAlias(string alias); } }