| | 1 | | using System.Linq.Expressions; |
| | 2 | | using Microsoft.EntityFrameworkCore; |
| | 3 | |
|
| | 4 | | namespace NLightning.Infrastructure.Repositories.Database; |
| | 5 | |
|
| | 6 | | using Helpers; |
| | 7 | | using Persistence.Contexts; |
| | 8 | |
|
| | 9 | | public class BaseDbRepository<TEntity> where TEntity : class |
| | 10 | | { |
| | 11 | | private readonly NLightningDbContext _context; |
| | 12 | | protected readonly DbSet<TEntity> DbSet; |
| | 13 | |
|
| 0 | 14 | | protected BaseDbRepository(NLightningDbContext context) |
| 0 | 15 | | { |
| 0 | 16 | | ArgumentNullException.ThrowIfNull(context); |
| | 17 | |
|
| 0 | 18 | | _context = context; |
| 0 | 19 | | DbSet = context.Set<TEntity>(); |
| 0 | 20 | | } |
| | 21 | |
|
| | 22 | | protected IQueryable<TEntity> Get(Expression<Func<TEntity, bool>>? predicate = null, |
| | 23 | | Expression<Func<TEntity, object>>? include = null, |
| | 24 | | Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null, |
| | 25 | | bool asNoTracking = true, int perPage = 0, int pageNumber = 1) |
| 0 | 26 | | { |
| 0 | 27 | | var query = asNoTracking ? DbSet.AsNoTracking() : DbSet; |
| | 28 | |
|
| 0 | 29 | | if (predicate is not null) |
| 0 | 30 | | query = query.Where(predicate); |
| | 31 | |
|
| 0 | 32 | | if (include is not null) |
| 0 | 33 | | query = query.Include(include); |
| | 34 | |
|
| 0 | 35 | | if (perPage > 0) |
| 0 | 36 | | query = query.Skip((pageNumber - 1) * perPage).Take(perPage); |
| | 37 | |
|
| 0 | 38 | | return orderBy is not null ? orderBy(query) : query; |
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | protected async Task<TEntity?> GetByIdAsync(object id, bool asNoTracking = true, |
| | 42 | | Expression<Func<TEntity, object>>? include = null) |
| 0 | 43 | | { |
| 0 | 44 | | var query = asNoTracking ? DbSet.AsNoTracking() : DbSet; |
| | 45 | |
|
| 0 | 46 | | if (include is not null) |
| 0 | 47 | | query = query.Include(include); |
| | 48 | |
|
| 0 | 49 | | var lambdaPredicate = PrimaryKeyHelper.GetPrimaryKeyExpression<TEntity>(id, _context) |
| 0 | 50 | | ?? throw new InvalidOperationException( |
| 0 | 51 | | $"Entity {typeof(TEntity).Name} does not have a primary key defined."); |
| | 52 | |
|
| 0 | 53 | | query = query.Where(lambdaPredicate); |
| | 54 | |
|
| 0 | 55 | | return await query.FirstOrDefaultAsync(); |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | protected void Insert(TEntity entity) |
| 0 | 59 | | { |
| 0 | 60 | | DbSet.Add(entity); |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | protected void Delete(TEntity entityToDelete) |
| 0 | 64 | | { |
| 0 | 65 | | if (_context.Entry(entityToDelete).State == EntityState.Detached) |
| 0 | 66 | | DbSet.Attach(entityToDelete); |
| | 67 | |
|
| 0 | 68 | | DbSet.Remove(entityToDelete); |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | protected async Task DeleteByIdAsync(object id) |
| 0 | 72 | | { |
| 0 | 73 | | var entityToDelete = await GetByIdAsync(id, false) |
| 0 | 74 | | ?? throw new InvalidOperationException($"Entity with id {id} not found."); |
| | 75 | |
|
| 0 | 76 | | Delete(entityToDelete); |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | protected void DeleteRange(IEnumerable<TEntity> entitiesToDelete) |
| 0 | 80 | | { |
| 0 | 81 | | var iEnumerable = entitiesToDelete as TEntity[] ?? entitiesToDelete.ToArray(); |
| 0 | 82 | | if (iEnumerable.Length == 0) |
| 0 | 83 | | return; |
| | 84 | |
|
| 0 | 85 | | foreach (var entity in iEnumerable) |
| 0 | 86 | | { |
| 0 | 87 | | if (_context.Entry(entity).State == EntityState.Detached) |
| 0 | 88 | | DbSet.Attach(entity); |
| 0 | 89 | | } |
| | 90 | |
|
| 0 | 91 | | DbSet.RemoveRange(iEnumerable); |
| 0 | 92 | | } |
| | 93 | |
|
| | 94 | | protected void DeleteWhere(Expression<Func<TEntity, bool>> predicate) |
| 0 | 95 | | { |
| 0 | 96 | | var entitiesToDelete = DbSet.Where(predicate).ToArray(); |
| 0 | 97 | | if (entitiesToDelete.Length == 0) |
| 0 | 98 | | return; |
| | 99 | |
|
| 0 | 100 | | DeleteRange(entitiesToDelete); |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | protected void Update(TEntity entityToUpdate) |
| 0 | 104 | | { |
| | 105 | | // Get the current state of the entity |
| 0 | 106 | | var trackedEntity = DbSet.Local.FirstOrDefault(e => e.Equals(entityToUpdate)); |
| 0 | 107 | | if (trackedEntity is not null) |
| 0 | 108 | | { |
| | 109 | | // If the entity is already tracked, update its state |
| 0 | 110 | | var entry = DbSet.Entry(trackedEntity); |
| 0 | 111 | | entry.CurrentValues.SetValues(entityToUpdate); |
| 0 | 112 | | } |
| | 113 | | else |
| 0 | 114 | | { |
| | 115 | | // If the entity is not tracked, attach it and set its state to modified |
| 0 | 116 | | DbSet.Update(entityToUpdate); |
| 0 | 117 | | } |
| 0 | 118 | | } |
| | 119 | | } |