using ClipForge.Core.Entities; using ClipForge.Core.Interfaces; using ClipForge.Infrastructure.Data; using Microsoft.EntityFrameworkCore; namespace ClipForge.Infrastructure.Repositories; public class BaseRepository : IRepository where T : BaseEntity { protected readonly AppDbContext _context; protected readonly DbSet _dbSet; public BaseRepository(AppDbContext context) { _context = context; _dbSet = context.Set(); } public async Task GetByIdAsync(Guid id, CancellationToken ct = default) => await _dbSet.FindAsync([id], ct); public async Task> GetAllAsync(CancellationToken ct = default) => await _dbSet.OrderByDescending(e => e.CreatedAt).ToListAsync(ct); public async Task AddAsync(T entity, CancellationToken ct = default) { await _dbSet.AddAsync(entity, ct); await _context.SaveChangesAsync(ct); return entity; } public async Task UpdateAsync(T entity, CancellationToken ct = default) { _dbSet.Update(entity); await _context.SaveChangesAsync(ct); } public async Task DeleteAsync(Guid id, CancellationToken ct = default) { var entity = await GetByIdAsync(id, ct); if (entity is not null) { _dbSet.Remove(entity); await _context.SaveChangesAsync(ct); } } }