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

100 lines
2.2 KiB
C#
Raw Normal View History

2020-03-22 14:47:59 +01:00
using Microsoft.AspNetCore.Mvc;
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;
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-15 14:09:52 +02:00
[ZeroAuthorize]
2020-03-22 14:47:59 +01:00
public abstract class BackofficeController : Controller
{
protected IZeroConfiguration Configuration { get; set; }
2020-04-19 18:22:44 +02:00
protected IMapper Mapper { get; set; }
2020-04-22 15:46:35 +02:00
protected IToken Token { get; set; }
2020-04-19 18:22:44 +02:00
2020-04-22 15:46:35 +02:00
public BackofficeController(IZeroConfiguration config, IMapper mapper, IToken token)
2020-04-19 18:22:44 +02:00
{
Configuration = config;
Mapper = mapper;
2020-04-22 15:46:35 +02:00
Token = token;
2020-04-19 18:22:44 +02:00
}
public BackofficeController(IZeroConfiguration config)
{
Configuration = config;
}
2020-04-19 18:22:44 +02:00
2020-04-24 12:46:25 +02:00
protected 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-22 15:46:35 +02:00
TTarget result = Mapper.Map<T, TTarget>(model);
2020-04-24 12:46:25 +02:00
return Json(result);
}
protected IActionResult As<T, TTarget>(IEnumerable<T> model) where TTarget : class, new() where T : IZeroEntity
{
if (model == null)
{
return new StatusCodeResult(404);
}
IList<TTarget> result = new List<TTarget>();
foreach (T item in model)
{
result.Add(Mapper.Map<T, TTarget>(item));
}
2020-04-22 15:46:35 +02:00
return Json(result);
}
2020-04-24 12:46:25 +02:00
protected IActionResult As<T, TTarget>(ListResult<T> model) where TTarget : class, new() where T : IZeroEntity
{
if (model == null)
{
return new StatusCodeResult(404);
}
IList<TTarget> list = new List<TTarget>();
foreach (T item in model.Items)
{
list.Add(Mapper.Map<T, TTarget>(item));
}
return Json(new ListResult<TTarget>(list, model.TotalItems, model.Page, model.PageSize)
{
Statistics = model.Statistics
});
}
2020-04-22 15:46:35 +02:00
protected TTarget Map<T, TTarget>(T model) where TTarget : class, new()
{
return Mapper.Map<T, TTarget>(model);
2020-04-19 18:22:44 +02:00
}
2020-03-22 14:47:59 +01:00
}
}