using GermanApp.Domain.Entities;
using GermanApp.Domain.Interfaces;
using GermanApp.Infrastructure.Data.DbContext;
using Microsoft.EntityFrameworkCore;
namespace GermanApp.Infrastructure.Data.Repositories;
///
/// Entity Framework Core implementation of IRepository.
/// This is part of the Infrastructure layer.
///
public class UserRepository : IRepository
{
private readonly AppDbContext _context;
public UserRepository(AppDbContext context)
{
_context = context;
}
public async Task GetByIdAsync(int id, CancellationToken cancellationToken = default)
{
return await _context.Users
.Include(u => u.StoryProgress)
.FirstOrDefaultAsync(u => u.Id == id, cancellationToken);
}
public async Task> GetAllAsync(CancellationToken cancellationToken = default)
{
return await _context.Users
.AsNoTracking()
.ToListAsync(cancellationToken);
}
public async Task AddAsync(User entity, CancellationToken cancellationToken = default)
{
await _context.Users.AddAsync(entity, cancellationToken);
await _context.SaveChangesAsync(cancellationToken);
return entity;
}
public async Task UpdateAsync(User entity, CancellationToken cancellationToken = default)
{
_context.Users.Update(entity);
await _context.SaveChangesAsync(cancellationToken);
}
public async Task DeleteAsync(User entity, CancellationToken cancellationToken = default)
{
_context.Users.Remove(entity);
await _context.SaveChangesAsync(cancellationToken);
}
public async Task ExistsAsync(int id, CancellationToken cancellationToken = default)
{
return await _context.Users
.AnyAsync(u => u.Id == id, cancellationToken);
}
}