using Microsoft.AspNetCore.Mvc; using Raven.Client.Documents; using Raven.Client.Documents.Session; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using zero.Core.Api; using zero.Core.Database; using zero.Core.Entities; using zero.Core.Routing; using zero.Web.Models; namespace zero.Web.Controllers { public class PagesController : BackofficeController { IPagesApi Api; IZeroStore Store; IRevisionsApi RevisionsApi; public PagesController(IPagesApi api, IRevisionsApi revisionsApi, IZeroStore store) { Api = api; RevisionsApi = revisionsApi; Store = store; } public async Task> GetAllowedPageTypes([FromQuery] string parent = null) => await Api.GetAllowedPageTypes(parent); public PageType GetPageType([FromQuery] string alias) => Api.GetPageType(alias); public async Task> GetById([FromQuery] string id) { IPage entity = await Api.GetById(id); if (entity == null) { return null; } return Edit>(new PageEditModel() { Entity = entity, //Revisions = await RevisionsApi.GetPaged(id), PageType = Api.GetPageType(entity.PageTypeAlias) }); } public async Task> GetEmpty([FromQuery] string type, [FromQuery] string parent = null) { return Edit(await Api.GetEmpty(type, parent)); } public async Task> GetPreviews([FromQuery] List ids) { IReadOnlyCollection pageTypes = Options.Pages.GetAllItems(); using IAsyncDocumentSession session = Store.OpenAsyncSession(); Dictionary pages = await session.LoadAsync(ids); Dictionary routes = await session.LoadAsync(pages.Where(x => x.Value != null).Select(x => "routes." + x.Value.Hash)); return Previews(pages, item => { PageType pageType = pageTypes.FirstOrDefault(x => x.Alias == item.PageTypeAlias); IRoute route = null; if (!routes.TryGetValue("routes." + item.Hash, out route) || route == null) { route = new Route() { Url = "No URL found" // TODO translate }; } return new PreviewModel() { Id = item.Id, Icon = pageType?.Icon ?? "fth-folder", Name = item.Name, Text = route.Url }; }); } public async Task> GetRevisions([FromQuery] string id, [FromQuery] int page = 1) => await RevisionsApi.GetPaged(id, page); public async Task> Save([FromBody] IPage model) => await Api.Save(model); [HttpPost] public async Task>> SaveSorting([FromBody] string[] ids) => await Api.SaveSorting(ids); [HttpPost] public async Task> Move([FromBody] ActionCopyModel model) => await Api.Move(model.Id, model.DestinationId); [HttpPost] public async Task> Copy([FromBody] ActionCopyModel model) => await Api.Copy(model.Id, model.DestinationId, model.IncludeDescendants); [HttpPost] public async Task> Restore([FromBody] ActionCopyModel model) => await Api.Restore(model.Id, model.IncludeDescendants); public async Task> Delete([FromQuery] string id) => await Api.Delete(id, true); } }