DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/PermissionService.cs

59 lines
1.9 KiB
C#
Raw Normal View History

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
{
private readonly IAsbCloudDbContext db;
public PermissionService(IAsbCloudDbContext db)
{
this.db = db;
}
public async Task<IEnumerable<PermissionDto>> GetallAsync(int idRole, CancellationToken token)
{
var entities = await (from p in db.Permissions
where p.IdUserRole == idRole
select p)
.ToListAsync(token).ConfigureAwait(false);
var dto = entities.Adapt<PermissionDto>();
return dto;
}
public Task<int> InsertRangeAsync(IEnumerable<PermissionDto> dtos, CancellationToken token)
{
var entities = dtos.Adapt<Permission>();
db.Permissions.AddRange(entities);
return db.SaveChangesAsync(token);
}
public Task<int> UpdateAsync(int id, PermissionDto dto, CancellationToken token)
{
var entity = dto.Adapt<Permission>();
db.Permissions.Update(entity);
return db.SaveChangesAsync(token);
}
public Task<int> DeleteAsync(int idRole, int idPermission, CancellationToken token)
{
var entities = db.Permissions.AsNoTracking()
.Where(e => e.IdUserRole == idRole && e.IdPermission == idPermission)
.ToList();
if (!entities.Any())
return Task.FromResult(0);
db.Permissions.RemoveRange(entities);
return db.SaveChangesAsync(token);
}
}
}