collection for integrations

This commit is contained in:
2020-12-16 13:22:18 +01:00
parent f099bbecaa
commit 4e2dbee4eb
11 changed files with 373 additions and 235 deletions
@@ -0,0 +1,132 @@
using FluentValidation;
using Raven.Client;
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;
namespace zero.Core.Collections
{
public abstract class CollectionCacheBase<T> : CollectionBase<T>, ICollectionBase<T>, IDisposable where T : IZeroEntity
{
public CollectionCacheBase(IZeroContext context, ICollectionInterceptorHandler interceptorHandler, IValidator<T> validator = null) : base(context, interceptorHandler, validator) { }
protected List<T> Items { get; set; }
protected async Task Preload()
{
if (Items == null || !Items.Any())
{
Items = await Session.Query<T>().ToListAsync();
}
}
/// <inheritdoc />
public override void ApplyScope(string scope)
{
Items = null;
base.ApplyScope(scope);
}
/// <inheritdoc />
public override async Task<T> GetById(string id)
{
await Preload();
if (id.IsNullOrWhiteSpace())
{
return default;
}
return Items.FirstOrDefault(x => x.Id == id);
}
/// <inheritdoc />
public override async Task<Dictionary<string, T>> GetByIds(params string[] ids)
{
await Preload();
Dictionary<string, T> models = Items.Where(x => ids.Contains(x.Id)).ToDictionary(x => x.Id, x => x);
Dictionary<string, T> result = new Dictionary<string, T>();
foreach (string id in ids)
{
models.TryGetValue(id, out T model);
result.Add(id, model);
}
return result;
}
/// <inheritdoc />
public override async Task<ListResult<T>> GetByQuery(ListQuery<T> query)
{
return await Session.Query<T>().OrderByDescending(x => x.CreatedDate).ToQueriedListAsync(query);
}
/// <inheritdoc />
public override async Task<List<T>> GetAll()
{
await Preload();
return Items;
}
/// <inheritdoc />
public override async Task<EntityResult<T>> Save(T model)
{
EntityResult<T> result = await Save(model);
if (result.IsSuccess)
{
Items = null;
}
return result;
}
/// <inheritdoc />
public override async Task<EntityResult<T>> DeleteById(string id)
{
EntityResult<T> result = await DeleteById(id);
if (result.IsSuccess)
{
Items = null;
}
return result;
}
/// <inheritdoc />
public override async Task<int> DeleteByIds(params string[] ids)
{
int successCount = await DeleteByIds(ids);
if (successCount > 0)
{
Items = null;
}
return successCount;
}
/// <inheritdoc />
public override async Task<EntityResult<T>> Purge(string querySuffix = null, Parameters parameters = null)
{
EntityResult<T> result = await Purge(querySuffix, parameters);
if (result.IsSuccess)
{
Items = null;
}
return result;
}
}
}
@@ -7,7 +7,7 @@ namespace zero.Core.Integrations
/// An integration is an application part which has a public configuration per app.
/// It's up to the user to provide functionality.
/// </summary>
public interface IIntegration
public interface IIntegrationType
{
/// <summary>
/// The alias
+16 -25
View File
@@ -1,35 +1,26 @@
using System;
using zero.Core.Attributes;
using zero.Core.Entities;
namespace zero.Core.Integrations
{
/// <summary>
/// An integration is an application part which has a public configuration per app.
/// It's up to the user to provide functionality.
/// </summary>
public class Integration : IIntegration
public abstract class Integration : ZeroEntity, IIntegration
{
/// <inheritdoc />
public string Alias { get; set; }
public Integration()
{
IsActive = true;
}
/// <inheritdoc />
public string Name { get; set; }
public string IntegrationAlias { get; set; }
}
/// <inheritdoc />
public string Description { get; set; }
/// <inheritdoc />
public string ImagePath { get; set; }
/// <inheritdoc />
public string Color { get; set; }
/// <inheritdoc />
public Type SettingsType { get; set; }
/// <inheritdoc />
public bool AllowMultiple { get; set; }
/// <inheritdoc />
public bool IsAutoActivated { get; set; }
[Collection("Integrations")]
public interface IIntegration : IZeroEntity, IZeroDbConventions
{
/// <summary>
/// Preferred countries are displayed on top in lists
/// </summary>
string IntegrationAlias { get; set; }
}
}
@@ -1,170 +0,0 @@
using Raven.Client.Documents;
using Raven.Client.Documents.Linq;
using Raven.Client.Documents.Session;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using zero.Core.Api;
using zero.Core.Database;
using zero.Core.Entities;
using zero.Core.Extensions;
using zero.Core.Options;
namespace zero.Core.Integrations
{
public class IntegrationService : BackofficeApi, IIntegrationService
{
protected IZeroOptions Options { get; private set; }
public IntegrationService(IZeroOptions options, IBackofficeStore bstore) : base(bstore)
{
Options = options;
}
/// <inheritdoc />
public IIntegration GetByAlias(string alias)
{
return GetAll().FirstOrDefault(x => x.Alias.Equals(alias, StringComparison.InvariantCultureIgnoreCase));
}
/// <inheritdoc />
public IIntegration GetBy<T>() where T : class, IIntegrationSettings
{
Type type = typeof(T);
return GetAll().FirstOrDefault(x => x.SettingsType == type);
}
/// <inheritdoc />
public IReadOnlyCollection<IIntegration> GetAll()
{
return Options.Integrations.GetAllItems();
}
/// <inheritdoc />
public async Task<IList<IIntegration>> GetActivated()
{
using IAsyncDocumentSession session = Store.OpenAsyncSession();
string[] activated = await session.Query<IIntegrationSettings>().Where(x => x.IsActive).Select(x => x.IntegrationAlias).ToArrayAsync();
return GetAll().Where(x => x.IsAutoActivated || activated.Contains(x.Alias)).ToList();
}
/// <inheritdoc />
public async Task<IList<IIntegration>> GetAvailable()
{
using IAsyncDocumentSession session = Store.OpenAsyncSession();
string[] activated = await session.Query<IIntegrationSettings>().Where(x => x.IsActive).Select(x => x.IntegrationAlias).ToArrayAsync();
return GetAll().Where(x => x.IsAutoActivated || !activated.Contains(x.Alias)).ToList();
}
/// <inheritdoc />
public async Task<IIntegrationSettings> GetSettingsByAlias(string alias)
{
IIntegration integration = GetByAlias(alias);
if (integration == null)
{
return default;
}
using IAsyncDocumentSession session = Store.OpenAsyncSession();
return await session.Query<IIntegrationSettings>()
.Where(x => x.IntegrationAlias == integration.Alias)
.FirstOrDefaultAsync();
}
/// <inheritdoc />
public async Task<IIntegrationSettings> GetSettingsById(string id)
{
using IAsyncDocumentSession session = Store.OpenAsyncSession();
return await session.LoadAsync<IIntegrationSettings>(id);
}
/// <inheritdoc />
public async Task<T> GetSettingsById<T>(string id) where T : class, IIntegrationSettings
{
using IAsyncDocumentSession session = Store.OpenAsyncSession();
return await session.LoadAsync<T>(id);
}
/// <inheritdoc />
public async Task<EntityResult<IIntegrationSettings>> Save(IIntegrationSettings model)
{
return await SaveModel(model, null);
}
/// <inheritdoc />
public async Task<EntityResult<IIntegrationSettings>> Delete(string id)
{
return await DeleteById<IIntegrationSettings>(id);
}
}
public interface IIntegrationService
{
/// <summary>
/// Get an integration by the specified alias
/// </summary>
IIntegration GetByAlias(string alias);
/// <summary>
/// Get an integration by the settings type
/// </summary>
IIntegration GetBy<T>() where T : class, IIntegrationSettings;
/// <summary>
/// Get all available app integrations
/// </summary>
IReadOnlyCollection<IIntegration> GetAll();
/// <summary>
/// Get already activated app integrations
/// </summary>
Task<IList<IIntegration>> GetActivated();
/// <summary>
/// Get available app integrations
/// </summary>
Task<IList<IIntegration>> GetAvailable();
/// <summary>
/// Get settings for a specific integration
/// </summary>
Task<IIntegrationSettings> GetSettingsByAlias(string alias);
/// <summary>
/// Get settings for a specific integration
/// </summary>
Task<IIntegrationSettings> GetSettingsById(string id);
/// <summary>
/// Get settings for a specific integration
/// </summary>
Task<T> GetSettingsById<T>(string id) where T : class, IIntegrationSettings;
/// <summary>
/// Saves settings for an integration
/// </summary>
Task<EntityResult<IIntegrationSettings>> Save(IIntegrationSettings model);
/// <summary>
/// Deletes an integration (therefore its settings)
/// </summary>
Task<EntityResult<IIntegrationSettings>> Delete(string id);
}
}
@@ -1,26 +0,0 @@
using zero.Core.Attributes;
using zero.Core.Entities;
namespace zero.Core.Integrations
{
public abstract class IntegrationSettings : ZeroEntity, IIntegrationSettings
{
public IntegrationSettings()
{
IsActive = true;
}
/// <inheritdoc />
public string IntegrationAlias { get; set; }
}
[Collection("Integrations")]
public interface IIntegrationSettings : IZeroEntity, IZeroDbConventions
{
/// <summary>
/// Preferred countries are displayed on top in lists
/// </summary>
string IntegrationAlias { get; set; }
}
}
+35
View File
@@ -0,0 +1,35 @@
using System;
namespace zero.Core.Integrations
{
/// <summary>
/// An integration is an application part which has a public configuration per app.
/// It's up to the user to provide functionality.
/// </summary>
public class IntegrationType : IIntegrationType
{
/// <inheritdoc />
public string Alias { get; set; }
/// <inheritdoc />
public string Name { get; set; }
/// <inheritdoc />
public string Description { get; set; }
/// <inheritdoc />
public string ImagePath { get; set; }
/// <inheritdoc />
public string Color { get; set; }
/// <inheritdoc />
public Type SettingsType { get; set; }
/// <inheritdoc />
public bool AllowMultiple { get; set; }
/// <inheritdoc />
public bool IsAutoActivated { get; set; }
}
}
@@ -0,0 +1,97 @@
using Raven.Client.Documents;
using Raven.Client.Documents.Linq;
using Raven.Client.Documents.Session;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using zero.Core.Database;
using zero.Core.Options;
namespace zero.Core.Integrations
{
public class IntegrationTypeService : IIntegrationTypeService
{
protected IZeroOptions Options { get; private set; }
protected IZeroStore Store { get; private set; }
public IntegrationTypeService(IZeroOptions options, IZeroStore store)
{
Options = options;
Store = store;
}
/// <inheritdoc />
public IIntegrationType GetByAlias(string alias)
{
return GetAll().FirstOrDefault(x => x.Alias.Equals(alias, StringComparison.InvariantCultureIgnoreCase));
}
/// <inheritdoc />
public IIntegrationType GetBy<T>() where T : class, IIntegration
{
Type type = typeof(T);
return GetAll().FirstOrDefault(x => x.SettingsType == type);
}
/// <inheritdoc />
public IReadOnlyCollection<IIntegrationType> GetAll()
{
return Options.Integrations.GetAllItems();
}
/// <inheritdoc />
public async Task<IList<IIntegrationType>> GetActivated()
{
using IAsyncDocumentSession session = Store.OpenAsyncSession();
string[] activated = await session.Query<IIntegration>().Where(x => x.IsActive).Select(x => x.IntegrationAlias).ToArrayAsync();
return GetAll().Where(x => x.IsAutoActivated || activated.Contains(x.Alias)).ToList();
}
/// <inheritdoc />
public async Task<IList<IIntegrationType>> GetAvailable()
{
using IAsyncDocumentSession session = Store.OpenAsyncSession();
string[] activated = await session.Query<IIntegration>().Where(x => x.IsActive).Select(x => x.IntegrationAlias).ToArrayAsync();
return GetAll().Where(x => x.IsAutoActivated || !activated.Contains(x.Alias)).ToList();
}
}
public interface IIntegrationTypeService
{
/// <summary>
/// Get an integration by the specified alias
/// </summary>
IIntegrationType GetByAlias(string alias);
/// <summary>
/// Get an integration by the settings type
/// </summary>
IIntegrationType GetBy<T>() where T : class, IIntegration;
/// <summary>
/// Get all available app integrations
/// </summary>
IReadOnlyCollection<IIntegrationType> GetAll();
/// <summary>
/// Get already activated app integrations
/// </summary>
Task<IList<IIntegrationType>> GetActivated();
/// <summary>
/// Get available app integrations
/// </summary>
Task<IList<IIntegrationType>> GetAvailable();
}
}
@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using zero.Core.Collections;
namespace zero.Core.Integrations
{
public class IntegrationsCollection : CollectionCacheBase<IIntegration>, IIntegrationsCollection
{
public IReadOnlyCollection<IIntegrationType> RegisteredTypes { get; private set; }
public IntegrationsCollection(IZeroContext context, ICollectionInterceptorHandler interceptorHandler) : base(context, interceptorHandler)
{
RegisteredTypes = context.Options.Integrations.GetAllItems();
}
/// <inheritdoc />
public async Task<T> Get<T>() where T : class, IIntegration
{
Type type = typeof(T);
IIntegrationType integration = RegisteredTypes.FirstOrDefault(x => x.SettingsType == type);
if (integration == null)
{
return default;
}
await Preload();
return Items.FirstOrDefault(x => x.IntegrationAlias == integration.Alias) as T;
}
/// <inheritdoc />
public async Task<IIntegration> GetByAlias(string alias)
{
IIntegrationType integration = RegisteredTypes.FirstOrDefault(x => x.Alias.Equals(alias, StringComparison.InvariantCultureIgnoreCase));
if (integration == null)
{
return default;
}
await Preload();
return Items.FirstOrDefault(x => x.IntegrationAlias == integration.Alias);
}
/// <inheritdoc />
public async Task<T> GetById<T>(string id) where T : class, IIntegration
{
return await GetById(id) as T;
}
}
public interface IIntegrationsCollection : ICollectionBase<IIntegration>
{
/// <summary>
/// Get settings for a specific integration
/// </summary>
Task<T> Get<T>() where T : class, IIntegration;
/// <summary>
/// Get settings for a specific integration
/// </summary>
Task<IIntegration> GetByAlias(string alias);
/// <summary>
/// Get settings for a specific integration
/// </summary>
Task<T> GetById<T>(string id) where T : class, IIntegration;
}
}
+2 -2
View File
@@ -3,9 +3,9 @@ using zero.Core.Integrations;
namespace zero.Core.Options
{
public class IntegrationOptions : ZeroBackofficeCollection<IIntegration>, IZeroCollectionOptions
public class IntegrationOptions : ZeroBackofficeCollection<IIntegrationType>, IZeroCollectionOptions
{
public void Add<T>() where T : IIntegration, new()
public void Add<T>() where T : IIntegrationType, new()
{
Items.Add(new T());
}
+12 -10
View File
@@ -8,11 +8,13 @@ namespace zero.Web.Controllers
//[ZeroAuthorize(Permissions.Sections.Spaces, PermissionsValue.True)]
public class IntegrationsController : BackofficeController
{
IIntegrationService Integrations;
IIntegrationsCollection Integrations;
IIntegrationTypeService Types;
public IntegrationsController(IIntegrationService integrations)
public IntegrationsController(IIntegrationsCollection integrations, IIntegrationTypeService types)
{
Integrations = integrations;
Types = types;
}
@@ -20,16 +22,16 @@ namespace zero.Web.Controllers
{
return Ok(new
{
available = await Integrations.GetAvailable(),
activated = await Integrations.GetActivated()
available = await Types.GetAvailable(),
activated = await Types.GetActivated()
});
}
public IActionResult GetEmptySettings([FromQuery] string alias)
{
IIntegration integration = Integrations.GetByAlias(alias);
IIntegrationSettings model = integration != null ? Activator.CreateInstance(integration.SettingsType) as IIntegrationSettings : null;
IIntegrationType integration = Types.GetByAlias(alias);
IIntegration model = integration != null ? Activator.CreateInstance(integration.SettingsType) as IIntegration : null;
if (model != null)
{
@@ -42,26 +44,26 @@ namespace zero.Web.Controllers
public async Task<IActionResult> GetSettingsByAlias([FromQuery] string alias)
{
IIntegrationSettings content = await Integrations.GetSettingsByAlias(alias);
IIntegration content = await Integrations.GetByAlias(alias);
return Ok(Edit(content));
}
public async Task<IActionResult> GetSettingsById([FromQuery] string id)
{
IIntegrationSettings content = await Integrations.GetSettingsById(id);
IIntegration content = await Integrations.GetById(id);
return Ok(Edit(content));
}
public async Task<IActionResult> Save([FromBody] IIntegrationSettings model)
public async Task<IActionResult> Save([FromBody] IIntegration model)
{
return Ok(await Integrations.Save(model));
}
public async Task<IActionResult> Delete([FromQuery] string id)
{
return Ok(await Integrations.Delete(id));
return Ok(await Integrations.DeleteById(id));
}
}
}
+2 -1
View File
@@ -117,7 +117,8 @@ namespace zero.Web.Defaults
services.AddScoped<IZeroMediaHelper, ZeroMediaHelper>();
services.AddScoped<IIntegrationService, IntegrationService>();
services.AddScoped<IIntegrationTypeService, IntegrationTypeService>();
services.AddScoped<IIntegrationsCollection, IntegrationsCollection>();
}
}
}