using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using MTWorkHR.Core.Entities.Base; using MTWorkHR.Core.IDto; using MTWorkHR.Core.IRepositories; using MTWorkHR.Identity.DBContext; namespace MTWorkHR.Infrastructure.Repositories { public class UserRoleRepository : IUserRoleRepository> { protected readonly HRIdentityDBContext context; private readonly DbSet> dbSet; public UserRoleRepository(HRIdentityDBContext _econtext) { context = _econtext; dbSet = context.Set>(); } public virtual async Task> AddAsync(IdentityUserRole entity) { await dbSet.AddAsync(entity); return entity; } public virtual async Task>> AddRangeAsync(IList> entity) { await dbSet.AddRangeAsync(entity); return entity; } public virtual async Task DeleteAsync(IdentityUserRole entity) { dbSet.Remove(entity); } public virtual async Task DeleteAsync(IEnumerable> entities) { dbSet.RemoveRange(entities); } public virtual async Task>, int>> GetAllAsync(IPagingInputDto pagingInputDto) { var query = dbSet.AsQueryable(); if (pagingInputDto.Filter != null) { var props = typeof(IdentityUserRole).GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(FilterAttribute))); var condition = ""; foreach (var p in props) { condition = (condition == "" ? condition : condition + " || ") + p.Name + ".Contains(@0)"; } // query = query.Where(condition, pagingInputDto.Filter); } //var order = query.OrderBy(pagingInputDto.OrderByField + " " + pagingInputDto.OrderType); var page = query.Skip((pagingInputDto.PageNumber * pagingInputDto.PageSize) - pagingInputDto.PageSize).Take(pagingInputDto.PageSize); var total = await query.CountAsync(); return new Tuple>, int>(await page.ToListAsync(), total); } public virtual async Task> GetByIdAsync(long id) { return await context.Set>().FindAsync(id); } public async Task GetUserRoleIdsByUserID(string id) { return await dbSet.AsQueryable() .Where(x => x.UserId == id).Select(x => x.RoleId).ToArrayAsync(); } public async Task>> GetRemovedUserRoleIdsByUserID(string id, string[] rolesIds) { return await dbSet.AsQueryable() .Where(x => x.UserId == id && !rolesIds.Contains(x.RoleId)).ToListAsync(); } } }