namespace DocuMed.Repository.Repositories.Base
{
    public class Repository<T> : IRepository<T> where T : class, IApiEntity
    {
        protected readonly ApplicationContext DbContext;

        public Repository(ApplicationContext dbContext)
        {
            DbContext = dbContext;
            Entities = DbContext.Set<T>();
            DbContext.ChangeTracker.Clear();
        }

        public DbSet<T> Entities { get; }

        public virtual IQueryable<T> Table => Entities.Where(e => !e.IsRemoved);

        public virtual IQueryable<T> TableNoTracking => Table.AsNoTracking();

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            DbContext?.Dispose();
        }
    }
}