|
| 1 | +using Microsoft.EntityFrameworkCore.ChangeTracking; |
| 2 | +using System.Linq; |
| 3 | +using System.Linq.Expressions; |
| 4 | + |
| 5 | +namespace MyFinance.Repository; |
| 6 | + |
| 7 | +public class Repo<TModel> : IRepo<TModel> where TModel : BaseModel |
| 8 | +{ |
| 9 | + private readonly MyFinanceContext _context; |
| 10 | + |
| 11 | + public Repo(MyFinanceContext context) |
| 12 | + { |
| 13 | + _context = context; |
| 14 | + } |
| 15 | + |
| 16 | + public DbSet<TModel> Table => _context.Set<TModel>(); |
| 17 | + |
| 18 | + public async Task<List<TModel>> GetAllAsync(Expression<Func<TModel, bool>> expression = null, Expression<Func<TModel, object>> include = null, Expression<Func<TModel, object>> ordered = null, int? skip = null, int? limit = null) |
| 19 | + { |
| 20 | + var query = Table.AsNoTracking(); |
| 21 | + |
| 22 | + if (expression != null) |
| 23 | + query = query.Where(expression); |
| 24 | + |
| 25 | + if (include != null) |
| 26 | + query = query.Include(include); |
| 27 | + |
| 28 | + if (ordered != null) |
| 29 | + query = query.OrderBy(ordered); |
| 30 | + |
| 31 | + if (skip.HasValue) |
| 32 | + query = query.Skip(skip.Value); |
| 33 | + |
| 34 | + if (limit.HasValue) |
| 35 | + query = query.Take(limit.Value); |
| 36 | + |
| 37 | + return await query.ToListAsync(); |
| 38 | + } |
| 39 | + |
| 40 | + public async Task<int> GetCountAsync(Expression<Func<TModel, bool>> filter = null) |
| 41 | + { |
| 42 | + var query = Table.AsNoTracking(); |
| 43 | + |
| 44 | + if (filter != null) |
| 45 | + query = query.Where(filter); |
| 46 | + |
| 47 | + return await query.CountAsync(); |
| 48 | + } |
| 49 | + |
| 50 | + public async Task<TModel?> GetSingleAsync(Expression<Func<TModel, bool>> expression, Expression<Func<TModel, object>> include = null) |
| 51 | + { |
| 52 | + var query = Table.AsNoTracking().Where(expression); |
| 53 | + |
| 54 | + if (include != null) |
| 55 | + query = query.Include(include); |
| 56 | + |
| 57 | + return await query.FirstOrDefaultAsync(); |
| 58 | + } |
| 59 | + |
| 60 | + public async Task<bool> InsertAsync(TModel item) |
| 61 | + { |
| 62 | + try |
| 63 | + { |
| 64 | + EntityEntry<TModel> entityEntry = await Table.AddAsync(item); |
| 65 | + await SaveAsync(); |
| 66 | + |
| 67 | + return true; |
| 68 | + } |
| 69 | + catch (Exception) |
| 70 | + { |
| 71 | + return false; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public async Task<bool> RemoveAsync(Guid id) |
| 76 | + { |
| 77 | + try |
| 78 | + { |
| 79 | + var model = await Table.FirstOrDefaultAsync(x => x.Id == id); |
| 80 | + Table.Remove(model); |
| 81 | + await SaveAsync(); |
| 82 | + |
| 83 | + return true; |
| 84 | + } |
| 85 | + catch (Exception) |
| 86 | + { |
| 87 | + return false; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public async Task<bool> RemoveRangeAsync(List<Guid> ids) |
| 92 | + { |
| 93 | + try |
| 94 | + { |
| 95 | + var models = await Table.AsNoTracking().Where(x => ids.Contains(x.Id)).ToListAsync(); |
| 96 | + Table.RemoveRange(models); |
| 97 | + await SaveAsync(); |
| 98 | + |
| 99 | + return true; |
| 100 | + } |
| 101 | + catch (Exception) |
| 102 | + { |
| 103 | + return false; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public async Task<int> SaveAsync() => await _context.SaveChangesAsync(); |
| 108 | + |
| 109 | + public async Task<bool> UpdateAsync(TModel item) |
| 110 | + { |
| 111 | + try |
| 112 | + { |
| 113 | + EntityEntry<TModel> entityEntry = Table.Update(item); |
| 114 | + await SaveAsync(); |
| 115 | + |
| 116 | + return true; |
| 117 | + } |
| 118 | + catch (Exception) |
| 119 | + { |
| 120 | + return false; |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments