using Oqtane.Models; using System.Threading.Tasks; using System.Linq; using System.Net.Http; using System.Collections.Generic; using Oqtane.Shared; using System; using System.Net; using Oqtane.Documentation; namespace Oqtane.Services { [PrivateApi("Don't show in the documentation, as everything should use the Interface")] public class PageService : ServiceBase, IPageService { private readonly SiteState _siteState; public PageService(HttpClient http, SiteState siteState) : base(http) { _siteState = siteState; } private string Apiurl => CreateApiUrl("Page", _siteState.Alias); public async Task> GetPagesAsync(int siteId) { List pages = await GetJsonAsync>($"{Apiurl}?siteid={siteId}"); pages = GetPagesHierarchy(pages); return pages; } public async Task GetPageAsync(int pageId) { return await GetJsonAsync($"{Apiurl}/{pageId}"); } public async Task GetPageAsync(int pageId, int userId) { return await GetJsonAsync($"{Apiurl}/{pageId}?userid={userId}"); } public async Task GetPageAsync(string path, int siteId) { try { return await GetJsonAsync($"{Apiurl}/path/{siteId}?path={WebUtility.UrlEncode(path)}"); } catch { return null; } } public async Task AddPageAsync(Page page) { return await PostJsonAsync(Apiurl, page); } public async Task AddPageAsync(int pageId, int userId) { return await PostJsonAsync($"{Apiurl}/{pageId}?userid={userId}", null); } public async Task UpdatePageAsync(Page page) { return await PutJsonAsync($"{Apiurl}/{page.PageId}", page); } public async Task UpdatePageOrderAsync(int siteId, int pageId, int? parentId) { var parent = parentId == null ? string.Empty : parentId.ToString(); await PutAsync($"{Apiurl}/?siteid={siteId}&pageid={pageId}&parentid={parent}"); } public async Task DeletePageAsync(int pageId) { await DeleteAsync($"{Apiurl}/{pageId}"); } private static List GetPagesHierarchy(List pages) { List hierarchy = new List(); Action, Page> getPath = null; getPath = (pageList, page) => { IEnumerable children; int level; if (page == null) { level = -1; children = pages.Where(item => item.ParentId == null); } else { level = page.Level; children = pages.Where(item => item.ParentId == page.PageId); } foreach (Page child in children) { child.Level = level + 1; child.HasChildren = pages.Any(item => item.ParentId == child.PageId); hierarchy.Add(child); getPath(pageList, child); } }; pages = pages.OrderBy(item => item.Order).ToList(); getPath(pages, null); // add any non-hierarchical items to the end of the list foreach (Page page in pages) { if (hierarchy.Find(item => item.PageId == page.PageId) == null) { hierarchy.Add(page); } } return hierarchy; } } }