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

116 lines
2.9 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;
2020-04-24 12:46:25 +02:00
using System.Collections.Generic;
2020-04-19 18:22:44 +02:00
using System.Threading.Tasks;
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-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-04-28 14:51:17 +02:00
[ZeroAuthorize, CanEdit, AddToken]
2020-05-22 21:19:49 +02:00
[BackofficeGenericController]
2020-03-22 14:47:59 +01:00
public abstract class BackofficeController : Controller
{
IMapper _mapper;
IZeroOptions _options;
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>());
2020-04-19 18:22:44 +02:00
/// <summary>
/// Creates a Microsoft.AspNetCore.Mvc.JsonResult object that serializes the specified data object to JSON.
/// </summary>
public override JsonResult Json(object data)
2020-05-19 14:42:51 +02:00
{
JsonSerializerSettings settings = JsonConvert.DefaultSettings();
settings.TypeNameHandling = TypeNameHandling.Objects;
return Json(data, settings);
}
/// <summary>
/// Creates an edit model with appropriate options and permissions
/// </summary>
public JsonResult JsonEdit<T>(T data)
{
return Json(new EditModel<T>()
{
2020-05-22 21:19:49 +02:00
Entity = data,
CanEdit = true
});
}
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
}
}