61 lines
No EOL
1.7 KiB
C#
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();
|
|
}
|
|
} |