2020-03-22 14:47:59 +01:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2020-05-11 15:34:47 +02:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-05-19 14:42:51 +02:00
|
|
|
using Newtonsoft.Json;
|
2020-06-03 12:57:29 +02:00
|
|
|
using System;
|
2020-04-24 12:46:25 +02:00
|
|
|
using System.Collections.Generic;
|
2020-06-17 15:55:58 +02:00
|
|
|
using System.Dynamic;
|
2020-04-19 18:22:44 +02:00
|
|
|
using System.Threading.Tasks;
|
2020-06-03 12:57:29 +02:00
|
|
|
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;
|
2020-05-09 11:42:45 +02:00
|
|
|
using zero.Core.Mapper;
|
2020-05-14 13:32:33 +02:00
|
|
|
using zero.Core.Options;
|
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
|
|
|
{
|
2020-05-28 00:36:52 +02:00
|
|
|
[ZeroAuthorize]
|
2020-08-13 12:03:24 +02:00
|
|
|
[ServiceFilter(typeof(ModelStateValidationFilterAttribute))]
|
2020-08-27 16:12:47 +02:00
|
|
|
public abstract class BackofficeController : Controller
|
2020-03-22 14:47:59 +01:00
|
|
|
{
|
2020-05-11 15:34:47 +02:00
|
|
|
IMapper _mapper;
|
|
|
|
|
IZeroOptions _options;
|
2020-06-03 12:57:29 +02:00
|
|
|
IToken _token;
|
2020-04-19 18:22:44 +02:00
|
|
|
|
2020-05-22 15:06:17 +02:00
|
|
|
protected IMapper Mapper => _mapper ?? (_mapper = HttpContext?.RequestServices?.GetService<IMapper>());
|
|
|
|
|
protected IZeroOptions Options => _options ?? (_options = HttpContext?.RequestServices?.GetService<IZeroOptions>());
|
2020-06-03 12:57:29 +02:00
|
|
|
protected IToken Token => _token ?? (_token = HttpContext?.RequestServices?.GetService<IToken>());
|
2020-04-19 18:22:44 +02:00
|
|
|
|
2020-06-03 12:57:29 +02:00
|
|
|
static Type AppAwareType = typeof(IAppAwareEntity);
|
|
|
|
|
static Type AppAwareShareableType = typeof(IAppAwareShareableEntity);
|
|
|
|
|
|
2020-05-27 18:29:36 +02:00
|
|
|
|
2020-05-22 15:06:17 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a Microsoft.AspNetCore.Mvc.JsonResult object that serializes the specified data object to JSON.
|
|
|
|
|
/// </summary>
|
2020-08-27 14:22:13 +02:00
|
|
|
public JsonResult Json(object data, bool typed) => Json(data);
|
2020-05-19 14:42:51 +02:00
|
|
|
|
|
|
|
|
|
2020-05-22 15:06:17 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Creates an edit model with appropriate options and permissions
|
|
|
|
|
/// </summary>
|
2020-09-06 15:34:47 +02:00
|
|
|
public IActionResult Edit<T>(T data, bool typed = true, Action<EditModel<T>> transform = null) where T : IZeroIdEntity
|
2020-05-22 15:06:17 +02:00
|
|
|
{
|
2020-06-03 12:57:29 +02:00
|
|
|
Type type = typeof(T);
|
|
|
|
|
bool canBeShared = AppAwareShareableType.IsAssignableFrom(type);
|
|
|
|
|
|
|
|
|
|
//ControllerContext.ActionDescriptor.FilterDescriptors[0].
|
|
|
|
|
|
2020-09-06 15:34:47 +02:00
|
|
|
if (data == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-20 14:35:08 +02:00
|
|
|
EditModel<T> model = new EditModel<T>();
|
2020-06-17 15:55:58 +02:00
|
|
|
model.Entity = data;
|
2020-06-24 13:00:50 +02:00
|
|
|
model.Meta.Token = Token.Get(data);
|
|
|
|
|
model.Meta.IsAppAware = AppAwareType.IsAssignableFrom(type);
|
|
|
|
|
model.Meta.CanBeShared = canBeShared;
|
|
|
|
|
model.Meta.CanCreate = true;
|
|
|
|
|
model.Meta.CanCreateShared = canBeShared;
|
|
|
|
|
model.Meta.CanEdit = true;
|
|
|
|
|
model.Meta.CanDelete = true;
|
2020-06-17 15:55:58 +02:00
|
|
|
|
|
|
|
|
transform?.Invoke(model);
|
|
|
|
|
|
|
|
|
|
return Json(model, typed);
|
2020-05-22 15:06:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-08-20 14:35:08 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Creates an edit model with appropriate options and permissions
|
|
|
|
|
/// </summary>
|
2020-09-06 15:34:47 +02:00
|
|
|
public IActionResult Edit<T, TWrapper>(TWrapper data, bool typed = true, Action<EditModel<T>> transform = null)
|
2020-08-20 14:35:08 +02:00
|
|
|
where T : IZeroIdEntity
|
|
|
|
|
where TWrapper : EditModel<T>, new()
|
|
|
|
|
{
|
|
|
|
|
Type type = typeof(T);
|
|
|
|
|
bool canBeShared = AppAwareShareableType.IsAssignableFrom(type);
|
|
|
|
|
|
|
|
|
|
//ControllerContext.ActionDescriptor.FilterDescriptors[0].
|
|
|
|
|
|
|
|
|
|
data.Meta.Token = Token.Get(data.Entity);
|
|
|
|
|
data.Meta.IsAppAware = AppAwareType.IsAssignableFrom(type);
|
|
|
|
|
data.Meta.CanBeShared = canBeShared;
|
|
|
|
|
data.Meta.CanCreate = true;
|
|
|
|
|
data.Meta.CanCreateShared = canBeShared;
|
|
|
|
|
data.Meta.CanEdit = true;
|
|
|
|
|
data.Meta.CanDelete = true;
|
|
|
|
|
|
|
|
|
|
transform?.Invoke(data);
|
|
|
|
|
|
|
|
|
|
return Json(data, 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-05-22 15:06:17 +02:00
|
|
|
|
2020-06-17 15:55:58 +02:00
|
|
|
public async Task<IActionResult> JsonPreviews<T>(Dictionary<string, T> items, Func<T, Task<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(await 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
|
|
|
|
2020-05-22 15:06:17 +02:00
|
|
|
if (result is ObsoleteEditModel)
|
2020-04-28 14:51:17 +02:00
|
|
|
{
|
2020-05-22 15:06:17 +02:00
|
|
|
ObsoleteEditModel editModel = result as ObsoleteEditModel;
|
2020-04-28 14:51:17 +02:00
|
|
|
//model.CanEdit =
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 15:06:17 +02:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 15:06:17 +02:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 15:06:17 +02:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 15:06:17 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|