using System.Linq.Expressions; using FindAndDrive.Domain.Interfaces.Repositories; using Microsoft.EntityFrameworkCore; namespace FindAndDrive.Persistence.Repository; public class GenericRepository : IGenericRepository where T : class { private readonly DefaultContext _context; protected GenericRepository(DefaultContext context) { _context = context; } public async Task Create(T entity) { _context.Set().Add(entity); await _context.SaveChangesAsync(); return entity; } public async Task> CreateRange(IEnumerable entities) { _context.Set().AddRange(entities); await _context.SaveChangesAsync(); return entities; } public IEnumerable Find(Expression> expression) { return _context.Set().Where(expression); } public async Task FindFirst(Expression> expression) { return await _context.Set().FirstAsync(expression); } public async Task Exists(Expression> expression) { return await _context.Set().AnyAsync(expression); } public async Task Update() { await _context.SaveChangesAsync(); } public async Task Update(T entity) { _context.Set().Attach(entity); await _context.SaveChangesAsync(); } public async Task Remove(T entity) { _context.Set().Remove(entity); await _context.SaveChangesAsync(); } public async Task RemoveRange(IEnumerable entities) { _context.Set().RemoveRange(entities); await _context.SaveChangesAsync(); } }