< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Repositories.Database.BaseDbRepository<T>
Assembly: NLightning.Infrastructure.Repositories
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Repositories/Database/BaseDbRepository.cs
Tag: 36_15743069263
Line coverage
0%
Covered lines: 0
Uncovered lines: 67
Coverable lines: 67
Total lines: 119
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 32
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
Get(...)0%110100%
Get(...)0%110100%
GetByIdAsync()0%4260%
GetByIdAsync()0%4260%
Insert(...)100%210%
Insert(...)100%210%
Delete(...)0%620%
Delete(...)0%620%
DeleteByIdAsync()0%620%
DeleteByIdAsync()0%620%
DeleteRange(...)0%7280%
DeleteRange(...)0%7280%
DeleteWhere(...)0%620%
DeleteWhere(...)0%620%
Update(...)0%620%
Update(...)0%620%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Repositories/Database/BaseDbRepository.cs

#LineLine coverage
 1using System.Linq.Expressions;
 2using Microsoft.EntityFrameworkCore;
 3
 4namespace NLightning.Infrastructure.Repositories.Database;
 5
 6using Helpers;
 7using Persistence.Contexts;
 8
 9public class BaseDbRepository<TEntity> where TEntity : class
 10{
 11    private readonly NLightningDbContext _context;
 12    protected readonly DbSet<TEntity> DbSet;
 13
 014    protected BaseDbRepository(NLightningDbContext context)
 015    {
 016        ArgumentNullException.ThrowIfNull(context);
 17
 018        _context = context;
 019        DbSet = context.Set<TEntity>();
 020    }
 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)
 026    {
 027        var query = asNoTracking ? DbSet.AsNoTracking() : DbSet;
 28
 029        if (predicate is not null)
 030            query = query.Where(predicate);
 31
 032        if (include is not null)
 033            query = query.Include(include);
 34
 035        if (perPage > 0)
 036            query = query.Skip((pageNumber - 1) * perPage).Take(perPage);
 37
 038        return orderBy is not null ? orderBy(query) : query;
 039    }
 40
 41    protected async Task<TEntity?> GetByIdAsync(object id, bool asNoTracking = true,
 42                                                Expression<Func<TEntity, object>>? include = null)
 043    {
 044        var query = asNoTracking ? DbSet.AsNoTracking() : DbSet;
 45
 046        if (include is not null)
 047            query = query.Include(include);
 48
 049        var lambdaPredicate = PrimaryKeyHelper.GetPrimaryKeyExpression<TEntity>(id, _context)
 050                           ?? throw new InvalidOperationException(
 051                                  $"Entity {typeof(TEntity).Name} does not have a primary key defined.");
 52
 053        query = query.Where(lambdaPredicate);
 54
 055        return await query.FirstOrDefaultAsync();
 056    }
 57
 58    protected void Insert(TEntity entity)
 059    {
 060        DbSet.Add(entity);
 061    }
 62
 63    protected void Delete(TEntity entityToDelete)
 064    {
 065        if (_context.Entry(entityToDelete).State == EntityState.Detached)
 066            DbSet.Attach(entityToDelete);
 67
 068        DbSet.Remove(entityToDelete);
 069    }
 70
 71    protected async Task DeleteByIdAsync(object id)
 072    {
 073        var entityToDelete = await GetByIdAsync(id, false)
 074                          ?? throw new InvalidOperationException($"Entity with id {id} not found.");
 75
 076        Delete(entityToDelete);
 077    }
 78
 79    protected void DeleteRange(IEnumerable<TEntity> entitiesToDelete)
 080    {
 081        var iEnumerable = entitiesToDelete as TEntity[] ?? entitiesToDelete.ToArray();
 082        if (iEnumerable.Length == 0)
 083            return;
 84
 085        foreach (var entity in iEnumerable)
 086        {
 087            if (_context.Entry(entity).State == EntityState.Detached)
 088                DbSet.Attach(entity);
 089        }
 90
 091        DbSet.RemoveRange(iEnumerable);
 092    }
 93
 94    protected void DeleteWhere(Expression<Func<TEntity, bool>> predicate)
 095    {
 096        var entitiesToDelete = DbSet.Where(predicate).ToArray();
 097        if (entitiesToDelete.Length == 0)
 098            return;
 99
 0100        DeleteRange(entitiesToDelete);
 0101    }
 102
 103    protected void Update(TEntity entityToUpdate)
 0104    {
 105        // Get the current state of the entity
 0106        var trackedEntity = DbSet.Local.FirstOrDefault(e => e.Equals(entityToUpdate));
 0107        if (trackedEntity is not null)
 0108        {
 109            // If the entity is already tracked, update its state
 0110            var entry = DbSet.Entry(trackedEntity);
 0111            entry.CurrentValues.SetValues(entityToUpdate);
 0112        }
 113        else
 0114        {
 115            // If the entity is not tracked, attach it and set its state to modified
 0116            DbSet.Update(entityToUpdate);
 0117        }
 0118    }
 119}