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<IDrillParamsService, DrillParamsService>();
|
||||||
services.AddTransient<IDrillFlowChartService, DrillFlowChartService>();
|
services.AddTransient<IDrillFlowChartService, DrillFlowChartService>();
|
||||||
services.AddTransient<ITimeZoneService, TimeZoneService>();
|
services.AddTransient<ITimeZoneService, TimeZoneService>();
|
||||||
services.AddTransient<IUserRoleService, UserRoleService>();
|
|
||||||
|
|
||||||
// admin crud services:
|
// admin crud services:
|
||||||
services.AddTransient<ICrudService<DepositDto>, CrudServiceBase<DepositDto, Deposit>>();
|
services.AddTransient<ICrudService<DepositDto>, CrudServiceBase<DepositDto, Deposit>>();
|
||||||
@ -56,6 +55,7 @@ namespace AsbCloudInfrastructure
|
|||||||
services.AddTransient<ICrudService<WellDto>, CrudServiceBase<WellDto, Well>>();
|
services.AddTransient<ICrudService<WellDto>, CrudServiceBase<WellDto, Well>>();
|
||||||
services.AddTransient<ICrudService<CompanyDto>, CrudServiceBase<CompanyDto, Company>>();
|
services.AddTransient<ICrudService<CompanyDto>, CrudServiceBase<CompanyDto, Company>>();
|
||||||
services.AddTransient<ICrudService<UserDto>, CrudServiceBase<UserDto, User>>();
|
services.AddTransient<ICrudService<UserDto>, CrudServiceBase<UserDto, User>>();
|
||||||
|
services.AddTransient<ICrudService<UserRoleDto>, CrudServiceBase<UserRoleDto, UserRole>>();
|
||||||
services.AddTransient<ICrudService<TelemetryDto>, CrudServiceBase<TelemetryDto, Telemetry>>();
|
services.AddTransient<ICrudService<TelemetryDto>, CrudServiceBase<TelemetryDto, Telemetry>>();
|
||||||
services.AddTransient<ICrudService<DrillParamsDto>, DrillParamsService>();
|
services.AddTransient<ICrudService<DrillParamsDto>, DrillParamsService>();
|
||||||
|
|
||||||
|
@ -81,12 +81,13 @@ namespace AsbCloudInfrastructure.Services
|
|||||||
return dto;
|
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);
|
var entity = Convert(item);
|
||||||
entity.Id = 0;
|
entity.Id = 0;
|
||||||
dbSet.Add(entity);
|
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)
|
public virtual Task<int> InsertRangeAsync(IEnumerable<TDto> items, CancellationToken token = default)
|
||||||
|
@ -3,95 +3,79 @@ using System.Linq;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using AsbCloudApp.Data;
|
using AsbCloudApp.Data;
|
||||||
using AsbCloudApp.Services;
|
|
||||||
using AsbCloudDb.Model;
|
using AsbCloudDb.Model;
|
||||||
using AsbCloudInfrastructure.Services.Cache;
|
using AsbCloudInfrastructure.Services.Cache;
|
||||||
using Mapster;
|
using Mapster;
|
||||||
|
|
||||||
namespace AsbCloudInfrastructure.Services
|
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<UserRole> cacheUserRoles;
|
||||||
private readonly CacheTable<Permission> cachePermissions;
|
private readonly CacheTable<Permission> cachePermissions;
|
||||||
private readonly CacheTable<RelationUserRolePermission> cacheUserRolesPermissions;
|
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)context);
|
||||||
cacheUserRoles = cacheDb.GetCachedTable<UserRole>((AsbCloudDbContext)db);
|
cachePermissions = cacheDb.GetCachedTable<Permission>((AsbCloudDbContext)context);
|
||||||
cachePermissions = cacheDb.GetCachedTable<Permission>((AsbCloudDbContext)db);
|
|
||||||
cacheUserRolesPermissions =
|
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))
|
var rolesDtos = await base.GetAllAsync(token);
|
||||||
.Adapt<UserRoleDto>();
|
|
||||||
|
|
||||||
return rolesDtos.Select(FillUserRoleWithPermissions);
|
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)
|
var roleDto = await base.GetAsync(id,token);
|
||||||
.ConfigureAwait(false)).Adapt<UserRoleDto>();
|
|
||||||
|
|
||||||
return roleDto is null ? null : FillUserRoleWithPermissions(roleDto);
|
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>();
|
var newRoleId = await base.InsertAsync(dto, token);
|
||||||
db.UserRoles.Add(newRole);
|
|
||||||
var result = await db.SaveChangesAsync(token);
|
|
||||||
|
|
||||||
if (dto.PermissionIds == default)
|
if (dto.PermissionIds == default)
|
||||||
return result;
|
return newRoleId;
|
||||||
|
|
||||||
foreach (var pId in dto.PermissionIds)
|
foreach (var pId in dto.PermissionIds)
|
||||||
{
|
{
|
||||||
var relation = new RelationUserRolePermission()
|
var relation = new RelationUserRolePermission()
|
||||||
{
|
{
|
||||||
IdUserRole = newRole.Id,
|
IdUserRole = newRoleId,
|
||||||
IdPermission = pId
|
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>();
|
var result = await base.UpdateAsync(id, item, token);
|
||||||
db.UserRoles.Update(entity);
|
|
||||||
await db.SaveChangesAsync(token);
|
|
||||||
|
|
||||||
if (dto.PermissionIds != default)
|
if (item.PermissionIds == default)
|
||||||
{
|
return result;
|
||||||
await cacheUserRolesPermissions.RemoveAsync(r => r.IdUserRole == dto.Id, token)
|
|
||||||
|
await cacheUserRolesPermissions.RemoveAsync(r => r.IdUserRole == item.Id, token)
|
||||||
.ConfigureAwait(false);
|
.ConfigureAwait(false);
|
||||||
|
|
||||||
var newRelations = dto.PermissionIds.Select(p => new RelationUserRolePermission()
|
var newRelations = item.PermissionIds.Select(p => new RelationUserRolePermission()
|
||||||
{
|
{
|
||||||
IdUserRole = dto.Id,
|
IdUserRole = item.Id,
|
||||||
IdPermission = p
|
IdPermission = p
|
||||||
});
|
});
|
||||||
await cacheUserRolesPermissions.InsertAsync(newRelations, token);
|
await cacheUserRolesPermissions.InsertAsync(newRelations, token);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token)
|
return result;
|
||||||
{
|
|
||||||
var entities = cacheUserRoles.Where(e => ids.Contains(e.Id));
|
|
||||||
if (entities == default)
|
|
||||||
return 0;
|
|
||||||
db.UserRoles.RemoveRange(entities);
|
|
||||||
return await db.SaveChangesAsync(token);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private UserRoleDto FillUserRoleWithPermissions(UserRoleDto roleDto)
|
private UserRoleDto FillUserRoleWithPermissions(UserRoleDto roleDto)
|
||||||
|
@ -1,7 +1,4 @@
|
|||||||
using System.Collections.Generic;
|
using AsbCloudApp.Data;
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using AsbCloudApp.Data;
|
|
||||||
using AsbCloudApp.Services;
|
using AsbCloudApp.Services;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
@ -11,93 +8,10 @@ namespace AsbCloudWebApi.Controllers
|
|||||||
[Route("api/admin/user/role")]
|
[Route("api/admin/user/role")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public class AdminUserRoleController : ControllerBase
|
public class AdminUserRoleController : CrudController<UserRoleDto, ICrudService<UserRoleDto>>
|
||||||
{
|
{
|
||||||
private readonly IUserRoleService userRoleService;
|
public AdminUserRoleController(ICrudService<UserRoleDto> service)
|
||||||
|
: base(service)
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user