using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Oqtane.Documentation;
using Oqtane.Models;
using Oqtane.Shared;
namespace Oqtane.Services
{
///
/// Service to manage entries
///
public interface IThemeService
{
///
/// Returns a list of available themes
///
///
Task> GetThemesAsync();
///
/// Returns a specific theme
///
///
///
///
Task GetThemeAsync(int themeId, int siteId);
///
/// Returns a theme s containing a specific theme control type
///
///
///
///
Theme GetTheme(List themes, string themeControlType);
///
/// Returns a list of s from the given themes
///
///
///
List GetThemeControls(List themes);
///
/// Returns a list of s for a theme containing a specific theme control type
///
///
///
///
List GetThemeControls(List themes, string themeControlType);
///
/// Returns a list of containers () for a theme containing a specific theme control type
///
///
///
///
List GetContainerControls(List themes, string themeControlType);
///
/// Updates a existing theme
///
///
///
Task UpdateThemeAsync(Theme theme);
///
/// Deletes a theme
///
///
///
Task DeleteThemeAsync(string themeName);
///
/// Creates a new theme
///
///
///
Task CreateThemeAsync(Theme theme);
///
/// Returns a list of theme templates ()
///
///
Task> GetThemeTemplatesAsync();
///
/// Returns a list of layouts () from the given themes with a matching theme name
///
///
///
///
List GetLayoutControls(List themes, string themeName);
}
[PrivateApi("Don't show in the documentation, as everything should use the Interface")]
public class ThemeService : ServiceBase, IThemeService
{
public ThemeService(HttpClient http, SiteState siteState) : base(http, siteState) { }
private string ApiUrl => CreateApiUrl("Theme");
public async Task> GetThemesAsync()
{
List themes = await GetJsonAsync>(ApiUrl);
return themes.OrderBy(item => item.Name).ToList();
}
public async Task GetThemeAsync(int themeId, int siteId)
{
return await GetJsonAsync($"{ApiUrl}/{themeId}?siteid={siteId}");
}
public Theme GetTheme(List themes, string themeControlType)
{
return themes.FirstOrDefault(item => item.Themes.Any(item => item.TypeName == themeControlType));
}
public List GetThemeControls(List themes)
{
return themes.SelectMany(item => item.Themes).OrderBy(item => item.Name).ToList();
}
public List GetThemeControls(List themes, string themeControlType)
{
return GetTheme(themes, themeControlType)?.Themes.OrderBy(item => item.Name).ToList();
}
public List GetContainerControls(List themes, string themeControlType)
{
return GetTheme(themes, themeControlType)?.Containers.OrderBy(item => item.Name).ToList();
}
public async Task UpdateThemeAsync(Theme theme)
{
await PutJsonAsync($"{ApiUrl}/{theme.ThemeId}", theme);
}
public async Task DeleteThemeAsync(string themeName)
{
await DeleteAsync($"{ApiUrl}/{themeName}");
}
public async Task CreateThemeAsync(Theme theme)
{
return await PostJsonAsync($"{ApiUrl}", theme);
}
public async Task> GetThemeTemplatesAsync()
{
List templates = await GetJsonAsync>($"{ApiUrl}/templates");
return templates;
}
//[Obsolete("This method is deprecated.", false)]
public List GetLayoutControls(List themes, string themeName)
{
return null;
}
}
}