output users + roles

This commit is contained in:
2020-04-24 12:46:25 +02:00
parent a44cc4b04d
commit 76e95d34f3
18 changed files with 430 additions and 99 deletions
+40 -8
View File
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using zero.Core;
using zero.Core.Api;
@@ -33,7 +34,7 @@ namespace zero.Web.Controllers
}
protected async Task<IActionResult> As<T, TTarget>(T model) where TTarget : class, new() where T : IZeroEntity
protected IActionResult As<T, TTarget>(T model) where TTarget : class, new() where T : IZeroEntity
{
if (model == null)
{
@@ -47,18 +48,49 @@ namespace zero.Web.Controllers
TTarget result = Mapper.Map<T, TTarget>(model);
//if (result is EditModel)
//{
// (result as EditModel).Meta = new EditModelMeta()
// {
// Token = await Token.Get(model)
// };
//}
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));
}
return Json(result);
}
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
});
}
protected TTarget Map<T, TTarget>(T model) where TTarget : class, new()
{
return Mapper.Map<T, TTarget>(model);
+4 -3
View File
@@ -28,7 +28,7 @@ namespace zero.Web.Controllers
[AddToken]
public async Task<IActionResult> GetById([FromQuery] string id)
{
return await As<Country, CountryEditModel>(await Api.GetById(id));
return As<Country, CountryEditModel>(await Api.GetById(id));
}
@@ -37,7 +37,7 @@ namespace zero.Web.Controllers
/// </summary>
public async Task<IActionResult> GetAll([FromQuery] ListQuery<Country> query)
{
return Json(await Api.GetByQuery("en-US", query));
return As<Country, CountryListModel>(await Api.GetByQuery("en-US", query));
}
@@ -48,7 +48,8 @@ namespace zero.Web.Controllers
[ZeroAuthorize(Permissions.Settings.Countries, PermissionsValue.Write)]
public async Task<IActionResult> Save([FromBody] CountryEditModel model)
{
return Json(await Api.Save(Mapper.Map<CountryEditModel, Country>(model)));
Country country = Mapper.Map(model, await Api.GetById(model.Id));
return Json(await Api.Save(country));
}
@@ -0,0 +1,40 @@
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using zero.Core;
using zero.Core.Api;
using zero.Core.Entities;
using zero.Core.Identity;
using zero.Web.Mapper;
using zero.Web.Models;
namespace zero.Web.Controllers
{
[ZeroAuthorize(Permissions.Settings.Users, PermissionsValue.Read)]
public class UserRolesController : BackofficeController
{
private IUserRolesApi Api { get; set; }
public UserRolesController(IZeroConfiguration config, IUserRolesApi api, IMapper mapper, IToken token) : base(config, mapper, token)
{
Api = api;
}
/// <summary>
/// Get role by id
/// </summary>
public async Task<IActionResult> GetById([FromQuery] string id)
{
return As<UserRole, UserRoleEditModel>(await Api.GetById(id));
}
/// <summary>
/// Get all roles
/// </summary>
public async Task<IActionResult> GetAll()
{
return As<UserRole, UserRoleListModel>(await Api.GetAll());
}
}
}
+2 -2
View File
@@ -25,7 +25,7 @@ namespace zero.Web.Controllers
/// </summary>
public async Task<IActionResult> GetById([FromQuery] string id)
{
return await As<User, UserEditModel>(await Api.GetUserById(id));
return As<User, UserEditModel>(await Api.GetUserById(id));
}
@@ -34,7 +34,7 @@ namespace zero.Web.Controllers
/// </summary>
public async Task<IActionResult> GetAll([FromQuery] ListQuery<User> query)
{
return Json(await Api.GetByQuery(query, "zero.applications.1-A"));
return As<User, UserListModel>(await Api.GetByQuery(query, "zero.applications.1-A"));
}
}
}