Files
mixtape/zero.Web/Controllers/PagesController.cs
T

117 lines
3.4 KiB
C#
Raw Normal View History

2020-05-19 15:53:01 +02:00
using Microsoft.AspNetCore.Mvc;
2020-11-02 12:44:13 +01:00
using Raven.Client.Documents;
using Raven.Client.Documents.Session;
2020-09-02 11:52:33 +02:00
using System;
using System.Collections.Generic;
2020-10-29 21:44:22 +01:00
using System.Linq;
2020-05-19 15:53:01 +02:00
using System.Threading.Tasks;
using zero.Core.Api;
2020-05-25 11:27:23 +02:00
using zero.Core.Entities;
using zero.Core.Extensions;
2020-11-02 12:44:13 +01:00
using zero.Core.Routing;
2020-08-20 14:35:08 +02:00
using zero.Web.Models;
2020-05-19 15:53:01 +02:00
namespace zero.Web.Controllers
{
public class PagesController : BackofficeController
2020-05-19 15:53:01 +02:00
{
IPagesApi Api;
2020-11-02 12:44:13 +01:00
IDocumentStore Raven;
2020-08-17 14:46:13 +02:00
IRevisionsApi RevisionsApi;
2020-05-19 15:53:01 +02:00
2020-11-02 12:44:13 +01:00
public PagesController(IPagesApi api, IRevisionsApi revisionsApi, IDocumentStore raven)
2020-05-19 15:53:01 +02:00
{
Api = api;
2020-08-17 14:46:13 +02:00
RevisionsApi = revisionsApi;
2020-11-02 12:44:13 +01:00
Raven = raven;
2020-05-19 15:53:01 +02:00
}
public async Task<IList<PageType>> GetAllowedPageTypes([FromQuery] string parent = null) => await Api.GetAllowedPageTypes(parent);
2020-07-08 13:59:46 +02:00
public PageType GetPageType([FromQuery] string alias) => Api.GetPageType(alias);
2020-05-25 11:27:23 +02:00
public async Task<PageEditModel<IPage>> GetById([FromQuery] string id)
2020-08-20 14:35:08 +02:00
{
IPage entity = await Api.GetById(id);
2020-08-20 14:35:08 +02:00
if (entity == null)
{
return null;
}
return Edit<IPage, PageEditModel<IPage>>(new PageEditModel<IPage>()
2020-08-20 14:35:08 +02:00
{
Entity = entity,
2020-08-28 14:26:26 +02:00
//Revisions = await RevisionsApi.GetPaged<IPage>(id),
2020-08-20 14:35:08 +02:00
PageType = Api.GetPageType(entity.PageTypeAlias)
});
}
2020-07-08 13:59:46 +02:00
2020-11-06 14:16:11 +01:00
public async Task<EditModel<IPage>> GetEmpty([FromQuery] string type, [FromQuery] string parent = null)
{
2020-11-06 14:16:11 +01:00
return Edit(await Api.GetEmpty(type, parent));
}
2020-07-08 13:59:46 +02:00
2020-10-29 21:44:22 +01:00
public async Task<IList<PreviewModel>> GetPreviews([FromQuery] List<string> ids)
{
IReadOnlyCollection<PageType> pageTypes = Options.Pages.GetAllItems();
2020-11-02 12:44:13 +01:00
using IAsyncDocumentSession session = Raven.OpenAsyncSession();
Dictionary<string, IPage> pages = await session.LoadAsync<IPage>(ids);
Dictionary<string, IRoute> routes = await session.LoadAsync<IRoute>(pages.Where(x => x.Value != null).Select(x => "routes." + x.Value.Hash));
return Previews(pages, item =>
2020-10-29 21:44:22 +01:00
{
PageType pageType = pageTypes.FirstOrDefault(x => x.Alias == item.PageTypeAlias);
2020-11-02 12:44:13 +01:00
IRoute route = null;
if (!routes.TryGetValue("routes." + item.Hash, out route))
{
route = new Route()
{
Url = "No URL found" // TODO translate
};
}
2020-10-29 21:44:22 +01:00
return new PreviewModel()
{
Id = item.Id,
Icon = pageType?.Icon ?? "fth-folder",
Name = item.Name,
2020-11-02 12:44:13 +01:00
Text = route.Url
2020-10-29 21:44:22 +01:00
};
});
}
public async Task<ListResult<Revision>> GetRevisions([FromQuery] string id, [FromQuery] int page = 1) => await RevisionsApi.GetPaged<IPage>(id, page);
public async Task<EntityResult<IPage>> Save([FromBody] IPage model) => await Api.Save(model);
2020-07-08 13:59:46 +02:00
2020-08-13 14:58:01 +02:00
[HttpPost]
public async Task<EntityResult<IList<IPage>>> SaveSorting([FromBody] string[] ids) => await Api.SaveSorting(ids);
2020-08-13 14:58:01 +02:00
2020-08-13 15:55:59 +02:00
[HttpPost]
public async Task<EntityResult<IPage>> Move([FromBody] ActionCopyModel model) => await Api.Move(model.Id, model.DestinationId);
2020-08-14 13:26:48 +02:00
[HttpPost]
public async Task<EntityResult<IPage>> Copy([FromBody] ActionCopyModel model) => await Api.Copy(model.Id, model.DestinationId, model.IncludeDescendants);
2020-08-13 15:55:59 +02:00
2020-08-27 11:41:40 +02:00
[HttpPost]
public async Task<EntityResult<string[]>> Restore([FromBody] ActionCopyModel model) => await Api.Restore(model.Id, model.IncludeDescendants);
2020-07-08 13:59:46 +02:00
public async Task<EntityResult<string[]>> Delete([FromQuery] string id) => await Api.Delete(id, true);
2020-05-19 15:53:01 +02:00
}
}