collection for integrations
This commit is contained in:
@@ -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
|
||||
@@ -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; }
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user