surveylib/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs
shept 62facb3423 completed QuestionRepository.cs
small readability fixes for SurveyRepository.cs
2025-03-14 23:15:33 +05:00

55 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 DataContext _context;
public QuestionRepository(DataContext context)
{
_context = context;
}
public async Task<QuestionBase?> GetByIdAsync(int id)
{
return await _context.Questions.FirstOrDefaultAsync(q => q.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(QuestionBase entity)
{
_context.Questions.Remove(entity);
await _context.SaveChangesAsync();
}
public async Task<QuestionBase?> GetWithAnswersAsync(int questionId)
{
return await _context.Questions.Include(q => q.Answers)
.FirstOrDefaultAsync(q => q.Id == questionId);
}
public async Task<IEnumerable<QuestionBase>> GetBySurveyIdAsync(int surveyId)
{
return await _context.Questions.Where(q => q.SurveyId == surveyId).ToListAsync();
}
}