surveylib/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs
2025-04-16 17:57:03 +05:00

49 lines
No EOL
1.3 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<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(QuestionBase entity)
{
_context.Questions.Remove(entity);
await _context.SaveChangesAsync();
}
public IEnumerable<QuestionBase> GetQuestionsBySurveyId(int surveyId)
{
return _context.Questions.Where(q => q.SurveyId == surveyId);
}
}