using NG_2023_Kanban.DataLayer.DbStartup; using NG_2023_Kanban.DataLayer.Entities; using NG_2023_Kanban.DataLayer.Interfaces; using Microsoft.EntityFrameworkCore; using System.Reflection; namespace NG_2023_Kanban.DataLayer.Repositories; public class BaseRepository : IRepository where T : BaseEntity { private DatabaseContext _context; public BaseRepository(DatabaseContext context) => _context = context; public async Task> GetAllAsync() => await _context.Set().Select(x => x).ToListAsync(); public async Task GetAsync(int id) => await _context.Set().FirstAsync(x => x.Id == id); public async Task> FindAsync(Func predicate) { var entities = await GetAllAsync(); return entities.Where(predicate).ToList(); } public async Task CreateAsync(T entity) { await _context.Set().AddAsync(entity); await _context.SaveChangesAsync(); } public async Task UpdateAsync(int id, T entity) { var updated = await GetAsync(id); foreach (PropertyInfo propertyInfo in entity.GetType().GetProperties()) { string name = propertyInfo.Name; var value = propertyInfo.GetValue(entity); if (value != null) { PropertyInfo setProperty = updated.GetType().GetProperty(name); setProperty.SetValue(updated, value); } } //_context.Set().Update(entity); await _context.SaveChangesAsync(); } public async Task DeleteAsync(int id) { var entity = await GetAsync(id); await DeleteAsync(entity); } public async Task DeleteAsync(T entity) { _context.Set().Remove(entity); await _context.SaveChangesAsync(); } }