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

61 lines
No EOL
1.7 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 DataContext _context;
public SurveyRepository(DataContext context)
{
_context = context;
}
public async Task<Survey?> GetByIdAsync(int id)
{
return await _context.Surveys.FirstOrDefaultAsync(s => s.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(Survey entity)
{
_context.Surveys.Remove(entity);
await _context.SaveChangesAsync();
}
public async Task<Survey?> GetWithQuestionsAsync(int surveyId)
{
return await _context.Surveys.Include(survey => survey.Questions)
.FirstOrDefaultAsync(s => s.Id == surveyId);
}
public async Task<Survey?> GetWithCompletionsAsync(int surveyId)
{
return await _context.Surveys.Include(survey => survey.Completions)
.FirstOrDefaultAsync(s => s.Id == surveyId);
}
public async Task<IEnumerable<Survey>> FindByTitleAsync(string title)
{
return await _context.Surveys.Where(s => s.Title.ToLower().Contains(title.ToLower())).ToListAsync();
}
}