Repository.cs 3.0 KB

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