forked from ddrilling/AsbCloudServer
69 lines
2.1 KiB
C#
69 lines
2.1 KiB
C#
using AsbCloudApp.Data;
|
|
using AsbCloudApp.Services;
|
|
using AsbCloudDb.Model;
|
|
using Mapster;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
{
|
|
public class PermissionService : IPermissionService, IConverter<PermissionDto, Permission>
|
|
{
|
|
private readonly IAsbCloudDbContext db;
|
|
|
|
public PermissionService(IAsbCloudDbContext db)
|
|
{
|
|
this.db = db;
|
|
}
|
|
|
|
public async Task<IEnumerable<PermissionDto>> GetByIdRoleAsync(int idRole, CancellationToken token)
|
|
{
|
|
var entities = await db.Permissions
|
|
.Where(p => p.IdUserRole == idRole)
|
|
.ToListAsync(token)
|
|
.ConfigureAwait(false);
|
|
var dto = entities.Select(Convert);
|
|
return dto;
|
|
}
|
|
|
|
public Task<int> InsertRangeAsync(IEnumerable<PermissionDto> dtos, CancellationToken token)
|
|
{
|
|
var entities = dtos.Select(Convert);
|
|
db.Permissions.AddRange(entities);
|
|
return db.SaveChangesAsync(token);
|
|
}
|
|
|
|
public Task<int> UpdateAsync(PermissionDto dto, CancellationToken token)
|
|
{
|
|
var entity = Convert(dto);
|
|
db.Permissions.Update(entity);
|
|
return db.SaveChangesAsync(token);
|
|
}
|
|
|
|
public Task<int> DeleteAsync(int idUserRole, int idPermission, CancellationToken token)
|
|
{
|
|
var entities = db.Permissions.AsNoTracking()
|
|
.Where(e => e.IdUserRole == idUserRole && e.IdPermission == idPermission)
|
|
.ToList();
|
|
if (!entities.Any())
|
|
return Task.FromResult(0);
|
|
db.Permissions.RemoveRange(entities);
|
|
return db.SaveChangesAsync(token);
|
|
}
|
|
|
|
public Permission Convert(PermissionDto src)
|
|
{
|
|
var entity = src.Adapt<Permission>();
|
|
return entity;
|
|
}
|
|
|
|
public PermissionDto Convert(Permission src)
|
|
{
|
|
var dto = src.Adapt<PermissionDto>();
|
|
return dto;
|
|
}
|
|
}
|
|
} |