| 5 | namespace EntraRoleReaper.Api.Data; |
| 6 | |
| 7 | public class Repository<TEntity>(ReaperDbContext dbContext) where TEntity : Entity |
| 8 | { |
| 9 | protected DbSet<TEntity> dbSet => dbContext.Set<TEntity>(); |
| 10 | public void Add(TEntity entity) |
| 11 | { |
| 12 | dbSet.Add(entity); |
| 13 | } |
| 14 | |
| 15 | public virtual async Task<IEnumerable<TEntity>> Get(Expression<Func<TEntity, bool>>? filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null, string includeProperties = "") |
| 16 | { |
| 17 | IQueryable<TEntity> query = dbSet; |
| 18 | if (filter != null) |
| 19 | { |
| 20 | query = query.Where(filter); |
| 21 | } |
| 22 | foreach (var includeProperty in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) |
| 23 | { |
| 24 | query = query.Include(includeProperty); |
| 25 | } |
| 26 | if (orderBy != null) |
| 27 | { |
| 28 | return await orderBy(query).ToListAsync(); |
| 29 | } |
| 30 | return await query.ToListAsync(); |
| 31 | } |
| 32 | |
| 33 | public virtual async Task<TEntity?> GetById(Guid id) |
| 34 | { |
| 35 | return await dbSet.FindAsync(id); |
| 36 | } |
| 37 | |
| 38 | public void Delete(Guid id) |
| 39 | { |
| 40 | var entityToDelete = dbSet.Find(id); |
| 41 | if (entityToDelete is null) return; |
| 42 | Delete(entityToDelete); |
| 43 | } |
| 44 | |
| 45 | public virtual void Delete(TEntity entity) |
| 46 | { |
| 47 | if (dbContext.Entry(entity).State == EntityState.Detached) |
| 48 | { |
| 49 | dbSet.Attach(entity); |
| 50 | } |
| 51 | else |
| 52 | { |
| 53 | dbSet.Remove(entity); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | public void Update(TEntity entity) |
| 58 | { |
| 59 | dbSet.Attach(entity); |
| 60 | dbContext.Entry(entity).State = EntityState.Modified; |
| 61 | } |
| 62 | } |
nothing calls this directly
no outgoing calls
no test coverage detected