diff --git a/SurveyLib.Core/Repositories/IGenericRepository.cs b/SurveyLib.Core/Repositories/IGenericRepository.cs index 071416f..b13c749 100644 --- a/SurveyLib.Core/Repositories/IGenericRepository.cs +++ b/SurveyLib.Core/Repositories/IGenericRepository.cs @@ -1,8 +1,10 @@ +using SurveyLib.Core.Models; + namespace SurveyLib.Core.Repositories; public interface IGenericRepository where T : class { - Task? GetByIdAsync(int id); + Task GetByIdAsync(int id); Task> GetAllAsync(); Task AddAsync(T entity); Task UpdateAsync(T entity); diff --git a/SurveyLib.Core/Repositories/ISurveyRepository.cs b/SurveyLib.Core/Repositories/ISurveyRepository.cs index 795c26d..a918cd3 100644 --- a/SurveyLib.Core/Repositories/ISurveyRepository.cs +++ b/SurveyLib.Core/Repositories/ISurveyRepository.cs @@ -6,5 +6,5 @@ public interface ISurveyRepository : IGenericRepository { Task GetWithQuestionsAsync(int surveyId); Task GetWithCompletionsAsync(int surveyId); - Task FindByTitleAsync(string title); + Task> FindByTitleAsync(string title); } \ No newline at end of file diff --git a/SurveyLib.Infrastructure.EFCore/Data/DataContext.cs b/SurveyLib.Infrastructure.EFCore/Data/DataContext.cs new file mode 100644 index 0000000..dcc2e68 --- /dev/null +++ b/SurveyLib.Infrastructure.EFCore/Data/DataContext.cs @@ -0,0 +1,17 @@ +using Microsoft.EntityFrameworkCore; +using SurveyLib.Core.Models; + +namespace SurveyLib.Infrastructure.EFCore.Data; + +public class DataContext : DbContext +{ + public DbSet Surveys { get; set; } + public DbSet Questions { get; set; } + public DbSet Completions { get; set; } + public DbSet Answers { get; set; } + + public DataContext(DbContextOptions options) : base(options) + { + Database.EnsureCreated(); + } +} \ No newline at end of file diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs new file mode 100644 index 0000000..d71e0a7 --- /dev/null +++ b/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs @@ -0,0 +1,54 @@ +using SurveyLib.Core.Models; +using SurveyLib.Core.Repositories; +using Microsoft.EntityFrameworkCore; +using SurveyLib.Infrastructure.EFCore.Data; + +namespace SurveyLib.Infrastructure.EFCore.Repositories; + +public class AnswerRepository : IAnswerRepository +{ + private readonly DataContext _context; + + public AnswerRepository(DataContext context) + { + _context = context; + } + + public async Task GetByIdAsync(int id) + { + throw new NotImplementedException(); + } + + public async Task> GetAllAsync() + { + return await _context.Answers.ToListAsync(); + } + + public async Task AddAsync(Answer entity) + { + await _context.Answers.AddAsync(entity); + await _context.SaveChangesAsync(); + } + + public async Task UpdateAsync(Answer entity) + { + _context.Answers.Update(entity); + await _context.SaveChangesAsync(); + } + + public async Task DeleteAsync(Answer entity) + { + _context.Answers.Remove(entity); + await _context.SaveChangesAsync(); + } + + public async Task> GetByAttemptIdAsync(int attemptId) + { + return await _context.Answers.Where(a => a.CompletionId == attemptId).ToListAsync(); + } + + public async Task> GetByQuestionIdAsync(int questionId) + { + return await _context.Answers.Where(a => a.QuestionId == questionId).ToListAsync(); + } +} \ No newline at end of file diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs new file mode 100644 index 0000000..bef9e48 --- /dev/null +++ b/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs @@ -0,0 +1,49 @@ +using Microsoft.EntityFrameworkCore; +using SurveyLib.Core.Models; +using SurveyLib.Core.Repositories; +using SurveyLib.Infrastructure.EFCore.Data; + +namespace SurveyLib.Infrastructure.EFCore.Repositories; + +public class CompletionRepository : ICompletionRepository +{ + private readonly DataContext _context; + + public CompletionRepository(DataContext context) + { + _context = context; + } + + public async Task GetByIdAsync(int id) + { + return await _context.Completions.FirstOrDefaultAsync(c => c.Id == id); + } + + public async Task> GetAllAsync() + { + return await _context.Completions.ToListAsync(); + } + + public async Task AddAsync(Completion entity) + { + await _context.Completions.AddAsync(entity); + await _context.SaveChangesAsync(); + } + + public async Task UpdateAsync(Completion entity) + { + _context.Completions.Update(entity); + await _context.SaveChangesAsync(); + } + + public async Task DeleteAsync(Completion entity) + { + _context.Completions.Remove(entity); + await _context.SaveChangesAsync(); + } + + public async Task> GetBySurveyIdAsync(int surveyId) + { + return await _context.Completions.Where(c => c.SurveyId == surveyId).ToListAsync(); + } +} \ No newline at end of file diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs new file mode 100644 index 0000000..d124a8e --- /dev/null +++ b/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs @@ -0,0 +1,55 @@ +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 GetByIdAsync(int id) + { + return await _context.Questions.FirstOrDefaultAsync(q => q.Id == id); + } + + public async Task> 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 GetWithAnswersAsync(int questionId) + { + return await _context.Questions.Include(q => q.Answers) + .FirstOrDefaultAsync(q => q.Id == questionId); + } + + public async Task> GetBySurveyIdAsync(int surveyId) + { + return await _context.Questions.Where(q => q.SurveyId == surveyId).ToListAsync(); + } +} \ No newline at end of file diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs new file mode 100644 index 0000000..b655a9e --- /dev/null +++ b/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs @@ -0,0 +1,61 @@ +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 GetByIdAsync(int id) + { + return await _context.Surveys.FirstOrDefaultAsync(s => s.Id == id); + } + + public async Task> 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 GetWithQuestionsAsync(int surveyId) + { + return await _context.Surveys.Include(survey => survey.Questions) + .FirstOrDefaultAsync(s => s.Id == surveyId); + } + + public async Task GetWithCompletionsAsync(int surveyId) + { + return await _context.Surveys.Include(survey => survey.Completions) + .FirstOrDefaultAsync(s => s.Id == surveyId); + } + + public async Task> FindByTitleAsync(string title) + { + return await _context.Surveys.Where(s => s.Title.ToLower().Contains(title.ToLower())).ToListAsync(); + } +} \ No newline at end of file diff --git a/SurveyLib.Infrastructure.EFCore/SurveyLib.Infrastructure.EFCore.csproj b/SurveyLib.Infrastructure.EFCore/SurveyLib.Infrastructure.EFCore.csproj new file mode 100644 index 0000000..08f712f --- /dev/null +++ b/SurveyLib.Infrastructure.EFCore/SurveyLib.Infrastructure.EFCore.csproj @@ -0,0 +1,17 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + diff --git a/SurveyLib.sln b/SurveyLib.sln index dafac24..8eefef0 100644 --- a/SurveyLib.sln +++ b/SurveyLib.sln @@ -2,6 +2,8 @@ Microsoft Visual Studio Solution File, Format Version 12.00 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SurveyLib.Core", "SurveyLib.Core\SurveyLib.Core.csproj", "{89972F62-9976-41F2-8259-428F4F196AFF}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SurveyLib.Infrastructure.EFCore", "SurveyLib.Infrastructure.EFCore\SurveyLib.Infrastructure.EFCore.csproj", "{57B8F7D6-53A6-41DF-961A-D5E3E7186DB7}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -12,5 +14,9 @@ Global {89972F62-9976-41F2-8259-428F4F196AFF}.Debug|Any CPU.Build.0 = Debug|Any CPU {89972F62-9976-41F2-8259-428F4F196AFF}.Release|Any CPU.ActiveCfg = Release|Any CPU {89972F62-9976-41F2-8259-428F4F196AFF}.Release|Any CPU.Build.0 = Release|Any CPU + {57B8F7D6-53A6-41DF-961A-D5E3E7186DB7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {57B8F7D6-53A6-41DF-961A-D5E3E7186DB7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {57B8F7D6-53A6-41DF-961A-D5E3E7186DB7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {57B8F7D6-53A6-41DF-961A-D5E3E7186DB7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal