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

98 lines
3.3 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 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;
2021-10-05 14:08:39 +02:00
using zero.Core.Collections;
2020-05-25 11:27:23 +02:00
using zero.Core.Entities;
2021-10-05 14:08:39 +02:00
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
{
2021-10-05 14:08:39 +02:00
public class PagesController : BackofficeCollectionController<Page, IPagesCollection>
2020-05-19 15:53:01 +02:00
{
2021-10-05 14:08:39 +02:00
IRoutes Routes;
2020-05-19 15:53:01 +02:00
2021-10-05 14:08:39 +02:00
public PagesController(IPagesCollection collection, IRoutes routes) : base(collection)
2020-05-19 15:53:01 +02:00
{
2021-10-05 14:08:39 +02:00
Routes = routes;
2020-05-19 15:53:01 +02:00
}
2021-10-05 14:08:39 +02:00
public override async Task<EditModel<Page>> GetById([FromQuery] string id, [FromQuery] string changeVector = null)
2020-08-20 14:35:08 +02:00
{
2021-10-05 14:08:39 +02:00
Page entity = changeVector.IsNullOrEmpty() ? await Collection.GetById(id) : await Collection.GetRevision(changeVector);
2020-08-20 14:35:08 +02:00
2021-10-05 14:08:39 +02:00
return entity == null ? null : Edit<Page, PageEditModel<Page>>(new PageEditModel<Page>()
2020-08-20 14:35:08 +02:00
{
Entity = entity,
2021-10-11 13:58:14 +02:00
PageType = Collection.GetPageType(entity.PageTypeAlias),
Urls = new List<string>() { await Routes.GetUrl(entity) }
2020-08-20 14:35:08 +02:00
});
}
2020-07-08 13:59:46 +02:00
2021-10-05 14:08:39 +02:00
public override async Task<IList<PreviewModel>> GetPreviews([FromQuery] List<string> ids)
2020-10-29 21:44:22 +01:00
{
IReadOnlyCollection<PageType> pageTypes = Options.Pages.GetAllItems();
2021-10-05 14:08:39 +02:00
Dictionary<string, Page> pages = await Collection.GetByIds(ids.ToArray());
2021-11-07 13:51:45 +01:00
Dictionary<Page, Route> routes = await Routes.GetRoutes(pages.Where(x => x.Value != null).Select(x => x.Value).ToArray());
2020-11-02 12:44:13 +01:00
return Previews(pages, item =>
2020-10-29 21:44:22 +01:00
{
2021-10-05 14:08:39 +02:00
routes.TryGetValue(item, out Route route);
2020-11-02 12:44:13 +01:00
2021-10-05 14:08:39 +02:00
PreviewModel model = new()
2020-10-29 21:44:22 +01:00
{
Id = item.Id,
2021-10-05 14:08:39 +02:00
Icon = pageTypes.FirstOrDefault(x => x.Alias == item.PageTypeAlias)?.Icon ?? "fth-folder",
2020-10-29 21:44:22 +01:00
Name = item.Name,
2021-10-05 14:08:39 +02:00
Text = route?.Url.Or("No URL found")
2020-10-29 21:44:22 +01:00
};
2021-10-05 14:08:39 +02:00
PreviewTransform?.Invoke(item, model);
return model;
2020-10-29 21:44:22 +01:00
});
2021-10-05 14:08:39 +02:00
2020-10-29 21:44:22 +01:00
}
2021-10-05 14:08:39 +02:00
public PageType GetPageType([FromQuery] string alias) => Collection.GetPageType(alias);
2021-10-11 13:58:14 +02:00
public async Task<List<string>> GetUrls([FromQuery] string pageId)
{
string url = await Routes.GetUrl<Page>(pageId);
return url.HasValue() ? new List<string>() { url } : new List<string>();
}
2021-10-05 14:08:39 +02:00
public async Task<IList<PageType>> GetAllowedPageTypes([FromQuery] string parent = null) => await Collection.GetAllowedPageTypes(parent);
2021-10-11 13:58:14 +02:00
public async Task<EditModel<Page>> GetEmptyByType([FromQuery] string type, [FromQuery] string parent = null) => Edit(await Collection.GetEmpty(type, parent));
2020-07-08 13:59:46 +02:00
2020-08-13 14:58:01 +02:00
[HttpPost]
2021-10-05 14:08:39 +02:00
public async Task<EntityResult<IList<Page>>> SaveSorting([FromBody] string[] ids) => await Collection.SaveSorting(ids);
2020-08-13 14:58:01 +02:00
2020-08-13 15:55:59 +02:00
[HttpPost]
2021-10-05 14:08:39 +02:00
public async Task<EntityResult<Page>> Move([FromBody] ActionCopyModel model) => await Collection.Move(model.Id, model.DestinationId);
2020-08-14 13:26:48 +02:00
[HttpPost]
2021-10-05 14:08:39 +02:00
public async Task<EntityResult<Page>> Copy([FromBody] ActionCopyModel model) => await Collection.Copy(model.Id, model.DestinationId, model.IncludeDescendants);
2020-08-13 15:55:59 +02:00
2020-08-27 11:41:40 +02:00
[HttpPost]
2021-10-05 14:08:39 +02:00
public async Task<EntityResult<string[]>> Restore([FromBody] ActionCopyModel model) => await Collection.Restore(model.Id, model.IncludeDescendants);
2020-07-08 13:59:46 +02:00
2021-10-05 14:08:39 +02:00
[HttpDelete]
public async Task<EntityResult<string[]>> DeleteRecursive([FromQuery] string id) => await Collection.Delete(id, recursive: true, moveToRecycleBin: true);
2020-05-19 15:53:01 +02:00
}
}