2021-12-01 17:10:17 +05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-02 12:03:30 +05:00
|
|
|
public async Task<IEnumerable<PermissionDto>> GetallAsync(int idRole, CancellationToken token)
|
2021-12-01 17:10:17 +05:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-12-02 12:03:30 +05:00
|
|
|
public Task<int> InsertRangeAsync(IEnumerable<PermissionDto> dtos, CancellationToken token)
|
2021-12-01 17:10:17 +05:00
|
|
|
{
|
|
|
|
var entities = dtos.Adapt<Permission>();
|
|
|
|
|
|
|
|
db.Permissions.AddRange(entities);
|
|
|
|
return db.SaveChangesAsync(token);
|
|
|
|
}
|
|
|
|
|
2021-12-02 12:03:30 +05:00
|
|
|
public Task<int> UpdateAsync(int id, PermissionDto dto, CancellationToken token)
|
2021-12-01 17:10:17 +05:00
|
|
|
{
|
|
|
|
var entity = dto.Adapt<Permission>();
|
|
|
|
db.Permissions.Update(entity);
|
|
|
|
return db.SaveChangesAsync(token);
|
|
|
|
}
|
|
|
|
|
2021-12-02 12:03:30 +05:00
|
|
|
public Task<int> DeleteAsync(int idRole, int idPermission, CancellationToken token)
|
2021-12-01 17:10:17 +05:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|