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

178 lines
5.0 KiB
C#
Raw Normal View History

2020-03-22 14:47:59 +01:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
2020-05-19 14:42:51 +02:00
using Newtonsoft.Json;
using System;
2020-04-24 12:46:25 +02:00
using System.Collections.Generic;
2020-04-19 18:22:44 +02:00
using System.Threading.Tasks;
using zero.Core.Api;
2020-04-22 15:46:35 +02:00
using zero.Core.Entities;
2020-04-16 00:56:22 +02:00
using zero.Core.Identity;
using zero.Core.Mapper;
using zero.Core.Options;
2020-06-03 15:16:43 +02:00
using zero.Core.Renderer;
2020-04-28 14:51:17 +02:00
using zero.Web.Filters;
2020-04-22 15:46:35 +02:00
using zero.Web.Models;
2020-03-22 14:47:59 +01:00
2020-03-24 23:09:29 +01:00
namespace zero.Web.Controllers
2020-03-22 14:47:59 +01:00
{
[ZeroAuthorize]
2020-05-22 21:19:49 +02:00
[BackofficeGenericController]
public abstract class BackofficeController : Controller, ISupportsGenericsController
2020-03-22 14:47:59 +01:00
{
IMapper _mapper;
IZeroOptions _options;
IToken _token;
2020-04-19 18:22:44 +02:00
protected IMapper Mapper => _mapper ?? (_mapper = HttpContext?.RequestServices?.GetService<IMapper>());
protected IZeroOptions Options => _options ?? (_options = HttpContext?.RequestServices?.GetService<IZeroOptions>());
protected IToken Token => _token ?? (_token = HttpContext?.RequestServices?.GetService<IToken>());
2020-04-19 18:22:44 +02:00
2020-05-27 18:29:36 +02:00
static JsonSerializerSettings JsonSettings;
static JsonSerializerSettings TypedJsonSettings;
static Type AppAwareType = typeof(IAppAwareEntity);
static Type AppAwareShareableType = typeof(IAppAwareShareableEntity);
2020-05-27 18:29:36 +02:00
static BackofficeController()
{
JsonSettings = new BackofficeJsonSerlializerSettings(false);
TypedJsonSettings = new BackofficeJsonSerlializerSettings(true);
}
/// <summary>
/// Creates a Microsoft.AspNetCore.Mvc.JsonResult object that serializes the specified data object to JSON.
/// </summary>
2020-05-27 18:29:36 +02:00
public override JsonResult Json(object data) => Json(data, false);
/// <summary>
/// Creates a Microsoft.AspNetCore.Mvc.JsonResult object that serializes the specified data object to JSON.
/// </summary>
public JsonResult Json(object data, bool typed) => Json(data, typed ? TypedJsonSettings : JsonSettings);
2020-05-19 14:42:51 +02:00
/// <summary>
/// Creates an edit model with appropriate options and permissions
/// </summary>
2020-06-03 15:16:43 +02:00
public JsonResult Edit<T>(T data, bool typed = true) where T : IZeroEntity => Edit(data, null, typed);
2020-05-27 18:29:36 +02:00
/// <summary>
/// Creates an edit model with appropriate options and permissions
/// </summary>
2020-06-03 15:16:43 +02:00
public JsonResult Edit<T>(T data, IRenderer<T> renderer, bool typed = true) where T : IZeroEntity
{
Type type = typeof(T);
bool canBeShared = AppAwareShareableType.IsAssignableFrom(type);
//ControllerContext.ActionDescriptor.FilterDescriptors[0].
return Json(new EditModel<T>()
{
2020-05-22 21:19:49 +02:00
Entity = data,
2020-06-03 15:16:43 +02:00
Renderer = renderer != null ? renderer.Build() : null,
Token = Token.Get(data),
IsAppAware = AppAwareType.IsAssignableFrom(type),
CanBeShared = canBeShared,
CanCreate = true,
CanCreateShared = canBeShared,
CanEdit = true,
CanDelete = true
2020-05-27 18:29:36 +02:00
}, typed);
}
2020-06-12 16:13:47 +02:00
public IActionResult JsonPreviews<T>(Dictionary<string, T> items, Func<T, PreviewModel> transform)
{
IList<PreviewModel> previews = new List<PreviewModel>();
foreach (var item in items)
{
bool exists = item.Value != null;
if (!exists)
{
previews.Add(new PreviewModel()
{
HasError = true,
Icon = "fth-alert-circle color-red",
Id = item.Key,
Name = "@errors.preview.notfound",
Text = "@errors.preview.notfound_text"
});
}
else
{
previews.Add(transform(item.Value));
}
}
return Json(previews);
}
2020-04-29 12:11:35 +02:00
protected async Task<IActionResult> As<T, TTarget>(T model) where TTarget : class, new() where T : IZeroEntity
2020-04-19 18:22:44 +02:00
{
if (model == null)
{
return new StatusCodeResult(404);
}
2020-04-21 16:23:43 +02:00
if (Mapper == null)
{
// TODO show error with help on how to inject mapper in constructor + base constructor
}
2020-04-29 12:11:35 +02:00
TTarget result = await Mapper.Map<T, TTarget>(model);
2020-04-22 15:46:35 +02:00
if (result is ObsoleteEditModel)
2020-04-28 14:51:17 +02:00
{
ObsoleteEditModel editModel = result as ObsoleteEditModel;
2020-04-28 14:51:17 +02:00
//model.CanEdit =
}
return Json(result);
2020-04-24 12:46:25 +02:00
}
2020-04-29 12:11:35 +02:00
protected async Task<IActionResult> As<T, TTarget>(IEnumerable<T> model) where TTarget : class, new() where T : IZeroEntity
2020-04-24 12:46:25 +02:00
{
if (model == null)
{
return new StatusCodeResult(404);
}
return Json(await Mapper.Map<T, TTarget>(model));
2020-04-22 15:46:35 +02:00
}
2020-04-29 12:11:35 +02:00
protected async Task<IActionResult> As<T, TTarget>(ListResult<T> model) where TTarget : class, new() where T : IZeroEntity
2020-04-24 12:46:25 +02:00
{
if (model == null)
{
return new StatusCodeResult(404);
}
return Json(await Mapper.Map<T, TTarget>(model));
2020-04-24 12:46:25 +02:00
}
2020-04-29 12:11:35 +02:00
protected async Task<IActionResult> As<T, TTarget>(EntityResult<T> model) where TTarget : class, new() where T : IZeroEntity
2020-04-28 14:51:17 +02:00
{
if (model == null)
{
return new StatusCodeResult(404);
}
return Json(await Mapper.Map<T, TTarget>(model));
2020-04-28 14:51:17 +02:00
}
2020-04-24 12:46:25 +02:00
2020-04-29 12:11:35 +02:00
protected async Task<TTarget> Map<T, TTarget>(T model) where TTarget : class, new()
2020-04-22 15:46:35 +02:00
{
2020-04-29 12:11:35 +02:00
return await Mapper.Map<T, TTarget>(model);
2020-04-19 18:22:44 +02:00
}
2020-03-22 14:47:59 +01:00
}
}