using ChapterX.Domain.Repositories; using ChapterX.Domain.Shared; using ChapterX.Infrastructure.Data.DataContext; using Microsoft.EntityFrameworkCore; namespace ChapterX.Infrastructure.Repositories { // Generic repository for aggregate roots/entities implementing IEntity. public class GenericRepository : IRepository where T : class, IEntity { protected readonly ApplicationDbContext _context; protected readonly DbSet _dbSet; public GenericRepository(ApplicationDbContext context) { _context = context; _dbSet = context.Set(); } public async Task GetByIdAsync(int id, CancellationToken cancellationToken = default) => await _dbSet.FindAsync([id], cancellationToken); public async Task> GetAllAsync(CancellationToken cancellationToken = default) => await _dbSet.ToListAsync(cancellationToken); public async Task AddAsync(T entity, CancellationToken cancellationToken = default) { await _dbSet.AddAsync(entity, cancellationToken); await _context.SaveChangesAsync(cancellationToken); } public async Task UpdateAsync(T entity, CancellationToken cancellationToken = default) { _dbSet.Update(entity); await _context.SaveChangesAsync(cancellationToken); } public async Task DeleteAsync(T entity, CancellationToken cancellationToken = default) { _dbSet.Remove(entity); await _context.SaveChangesAsync(cancellationToken); } } }