using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using zero.Core.Collections;
using zero.Core.Entities;
using zero.Core.Extensions;
using zero.Core.Handlers;
using zero.Core.Options;
namespace zero.Core.Api
{
public class ModulesApi : BackofficeApi, IModulesApi
{
protected IZeroOptions Options { get; private set; }
protected IHandlerHolder Handler { get; private set; }
public ModulesApi(IZeroOptions options, ICollectionContext store, IHandlerHolder handler) : base(store)
{
Options = options;
Handler = handler;
}
///
public async Task> GetModuleTypes(string[] tags = default, string pageId = default)
{
IEnumerable types = Options.Modules.GetAllItems();
List modules = types.ToList();
Page page = null;
if (!pageId.IsNullOrEmpty())
{
page = await GetById(pageId);
}
if (tags?.Length > 0)
{
modules = types.Where(x => x.Tags.Any(t => tags.Contains(t, StringComparer.InvariantCultureIgnoreCase))).ToList();
}
IModuleTypeHandler handler = Handler.Get();
// if there is no registered handler we just allow all page types
if (handler == null)
{
return modules;
}
return handler.GetAllowedModuleTypes(Context.Context.Application, types, page, tags)?.ToList() ?? new();
}
///
public ModuleType GetModuleType(string alias)
{
return Options.Modules.GetAllItems().FirstOrDefault(x => x.Alias == alias);
}
}
public interface IModulesApi
{
///
/// Get all available module types (can be limited to the passed tags)
///
Task> GetModuleTypes(string[] tags = default, string pageId = default);
///
/// Get a specific module type by alias
///
ModuleType GetModuleType(string alias);
}
}