Spaces:
Running
Running
| using FlowAPI.Application.Interfaces; | |
| using FlowAPI.Infrastructure.Data; | |
| using Microsoft.EntityFrameworkCore; | |
| namespace FlowAPI.Infrastructure.Repositories | |
| { | |
| public class GenericRepository<T> : IGenericRepository<T> where T : class | |
| { | |
| protected readonly AppDbContext _context; | |
| protected readonly DbSet<T> _dbSet; | |
| public GenericRepository(AppDbContext context) | |
| { | |
| _context = context; | |
| _dbSet = context.Set<T>(); | |
| } | |
| public virtual async Task<IEnumerable<T>> GetAllAsync() | |
| { | |
| return await _dbSet.AsNoTracking().ToListAsync(); | |
| } | |
| public virtual async Task<T?> GetByIdAsync(Guid id) | |
| { | |
| return await _dbSet.FindAsync(id); | |
| } | |
| public virtual async Task<T> CreateAsync(T entity) | |
| { | |
| _dbSet.Add(entity); | |
| await _context.SaveChangesAsync(); | |
| return entity; | |
| } | |
| public virtual async Task<T> UpdateAsync(T entity) | |
| { | |
| _dbSet.Update(entity); | |
| await _context.SaveChangesAsync(); | |
| return entity; | |
| } | |
| public virtual async Task<bool> DeleteAsync(Guid id) | |
| { | |
| var entity = await _dbSet.FindAsync(id); | |
| if (entity is null) return false; | |
| _dbSet.Remove(entity); | |
| await _context.SaveChangesAsync(); | |
| return true; | |
| } | |
| } | |
| } | |