using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using zero.Core.Api; using zero.Core.Entities; using zero.Core.Extensions; using zero.Web.Models; namespace zero.Web.Controllers { public class PagesController : BackofficeController { IPagesApi Api; IRevisionsApi RevisionsApi; public PagesController(IPagesApi api, IRevisionsApi revisionsApi) { Api = api; RevisionsApi = revisionsApi; } 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 EditModel GetEmpty([FromQuery] string type, [FromQuery] string parent = null) { PageType pageType = Api.GetPageType(type); IPage model = Activator.CreateInstance(pageType.ContentType) as IPage; model.PageTypeAlias = type; model.ParentId = parent; return Edit(model); } public async Task> GetPreviews([FromQuery] List ids) { IReadOnlyCollection pageTypes = Options.Pages.GetAllItems(); return Previews(await Api.GetByIds(ids.ToArray()), item => { PageType pageType = pageTypes.FirstOrDefault(x => x.Alias == item.PageTypeAlias); return new PreviewModel() { Id = item.Id, Icon = pageType?.Icon ?? "fth-folder", Name = item.Name, //Text = item. }; }); } 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); } }