Files
mixtape/zero.Core/Configuration/Integrations/IntegrationsCollection.cs
T

288 lines
8.2 KiB
C#
Raw Normal View History

2021-11-24 15:49:25 +01:00
//using Microsoft.Extensions.Logging;
//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.Integrations;
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
//namespace zero.Core.Collections
//{
// public class IntegrationsCollection : EntityStore<Integration>, IIntegrationsCollection
// {
// /// <inheritdoc />
// public IReadOnlyCollection<IntegrationType> RegisteredTypes { get; private set; }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// protected ILogger<IntegrationsCollection> Logger { get; private set; }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// public IntegrationsCollection(IStoreContext<Integration> context, ILogger<IntegrationsCollection> logger) : base(context)
// {
// Options = new(true);
// RegisteredTypes = Context.Options.Integrations.GetAllItems();
// Logger = logger;
// }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// /// <inheritdoc />
// public Integration GetEmpty(string alias)
// {
// IntegrationType type = RegisteredTypes.FirstOrDefault(x => x.Alias.Equals(alias, StringComparison.InvariantCultureIgnoreCase));
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// if (type == null)
// {
// return null;
// }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// try
// {
// Integration model = Activator.CreateInstance(type.ContentType) as Integration;
// model.TypeAlias = type.Alias;
// return model;
// }
// catch
// {
// Logger.LogWarning("Could not create integration with type {alias}", alias);
// }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// return null;
// }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// /// <inheritdoc />
// public async Task<Integration> GetByAlias(string alias)
// {
// IntegrationType type = RegisteredTypes.FirstOrDefault(x => x.Alias.Equals(alias, StringComparison.InvariantCultureIgnoreCase));
// return await Load<Integration>(type);
// }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// /// <inheritdoc />
// public async Task<T> Get<T>(string alias = null) where T : Integration, new()
// {
// IntegrationType type = RegisteredTypes.FirstOrDefault(x => x.ContentType == typeof(T) && (alias.IsNullOrEmpty() || x.Alias.Equals(alias, StringComparison.InvariantCultureIgnoreCase)));
// return await Load<T>(type);
// }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// /// <inheritdoc />
// public async Task<IList<Integration>> GetByTag(string tag)
// {
// IEnumerable<IntegrationType> types = RegisteredTypes.Where(x => x.Tags.Contains(tag, StringComparer.InvariantCultureIgnoreCase));
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// if (!types.Any())
// {
// return new List<Integration>();
// }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// string[] aliases = types.Select(x => x.Alias).ToArray();
// return await Session.Query<Integration>().Where(x => x.TypeAlias.In(aliases)).ToListAsync();
// }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// /// <inheritdoc />
// public async Task<bool> Any(string tag)
// {
// return (await GetByTag(tag)).Any(x => x.IsActive);
// }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// /// <inheritdoc />
// public override async Task<Paged<Integration>> Load(ListQuery<Integration> query)
// {
// List<Integration> result = new();
// List<Integration> models = await Session.Query<Integration>().ToListAsync();
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// foreach (IntegrationType type in RegisteredTypes)
// {
// Integration model = models.FirstOrDefault(x => x.TypeAlias == type.Alias);
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// if (model != null)
// {
// model.Name = type.Name;
// result.Add(model);
// }
// }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// return result.ToQueriedList(query);
// }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// /// <inheritdoc />
// public async Task<IList<IntegrationTypeWithStatus>> GetTypesWithStatus()
// {
// List<IntegrationTypeWithStatus> result = new();
// List<Integration> models = await Session.Query<Integration>().ToListAsync();
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// foreach (IntegrationType type in RegisteredTypes)
// {
// Integration model = models.FirstOrDefault(x => x.TypeAlias == type.Alias);
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// result.Add(new()
// {
// Type = type,
// Id = model?.Id,
// IsActive = model?.IsActive ?? false,
// IsConfigured = model != null
// });
// }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// return result.OrderByDescending(x => x.IsActive).ThenByDescending(x => x.IsConfigured).ThenByDescending(x => x.Type.Name).ToList();
// }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// /// <inheritdoc />
// public override async Task<EntityResult<Integration>> Save(Integration model)
// {
// if (model == null)
// {
// return EntityResult<Integration>.Fail("@integration.errors.notfound");
// }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// IntegrationType type = RegisteredTypes.FirstOrDefault(x => x.Alias.Equals(model.TypeAlias, StringComparison.InvariantCultureIgnoreCase));
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// if (type == null)
// {
// return EntityResult<Integration>.Fail("@integration.errors.typenotfound");
// }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// string existingId = await Session.Query<Integration>().Where(x => x.TypeAlias == type.Alias).Select(x => x.Id).FirstOrDefaultAsync();
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// if (!existingId.IsNullOrEmpty() && existingId != model.Id)
// {
// return EntityResult<Integration>.Fail("@integration.errors.multiplenotallowed");
// }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// model.Alias = type.Alias;
// model.Name = null;
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// EntityResult<Integration> result = await base.Save(model);
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// if (result.IsSuccess)
// {
// result.Model.Name = type.Name;
// }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// return result;
// }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// /// <inheritdoc />
// public async Task<EntityResult<Integration>> Activate(string alias)
// {
// Integration model = await GetByAlias(alias);
// if (model != null)
// {
// model.IsActive = true;
// }
// return await Save(model);
// }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// /// <inheritdoc />
// public async Task<EntityResult<Integration>> Deactivate(string alias)
// {
// Integration model = await GetByAlias(alias);
// if (model != null)
// {
// model.IsActive = false;
// }
// return await Save(model);
// }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// /// <inheritdoc />
// public async Task<EntityResult<Integration>> DeleteByAlias(string alias)
// {
// return await base.Delete(await GetByAlias(alias));
// }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// /// <summary>
// /// Get integration data from database
// /// </summary>
// protected async Task<T> Load<T>(IntegrationType type) where T : Integration, new()
// {
// if (type == null)
// {
// return default;
// }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// T entity = await Session.Query<T>().FirstOrDefaultAsync(x => x.TypeAlias == type.Alias);
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// if (entity != null)
// {
// entity.Name = type.Name;
// entity.TypeAlias = type.Alias;
// }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// return WhenActive(entity) as T;
// }
// }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// public interface IIntegrationsCollection
// {
// /// <summary>
// /// Get all registered integration types
// /// </summary>
// IReadOnlyCollection<IntegrationType> RegisteredTypes { get; }
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// /// <summary>
// /// Get new integration model for the specified integration type alias
// /// </summary>
// Integration GetEmpty(string alias);
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// /// <summary>
// /// Get integration by an alias
// /// </summary>
// Task<Integration> GetByAlias(string alias);
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// /// <summary>
// /// Get an integration by type and optional alias
// /// </summary>
// Task<T> Get<T>(string alias = null) where T : Integration, new();
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// /// <summary>
// /// Get all integrations by a certain tag
// /// </summary>
// Task<IList<Integration>> GetByTag(string tag);
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// /// <summary>
// /// Check if any integrations of certain tag are activated
// /// </summary>
// Task<bool> Any(string tag);
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// /// <summary>
// /// Get all integrations with the specified query
// /// </summary>
// Task<Paged<Integration>> Load(ListQuery<Integration> query);
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// /// <summary>
// /// Get all integration types with their configuration status
// /// </summary>
// Task<IList<IntegrationTypeWithStatus>> GetTypesWithStatus();
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// /// <summary>
// /// Saves an integration
// /// </summary>
// Task<EntityResult<Integration>> Save(Integration model);
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// /// <summary>
// /// Activates a configured integration
// /// </summary>
// Task<EntityResult<Integration>> Activate(string alias);
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// /// <summary>
// /// Disables a configured integration
// /// </summary>
// Task<EntityResult<Integration>> Deactivate(string alias);
2021-01-15 13:56:50 +01:00
2021-11-24 15:49:25 +01:00
// /// <summary>
// /// Deletes configuration of an integration and disables it
// /// </summary>
// Task<EntityResult<Integration>> DeleteByAlias(string alias);
// }
//}