RolePermissionRepository.cs 3.1 KB

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