UserRoleRepository.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.Infrastructure.DBContext;
  7. namespace MTWorkHR.Infrastructure.Repositories
  8. {
  9. public class UserRoleRepository : IUserRoleRepository<IdentityUserRole<string>>
  10. {
  11. protected readonly HRDataContext context;
  12. private readonly DbSet<IdentityUserRole<string>> dbSet;
  13. public UserRoleRepository(HRDataContext _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. try
  26. {
  27. await dbSet.AddRangeAsync(entity);
  28. }catch (Exception x)
  29. {
  30. var ss = x.Message;
  31. }
  32. return entity;
  33. }
  34. public virtual async Task DeleteAsync(IdentityUserRole<string> entity)
  35. {
  36. dbSet.Remove(entity);
  37. }
  38. public virtual async Task DeleteAsync(IEnumerable<IdentityUserRole<string>> entities)
  39. {
  40. dbSet.RemoveRange(entities);
  41. }
  42. public virtual async Task<Tuple<ICollection<IdentityUserRole<string>>, int>> GetAllAsync(IPagingInputDto pagingInputDto)
  43. {
  44. var query = dbSet.AsQueryable();
  45. if (pagingInputDto.Filter != null)
  46. {
  47. var props = typeof(IdentityUserRole<string>).GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(FilterAttribute)));
  48. var condition = "";
  49. foreach (var p in props)
  50. {
  51. condition = (condition == "" ? condition : condition + " || ") + p.Name + ".Contains(@0)";
  52. }
  53. // query = query.Where(condition, pagingInputDto.Filter);
  54. }
  55. //var order = query.OrderBy(pagingInputDto.OrderByField + " " + pagingInputDto.OrderType);
  56. var page = query.Skip((pagingInputDto.PageNumber * pagingInputDto.PageSize) - pagingInputDto.PageSize).Take(pagingInputDto.PageSize);
  57. var total = await query.CountAsync();
  58. return new Tuple<ICollection<IdentityUserRole<string>>, int>(await page.ToListAsync(), total);
  59. }
  60. public virtual async Task<IdentityUserRole<string>> GetByIdAsync(long id)
  61. {
  62. return await context.Set<IdentityUserRole<string>>().FindAsync(id);
  63. }
  64. public async Task<string[]> GetUserRoleIdsByUserID(string id)
  65. {
  66. return await dbSet.AsQueryable()
  67. .Where(x => x.UserId == id).Select(x => x.RoleId).ToArrayAsync();
  68. }
  69. public async Task<List<IdentityUserRole<string>>> GetRemovedUserRoleIdsByUserID(string id, string[] rolesIds)
  70. {
  71. return await dbSet.AsQueryable()
  72. .Where(x => x.UserId == id && !rolesIds.Contains(x.RoleId)).ToListAsync();
  73. }
  74. }
  75. }