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

107 lines
2.4 KiB
C#
Raw Normal View History

2020-03-22 14:47:59 +01:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
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;
2020-04-22 15:46:35 +02:00
using zero.Core.Api;
using zero.Core.Entities;
2020-04-16 00:56:22 +02:00
using zero.Core.Identity;
using zero.Core.Mapper;
2020-04-28 14:51:17 +02:00
using zero.Web.Filters;
2020-04-19 18:22:44 +02:00
using zero.Web.Mapper;
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-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
2020-04-19 18:22:44 +02:00
{
get
{
return _mapper ?? (_mapper = HttpContext?.RequestServices?.GetService<IMapper>());
}
}
protected IZeroOptions Options
{
get
{
return _options ?? (_options = HttpContext?.RequestServices?.GetService<IZeroOptions>());
}
2020-04-19 18:22:44 +02:00
}
public BackofficeController()
{
}
2020-04-19 18:22:44 +02:00
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-04-28 14:51:17 +02:00
if (result is EditModel)
{
EditModel editModel = result as EditModel;
//model.CanEdit =
}
2020-04-24 12:46:25 +02:00
return Json(result);
}
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-04-29 12:11:35 +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-04-29 12:11:35 +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-04-29 12:11:35 +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
}
}