UserRoleRepository.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using Microsoft.AspNetCore.Identity;
  2. using Microsoft.EntityFrameworkCore;
  3. using MTWorkHR.Core.Entities.Base;
  4. using MTWorkHR.Core.IDto;
  5. using MTWorkHR.Core.IRepositories;
  6. using MTWorkHR.Identity.DBContext;
  7. namespace MTWorkHR.Infrastructure.Repositories
  8. {
  9. public class UserRoleRepository : IUserRoleRepository<IdentityUserRole<string>>
  10. {
  11. protected readonly HRIdentityDBContext context;
  12. private readonly DbSet<IdentityUserRole<string>> dbSet;
  13. public UserRoleRepository(HRIdentityDBContext _econtext)
  14. {
  15. context = _econtext;
  16. dbSet = context.Set<IdentityUserRole<string>>();
  17. }
  18. public virtual async Task<IdentityUserRole<string>> AddAsync(IdentityUserRole<string> entity)
  19. {
  20. await dbSet.AddAsync(entity);
  21. return entity;
  22. }
  23. public virtual async Task<IList<IdentityUserRole<string>>> AddRangeAsync(IList<IdentityUserRole<string>> entity)
  24. {
  25. await dbSet.AddRangeAsync(entity);
  26. return entity;
  27. }
  28. public virtual async Task DeleteAsync(IdentityUserRole<string> entity)
  29. {
  30. dbSet.Remove(entity);
  31. }
  32. public virtual async Task DeleteAsync(IEnumerable<IdentityUserRole<string>> entities)
  33. {
  34. dbSet.RemoveRange(entities);
  35. }
  36. public virtual async Task<Tuple<ICollection<IdentityUserRole<string>>, int>> GetAllAsync(IPagingInputDto pagingInputDto)
  37. {
  38. var query = dbSet.AsQueryable();
  39. if (pagingInputDto.Filter != null)
  40. {
  41. var props = typeof(IdentityUserRole<string>).GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(FilterAttribute)));
  42. var condition = "";
  43. foreach (var p in props)
  44. {
  45. condition = (condition == "" ? condition : condition + " || ") + p.Name + ".Contains(@0)";
  46. }
  47. // query = query.Where(condition, pagingInputDto.Filter);
  48. }
  49. //var order = query.OrderBy(pagingInputDto.OrderByField + " " + pagingInputDto.OrderType);
  50. var page = query.Skip((pagingInputDto.PageNumber * pagingInputDto.PageSize) - pagingInputDto.PageSize).Take(pagingInputDto.PageSize);
  51. var total = await query.CountAsync();
  52. return new Tuple<ICollection<IdentityUserRole<string>>, int>(await page.ToListAsync(), total);
  53. }
  54. public virtual async Task<IdentityUserRole<string>> GetByIdAsync(long id)
  55. {
  56. return await context.Set<IdentityUserRole<string>>().FindAsync(id);
  57. }
  58. public async Task<string[]> GetUserRoleIdsByUserID(string id)
  59. {
  60. return await dbSet.AsQueryable()
  61. .Where(x => x.UserId == id).Select(x => x.RoleId).ToArrayAsync();
  62. }
  63. public async Task<List<IdentityUserRole<string>>> GetRemovedUserRoleIdsByUserID(string id, string[] rolesIds)
  64. {
  65. return await dbSet.AsQueryable()
  66. .Where(x => x.UserId == id && !rolesIds.Contains(x.RoleId)).ToListAsync();
  67. }
  68. }
  69. }