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

54 lines
No EOL
1.5 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 QuestionRepository : IQuestionRepository
{
private readonly SurveyDbContext _context;
public QuestionRepository(SurveyDbContext context)
{
_context = context;
}
public async Task<QuestionBase?> GetByIdAsync(int id)
{
return await _context.Questions.FindAsync(id);
}
public async Task<QuestionBase?> GetByIdAsNoTrackingAsync(int id)
{
return await _context.Questions.AsNoTracking().FirstOrDefaultAsync(x => x.Id == id);
}
public async Task<IEnumerable<QuestionBase>> GetAllAsync()
{
return await _context.Questions.ToListAsync();
}
public async Task AddAsync(QuestionBase entity)
{
await _context.Questions.AddAsync(entity);
await _context.SaveChangesAsync();
}
public async Task UpdateAsync(QuestionBase entity)
{
_context.Questions.Update(entity);
await _context.SaveChangesAsync();
}
public async Task DeleteAsync(int id)
{
await _context.Questions.Where(q => q.Id == id).ExecuteDeleteAsync();
await _context.SaveChangesAsync();
}
public async Task<IEnumerable<QuestionBase>> GetQuestionsBySurveyId(int surveyId)
{
return await _context.Questions.Include(q => q.AnswerVariants).Where(q => q.SurveyId == surveyId).ToListAsync();
}
}