using Microsoft.AspNetCore.Mvc; using Raven.Client.Documents; using zero.Api.Models; namespace zero.Backoffice.Endpoints.Pages; public class PagesController : ZeroBackofficeController { readonly IPagesStore Store; readonly IPageTreeService PageTreeService; readonly IPageTypeService PageTypeService; readonly IRoutes Routes; public PagesController(IPagesStore store, IPageTreeService pageTreeService, IPageTypeService pageTypeService, IRoutes routes) { Store = store; PageTreeService = pageTreeService; PageTypeService = pageTypeService; Routes = routes; } [HttpGet("{parentId}/children")] public async Task>> GetChildren(string parentId = null, string activeId = null, string search = null) { return await PageTreeService.GetChildren(parentId, activeId, search); } [HttpGet("{parentId}/dependencies")] //[ZeroAuthorize(MediaPermissions.Update)] public virtual async Task> GetDependencies(string parentId) { int descendantCount = await Store.Session.Query() .ProjectInto() .CountAsync(x => x.PathIds.Contains(parentId)); return new { pages = descendantCount + 1 }; } [HttpGet("previews")] public async Task>> GetPreviews([FromQuery] List ids) { IEnumerable pageTypes = PageTypeService.GetAll(); Dictionary pages = await Store.Load(ids.ToArray()); Dictionary routes = await Routes.GetRoutes(pages.Where(x => x.Value != null).Select(x => x.Value).ToArray()); List previews = new(); foreach ((string id, Page page) in pages) { if (page != null) { routes.TryGetValue(page, out Route route); FlavorConfig pageType = PageTypeService.GetByAlias(page.Flavor); previews.Add(new() { Name = page.Name, Text = route?.Url.Or("@page.picker.urlnotfound"), Id = page.Id, Icon = pageType?.Icon.Or("fth-folder") }); } else { previews.Add(new() { HasError = true, Icon = "fth-alert-circle color-red", Id = "tmp_" + IdGenerator.Create(), Name = "@errors.preview.notfound", Text = "@errors.preview.notfound_text" }); } } return previews; } }