Files
mixtape/zero.Core/Pages/Modules/ModulesApi.cs
T

76 lines
2.0 KiB
C#
Raw Normal View History

2020-10-20 16:00:55 +02:00
using System;
using System.Collections.Generic;
2020-08-19 12:53:18 +02:00
using System.Linq;
2021-01-06 19:29:11 +01:00
using System.Threading.Tasks;
2021-10-13 12:47:18 +02:00
using zero.Core.Collections;
2020-08-19 12:53:18 +02:00
using zero.Core.Entities;
2021-01-06 19:29:11 +01:00
using zero.Core.Extensions;
using zero.Core.Handlers;
2020-08-19 12:53:18 +02:00
using zero.Core.Options;
namespace zero.Core.Api
{
public class ModulesApi : BackofficeApi, IModulesApi
{
protected IZeroOptions Options { get; private set; }
2021-01-06 19:29:11 +01:00
protected IHandlerHolder Handler { get; private set; }
2021-11-23 22:56:22 +01:00
public ModulesApi(IZeroOptions options, IStoreContext store, IHandlerHolder handler) : base(store)
2020-08-19 12:53:18 +02:00
{
Options = options;
2021-01-06 19:29:11 +01:00
Handler = handler;
2020-08-19 12:53:18 +02:00
}
/// <inheritdoc />
2021-11-24 15:49:25 +01:00
public async Task<IList<PageModuleType>> GetModuleTypes(string[] tags = default, string pageId = default)
2020-08-19 12:53:18 +02:00
{
2021-11-24 15:49:25 +01:00
IEnumerable<PageModuleType> types = Options.Modules.GetAllItems();
List<PageModuleType> modules = types.ToList();
2021-05-04 17:23:52 +02:00
Page page = null;
2021-01-06 19:29:11 +01:00
if (!pageId.IsNullOrEmpty())
{
2021-05-04 17:23:52 +02:00
page = await GetById<Page>(pageId);
2021-01-06 19:29:11 +01:00
}
2020-10-20 16:00:55 +02:00
if (tags?.Length > 0)
{
2021-01-06 19:29:11 +01:00
modules = types.Where(x => x.Tags.Any(t => tags.Contains(t, StringComparer.InvariantCultureIgnoreCase))).ToList();
2020-10-20 16:00:55 +02:00
}
2021-01-06 19:29:11 +01:00
IModuleTypeHandler handler = Handler.Get<IModuleTypeHandler>();
// if there is no registered handler we just allow all page types
if (handler == null)
{
return modules;
}
2021-10-13 12:47:18 +02:00
return handler.GetAllowedModuleTypes(Context.Context.Application, types, page, tags)?.ToList() ?? new();
2020-08-19 12:53:18 +02:00
}
/// <inheritdoc />
2021-11-24 15:49:25 +01:00
public PageModuleType GetModuleType(string alias)
2020-08-19 12:53:18 +02:00
{
return Options.Modules.GetAllItems().FirstOrDefault(x => x.Alias == alias);
}
}
public interface IModulesApi
{
/// <summary>
2020-10-20 16:00:55 +02:00
/// Get all available module types (can be limited to the passed tags)
2020-08-19 12:53:18 +02:00
/// </summary>
2021-11-24 15:49:25 +01:00
Task<IList<PageModuleType>> GetModuleTypes(string[] tags = default, string pageId = default);
2020-08-19 12:53:18 +02:00
/// <summary>
/// Get a specific module type by alias
/// </summary>
2021-11-24 15:49:25 +01:00
PageModuleType GetModuleType(string alias);
2020-08-19 12:53:18 +02:00
}
}