surveylib/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs
2025-05-31 00:50:39 +05:00

54 lines
No EOL
1.4 KiB
C#

using Microsoft.EntityFrameworkCore;
using SurveyLib.Core.Models;
using SurveyLib.Core.Repositories;
using SurveyLib.Infrastructure.EFCore.Data;
namespace SurveyLib.Infrastructure.EFCore.Repositories;
public class SurveyRepository : ISurveyRepository
{
private readonly SurveyDbContext _context;
public SurveyRepository(SurveyDbContext context)
{
_context = context;
}
public async Task<Survey?> GetByIdAsync(int id)
{
return await _context.Surveys.FindAsync(id);
}
public async Task<Survey?> GetByIdAsNoTrackingAsync(int id)
{
return await _context.Surveys.AsNoTracking().FirstOrDefaultAsync(c => c.Id == id);
}
public async Task<IEnumerable<Survey>> GetAllAsync()
{
return await _context.Surveys.ToListAsync();
}
public async Task AddAsync(Survey entity)
{
await _context.Surveys.AddAsync(entity);
await _context.SaveChangesAsync();
}
public async Task UpdateAsync(Survey entity)
{
_context.Surveys.Update(entity);
await _context.SaveChangesAsync();
}
public async Task DeleteAsync(int id)
{
await _context.Surveys.Where(s => s.Id == id).ExecuteDeleteAsync();
await _context.SaveChangesAsync();
}
public async Task<IEnumerable<Survey>> GetSurveysByUserIdAsync(int userId)
{
return await _context.Surveys.Where(s => s.CreatedBy == userId).ToListAsync();
}
}