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> 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(); return dto; } public Task InsertRangeAsync(IEnumerable dtos, CancellationToken token) { var entities = dtos.Adapt(); db.Permissions.AddRange(entities); return db.SaveChangesAsync(token); } public Task UpdateAsync(int id, PermissionDto dto, CancellationToken token) { var entity = dto.Adapt(); db.Permissions.Update(entity); return db.SaveChangesAsync(token); } public Task 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); } } }