forked from ddrilling/AsbCloudServer
CS2-123: Changed UserRoleService and controller to inherit CrudController/service
This commit is contained in:
parent
908c855463
commit
576d119674
@ -48,7 +48,6 @@ namespace AsbCloudInfrastructure
|
||||
services.AddTransient<IDrillParamsService, DrillParamsService>();
|
||||
services.AddTransient<IDrillFlowChartService, DrillFlowChartService>();
|
||||
services.AddTransient<ITimeZoneService, TimeZoneService>();
|
||||
services.AddTransient<IUserRoleService, UserRoleService>();
|
||||
|
||||
// admin crud services:
|
||||
services.AddTransient<ICrudService<DepositDto>, CrudServiceBase<DepositDto, Deposit>>();
|
||||
@ -56,6 +55,7 @@ namespace AsbCloudInfrastructure
|
||||
services.AddTransient<ICrudService<WellDto>, CrudServiceBase<WellDto, Well>>();
|
||||
services.AddTransient<ICrudService<CompanyDto>, CrudServiceBase<CompanyDto, Company>>();
|
||||
services.AddTransient<ICrudService<UserDto>, CrudServiceBase<UserDto, User>>();
|
||||
services.AddTransient<ICrudService<UserRoleDto>, CrudServiceBase<UserRoleDto, UserRole>>();
|
||||
services.AddTransient<ICrudService<TelemetryDto>, CrudServiceBase<TelemetryDto, Telemetry>>();
|
||||
services.AddTransient<ICrudService<DrillParamsDto>, DrillParamsService>();
|
||||
|
||||
|
@ -81,12 +81,13 @@ namespace AsbCloudInfrastructure.Services
|
||||
return dto;
|
||||
}
|
||||
|
||||
public virtual Task<int> InsertAsync(TDto item, CancellationToken token = default)
|
||||
public virtual async Task<int> InsertAsync(TDto item, CancellationToken token = default)
|
||||
{
|
||||
var entity = Convert(item);
|
||||
entity.Id = 0;
|
||||
dbSet.Add(entity);
|
||||
return context.SaveChangesAsync(token);
|
||||
await context.SaveChangesAsync(token);
|
||||
return entity.Id;
|
||||
}
|
||||
|
||||
public virtual Task<int> InsertRangeAsync(IEnumerable<TDto> items, CancellationToken token = default)
|
||||
|
@ -3,95 +3,79 @@ using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Services;
|
||||
using AsbCloudDb.Model;
|
||||
using AsbCloudInfrastructure.Services.Cache;
|
||||
using Mapster;
|
||||
|
||||
namespace AsbCloudInfrastructure.Services
|
||||
{
|
||||
public class UserRoleService : IUserRoleService
|
||||
public class UserRoleService : CrudServiceBase<UserRoleDto, UserRole>
|
||||
{
|
||||
private readonly IAsbCloudDbContext db;
|
||||
private readonly CacheTable<UserRole> cacheUserRoles;
|
||||
private readonly CacheTable<Permission> cachePermissions;
|
||||
private readonly CacheTable<RelationUserRolePermission> cacheUserRolesPermissions;
|
||||
|
||||
public UserRoleService(IAsbCloudDbContext db, CacheDb cacheDb)
|
||||
public UserRoleService(IAsbCloudDbContext context, CacheDb cacheDb) : base(context)
|
||||
{
|
||||
this.db = db;
|
||||
cacheUserRoles = cacheDb.GetCachedTable<UserRole>((AsbCloudDbContext)db);
|
||||
cachePermissions = cacheDb.GetCachedTable<Permission>((AsbCloudDbContext)db);
|
||||
cacheUserRoles = cacheDb.GetCachedTable<UserRole>((AsbCloudDbContext)context);
|
||||
cachePermissions = cacheDb.GetCachedTable<Permission>((AsbCloudDbContext)context);
|
||||
cacheUserRolesPermissions =
|
||||
cacheDb.GetCachedTable<RelationUserRolePermission>((AsbCloudDbContext)db);
|
||||
cacheDb.GetCachedTable<RelationUserRolePermission>((AsbCloudDbContext)context);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<UserRoleDto>> GetAllAsync(CancellationToken token)
|
||||
public override async Task<IEnumerable<UserRoleDto>> GetAllAsync(CancellationToken token = default)
|
||||
{
|
||||
var rolesDtos = (await cacheUserRoles.WhereAsync(token).ConfigureAwait(false))
|
||||
.Adapt<UserRoleDto>();
|
||||
var rolesDtos = await base.GetAllAsync(token);
|
||||
|
||||
return rolesDtos.Select(FillUserRoleWithPermissions);
|
||||
}
|
||||
|
||||
public async Task<UserRoleDto> GetAsync(int id, CancellationToken token)
|
||||
public override async Task<UserRoleDto> GetAsync(int id, CancellationToken token = default)
|
||||
{
|
||||
var roleDto = (await cacheUserRoles.FirstOrDefaultAsync(r => r.Id == id, token)
|
||||
.ConfigureAwait(false)).Adapt<UserRoleDto>();
|
||||
var roleDto = await base.GetAsync(id,token);
|
||||
|
||||
return roleDto is null ? null : FillUserRoleWithPermissions(roleDto);
|
||||
}
|
||||
|
||||
public async Task<int> InsertAsync(UserRoleDto dto, CancellationToken token = default)
|
||||
public override async Task<int> InsertAsync(UserRoleDto dto, CancellationToken token = default)
|
||||
{
|
||||
var newRole = dto.Adapt<UserRole>();
|
||||
db.UserRoles.Add(newRole);
|
||||
var result = await db.SaveChangesAsync(token);
|
||||
var newRoleId = await base.InsertAsync(dto, token);
|
||||
|
||||
if (dto.PermissionIds == default)
|
||||
return result;
|
||||
return newRoleId;
|
||||
|
||||
foreach (var pId in dto.PermissionIds)
|
||||
{
|
||||
var relation = new RelationUserRolePermission()
|
||||
{
|
||||
IdUserRole = newRole.Id,
|
||||
IdUserRole = newRoleId,
|
||||
IdPermission = pId
|
||||
};
|
||||
|
||||
db.RelationUserRolesPermissions.Add(relation);
|
||||
context.RelationUserRolesPermissions.Add(relation);
|
||||
}
|
||||
|
||||
return await db.SaveChangesAsync(token);
|
||||
return await context.SaveChangesAsync(token);
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(UserRoleDto dto, CancellationToken token = default)
|
||||
public override async Task<int> UpdateAsync(int id, UserRoleDto item, CancellationToken token = default)
|
||||
{
|
||||
var entity = dto.Adapt<UserRole>();
|
||||
db.UserRoles.Update(entity);
|
||||
await db.SaveChangesAsync(token);
|
||||
var result = await base.UpdateAsync(id, item, token);
|
||||
|
||||
if (dto.PermissionIds != default)
|
||||
if (item.PermissionIds == default)
|
||||
return result;
|
||||
|
||||
await cacheUserRolesPermissions.RemoveAsync(r => r.IdUserRole == item.Id, token)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var newRelations = item.PermissionIds.Select(p => new RelationUserRolePermission()
|
||||
{
|
||||
await cacheUserRolesPermissions.RemoveAsync(r => r.IdUserRole == dto.Id, token)
|
||||
.ConfigureAwait(false);
|
||||
IdUserRole = item.Id,
|
||||
IdPermission = p
|
||||
});
|
||||
await cacheUserRolesPermissions.InsertAsync(newRelations, token);
|
||||
|
||||
var newRelations = dto.PermissionIds.Select(p => new RelationUserRolePermission()
|
||||
{
|
||||
IdUserRole = dto.Id,
|
||||
IdPermission = p
|
||||
});
|
||||
await cacheUserRolesPermissions.InsertAsync(newRelations, token);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token)
|
||||
{
|
||||
var entities = cacheUserRoles.Where(e => ids.Contains(e.Id));
|
||||
if (entities == default)
|
||||
return 0;
|
||||
db.UserRoles.RemoveRange(entities);
|
||||
return await db.SaveChangesAsync(token);
|
||||
return result;
|
||||
}
|
||||
|
||||
private UserRoleDto FillUserRoleWithPermissions(UserRoleDto roleDto)
|
||||
|
@ -1,7 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@ -11,93 +8,10 @@ namespace AsbCloudWebApi.Controllers
|
||||
[Route("api/admin/user/role")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class AdminUserRoleController : ControllerBase
|
||||
public class AdminUserRoleController : CrudController<UserRoleDto, ICrudService<UserRoleDto>>
|
||||
{
|
||||
private readonly IUserRoleService userRoleService;
|
||||
|
||||
public AdminUserRoleController(IUserRoleService userRoleService)
|
||||
{
|
||||
this.userRoleService = userRoleService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получает список всех доступных ролей
|
||||
/// </summary>
|
||||
/// <param name="token">Токен отмены задачи</param>
|
||||
/// <returns>Список всех доступных ролей</returns>
|
||||
[HttpGet]
|
||||
[ProducesResponseType(typeof(IEnumerable<UserRoleDto>), (int)System.Net.HttpStatusCode.OK)]
|
||||
public async Task<IActionResult> GetAllAsync(CancellationToken token = default)
|
||||
{
|
||||
// TODO: Как будем делать проверку ролей пользователя? Админ, не админ.
|
||||
|
||||
var result = await userRoleService.GetAllAsync(token)
|
||||
.ConfigureAwait(false);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получает информацию о запрашиваемой роли
|
||||
/// </summary>
|
||||
/// <param name="idRole">id запрашиваемой задачи</param>
|
||||
/// <param name="token">Токен отмены задачи</param>
|
||||
/// <returns>Информацию о запрашиваемой роли</returns>
|
||||
[HttpGet("{idRole}")]
|
||||
[ProducesResponseType(typeof(UserRoleDto), (int)System.Net.HttpStatusCode.OK)]
|
||||
public async Task<IActionResult> GetAsync(int idRole, CancellationToken token = default)
|
||||
{
|
||||
|
||||
var result = await userRoleService.GetAsync(idRole, token)
|
||||
.ConfigureAwait(false);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Добавить запись
|
||||
/// </summary>
|
||||
/// <param name="dto">Объект с параметрами добавляемой роли</param>
|
||||
/// <param name="token">Токен отмены задачи</param>
|
||||
/// <returns>1 - добавлено, 0 - нет</returns>
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
||||
public async Task<IActionResult> Insert([FromBody] UserRoleDto dto,
|
||||
CancellationToken token = default)
|
||||
{
|
||||
var result = await userRoleService.InsertAsync(dto, token)
|
||||
.ConfigureAwait(false);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Редактировать запись по id
|
||||
/// </summary>
|
||||
/// <param name="dto">Объект с параметрами добавляемой роли</param>
|
||||
/// <param name="permissionIds">Id добавляемых к роли разрешений</param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns>1 - успешно отредактировано, 0 - нет</returns>
|
||||
[HttpPut("{id}")]
|
||||
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
||||
public async Task<IActionResult> Update([FromBody] UserRoleDto dto,
|
||||
CancellationToken token = default)
|
||||
{
|
||||
|
||||
await userRoleService.UpdateAsync(dto, token).ConfigureAwait(false);
|
||||
return Ok();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Удаляет роли по указанным id
|
||||
/// </summary>
|
||||
/// <param name="ids">Список id ролей для удаления</param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
[HttpDelete]
|
||||
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
||||
public async Task<IActionResult> Update(IEnumerable<int> ids, CancellationToken token = default)
|
||||
{
|
||||
|
||||
var result = await userRoleService.DeleteAsync(ids, token).ConfigureAwait(false);
|
||||
return Ok(result);
|
||||
}
|
||||
public AdminUserRoleController(ICrudService<UserRoleDto> service)
|
||||
: base(service)
|
||||
{ }
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user