From ca37b03f33d91bff38289c129bb4c6b1b4c278b3 Mon Sep 17 00:00:00 2001 From: shept Date: Fri, 14 Mar 2025 22:39:19 +0500 Subject: [PATCH 1/7] created infrastructure project --- .../SurveyLib.Infrastructure.EFCore.csproj | 18 ++++++++++++++++++ SurveyLib.sln | 6 ++++++ 2 files changed, 24 insertions(+) create mode 100644 SurveyLib.Infrastructure.EFCore/SurveyLib.Infrastructure.EFCore.csproj diff --git a/SurveyLib.Infrastructure.EFCore/SurveyLib.Infrastructure.EFCore.csproj b/SurveyLib.Infrastructure.EFCore/SurveyLib.Infrastructure.EFCore.csproj new file mode 100644 index 0000000..0123882 --- /dev/null +++ b/SurveyLib.Infrastructure.EFCore/SurveyLib.Infrastructure.EFCore.csproj @@ -0,0 +1,18 @@ + + + + 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 From 4e3c3e6149a9f108b8358f5d4d565e7968eee1ef Mon Sep 17 00:00:00 2001 From: shept Date: Fri, 14 Mar 2025 22:41:53 +0500 Subject: [PATCH 2/7] added reference to Core added empty implementations of repositories --- .../Repositories/AnswerRepository.cs | 42 +++++++++++++++++ .../Repositories/CompletionRepository.cs | 37 +++++++++++++++ .../Repositories/QuestionRepository.cs | 42 +++++++++++++++++ .../Repositories/SurveyRepository.cs | 47 +++++++++++++++++++ .../SurveyLib.Infrastructure.EFCore.csproj | 5 +- 5 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs create mode 100644 SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs create mode 100644 SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs create mode 100644 SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs new file mode 100644 index 0000000..3906537 --- /dev/null +++ b/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs @@ -0,0 +1,42 @@ +using SurveyLib.Core.Models; +using SurveyLib.Core.Repositories; + +namespace SurveyLib.Infrastructure.EFCore.Repositories; + +public class AnswerRepository : IAnswerRepository +{ + public Task? GetByIdAsync(int id) + { + throw new NotImplementedException(); + } + + public Task> GetAllAsync() + { + throw new NotImplementedException(); + } + + public Task AddAsync(Answer entity) + { + throw new NotImplementedException(); + } + + public Task UpdateAsync(Answer entity) + { + throw new NotImplementedException(); + } + + public Task DeleteAsync(Answer entity) + { + throw new NotImplementedException(); + } + + public Task> GetByAttemptIdAsync(int attemptId) + { + throw new NotImplementedException(); + } + + public Task> GetByQuestionIdAsync(int questionId) + { + throw new NotImplementedException(); + } +} \ 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..c628977 --- /dev/null +++ b/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs @@ -0,0 +1,37 @@ +using SurveyLib.Core.Models; +using SurveyLib.Core.Repositories; + +namespace SurveyLib.Infrastructure.EFCore.Repositories; + +public class CompletionRepository : ICompletionRepository +{ + public Task? GetByIdAsync(int id) + { + throw new NotImplementedException(); + } + + public Task> GetAllAsync() + { + throw new NotImplementedException(); + } + + public Task AddAsync(Completion entity) + { + throw new NotImplementedException(); + } + + public Task UpdateAsync(Completion entity) + { + throw new NotImplementedException(); + } + + public Task DeleteAsync(Completion entity) + { + throw new NotImplementedException(); + } + + public Task> GetBySurveyIdAsync(int surveyId) + { + throw new NotImplementedException(); + } +} \ 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..d193671 --- /dev/null +++ b/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs @@ -0,0 +1,42 @@ +using SurveyLib.Core.Models; +using SurveyLib.Core.Repositories; + +namespace SurveyLib.Infrastructure.EFCore.Repositories; + +public class QuestionRepository : IQuestionRepository +{ + public Task? GetByIdAsync(int id) + { + throw new NotImplementedException(); + } + + public Task> GetAllAsync() + { + throw new NotImplementedException(); + } + + public Task AddAsync(QuestionBase entity) + { + throw new NotImplementedException(); + } + + public Task UpdateAsync(QuestionBase entity) + { + throw new NotImplementedException(); + } + + public Task DeleteAsync(QuestionBase entity) + { + throw new NotImplementedException(); + } + + public Task GetWithAnswersAsync(int questionId) + { + throw new NotImplementedException(); + } + + public Task> GetBySurveyIdAsync(int surveyId) + { + throw new NotImplementedException(); + } +} \ 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..67e3295 --- /dev/null +++ b/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs @@ -0,0 +1,47 @@ +using SurveyLib.Core.Models; +using SurveyLib.Core.Repositories; + +namespace SurveyLib.Infrastructure.EFCore.Repositories; + +public class SurveyRepository : ISurveyRepository +{ + public Task? GetByIdAsync(int id) + { + throw new NotImplementedException(); + } + + public Task> GetAllAsync() + { + throw new NotImplementedException(); + } + + public Task AddAsync(Survey entity) + { + throw new NotImplementedException(); + } + + public Task UpdateAsync(Survey entity) + { + throw new NotImplementedException(); + } + + public Task DeleteAsync(Survey entity) + { + throw new NotImplementedException(); + } + + public Task GetWithQuestionsAsync(int surveyId) + { + throw new NotImplementedException(); + } + + public Task GetWithCompletionsAsync(int surveyId) + { + throw new NotImplementedException(); + } + + public Task FindByTitleAsync(string title) + { + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/SurveyLib.Infrastructure.EFCore/SurveyLib.Infrastructure.EFCore.csproj b/SurveyLib.Infrastructure.EFCore/SurveyLib.Infrastructure.EFCore.csproj index 0123882..1e98149 100644 --- a/SurveyLib.Infrastructure.EFCore/SurveyLib.Infrastructure.EFCore.csproj +++ b/SurveyLib.Infrastructure.EFCore/SurveyLib.Infrastructure.EFCore.csproj @@ -8,11 +8,14 @@ - + + + + From 7d06084dfb67657848a027bdb9d0a8b5c83924da Mon Sep 17 00:00:00 2001 From: shept Date: Fri, 14 Mar 2025 22:47:19 +0500 Subject: [PATCH 3/7] added DataContext.cs --- .../Data/DataContext.cs | 17 +++++++++++++++++ .../Repositories/AnswerRepository.cs | 9 +++++++++ .../Repositories/CompletionRepository.cs | 8 ++++++++ .../Repositories/QuestionRepository.cs | 8 ++++++++ .../Repositories/SurveyRepository.cs | 8 ++++++++ .../SurveyLib.Infrastructure.EFCore.csproj | 4 ---- 6 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 SurveyLib.Infrastructure.EFCore/Data/DataContext.cs 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 index 3906537..d9a78c6 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs @@ -1,10 +1,19 @@ 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 Task? GetByIdAsync(int id) { throw new NotImplementedException(); diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs index c628977..83c2705 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs @@ -1,10 +1,18 @@ 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 Task? GetByIdAsync(int id) { throw new NotImplementedException(); diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs index d193671..eb3403b 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs @@ -1,10 +1,18 @@ 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 Task? GetByIdAsync(int id) { throw new NotImplementedException(); diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs index 67e3295..4aeb17e 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs @@ -1,10 +1,18 @@ 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 Task? GetByIdAsync(int id) { throw new NotImplementedException(); diff --git a/SurveyLib.Infrastructure.EFCore/SurveyLib.Infrastructure.EFCore.csproj b/SurveyLib.Infrastructure.EFCore/SurveyLib.Infrastructure.EFCore.csproj index 1e98149..08f712f 100644 --- a/SurveyLib.Infrastructure.EFCore/SurveyLib.Infrastructure.EFCore.csproj +++ b/SurveyLib.Infrastructure.EFCore/SurveyLib.Infrastructure.EFCore.csproj @@ -6,10 +6,6 @@ enable - - - - From 960a6ce191fbe03aa98d55fba600d5b9756c1aa1 Mon Sep 17 00:00:00 2001 From: shept Date: Fri, 14 Mar 2025 23:07:06 +0500 Subject: [PATCH 4/7] completed SurveyRepository.cs --- .../Repositories/IGenericRepository.cs | 4 ++- .../Repositories/ISurveyRepository.cs | 2 +- .../Repositories/AnswerRepository.cs | 2 +- .../Repositories/CompletionRepository.cs | 2 +- .../Repositories/QuestionRepository.cs | 2 +- .../Repositories/SurveyRepository.cs | 36 ++++++++++--------- 6 files changed, 27 insertions(+), 21 deletions(-) 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/Repositories/AnswerRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs index d9a78c6..fb77da5 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs @@ -14,7 +14,7 @@ public class AnswerRepository : IAnswerRepository _context = context; } - public Task? GetByIdAsync(int id) + public Task GetByIdAsync(int id) { throw new NotImplementedException(); } diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs index 83c2705..e33eba6 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs @@ -13,7 +13,7 @@ public class CompletionRepository : ICompletionRepository _context = context; } - public Task? GetByIdAsync(int id) + public Task GetByIdAsync(int id) { throw new NotImplementedException(); } diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs index eb3403b..ce448a6 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs @@ -13,7 +13,7 @@ public class QuestionRepository : IQuestionRepository _context = context; } - public Task? GetByIdAsync(int id) + public Task GetByIdAsync(int id) { throw new NotImplementedException(); } diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs index 4aeb17e..c36e455 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs @@ -1,3 +1,4 @@ +using Microsoft.EntityFrameworkCore; using SurveyLib.Core.Models; using SurveyLib.Core.Repositories; using SurveyLib.Infrastructure.EFCore.Data; @@ -13,43 +14,46 @@ public class SurveyRepository : ISurveyRepository _context = context; } - public Task? GetByIdAsync(int id) + public async Task GetByIdAsync(int id) { - throw new NotImplementedException(); + return await _context.Surveys.FirstOrDefaultAsync(s => s.Id == id); } - public Task> GetAllAsync() + public async Task> GetAllAsync() { - throw new NotImplementedException(); + return await _context.Surveys.ToListAsync(); } - public Task AddAsync(Survey entity) + public async Task AddAsync(Survey entity) { - throw new NotImplementedException(); + await _context.Surveys.AddAsync(entity); + await _context.SaveChangesAsync(); } - public Task UpdateAsync(Survey entity) + public async Task UpdateAsync(Survey entity) { - throw new NotImplementedException(); + _context.Surveys.Update(entity); + await _context.SaveChangesAsync(); } - public Task DeleteAsync(Survey entity) + public async Task DeleteAsync(Survey entity) { - throw new NotImplementedException(); + _context.Surveys.Remove(entity); + await _context.SaveChangesAsync(); } - public Task GetWithQuestionsAsync(int surveyId) + public async Task GetWithQuestionsAsync(int surveyId) { - throw new NotImplementedException(); + return await _context.Surveys.Include(survey => survey.Questions).FirstOrDefaultAsync(s => s.Id == surveyId); } - public Task GetWithCompletionsAsync(int surveyId) + public async Task GetWithCompletionsAsync(int surveyId) { - throw new NotImplementedException(); + return await _context.Surveys.Include(survey => survey.Completions).FirstOrDefaultAsync(s => s.Id == surveyId); } - public Task FindByTitleAsync(string title) + public async Task> FindByTitleAsync(string title) { - throw new NotImplementedException(); + return await _context.Surveys.Where(s => s.Title.ToLower().Contains(title.ToLower())).ToListAsync(); } } \ No newline at end of file From 62facb34239c12c7dafa07694e264fcc1de8241f Mon Sep 17 00:00:00 2001 From: shept Date: Fri, 14 Mar 2025 23:15:33 +0500 Subject: [PATCH 5/7] completed QuestionRepository.cs small readability fixes for SurveyRepository.cs --- .../Repositories/QuestionRepository.cs | 33 +++++++++++-------- .../Repositories/SurveyRepository.cs | 6 ++-- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs index ce448a6..d124a8e 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs @@ -1,3 +1,4 @@ +using Microsoft.EntityFrameworkCore; using SurveyLib.Core.Models; using SurveyLib.Core.Repositories; using SurveyLib.Infrastructure.EFCore.Data; @@ -13,38 +14,42 @@ public class QuestionRepository : IQuestionRepository _context = context; } - public Task GetByIdAsync(int id) + public async Task GetByIdAsync(int id) { - throw new NotImplementedException(); + return await _context.Questions.FirstOrDefaultAsync(q => q.Id == id); } - public Task> GetAllAsync() + public async Task> GetAllAsync() { - throw new NotImplementedException(); + return await _context.Questions.ToListAsync(); } - public Task AddAsync(QuestionBase entity) + public async Task AddAsync(QuestionBase entity) { - throw new NotImplementedException(); + await _context.Questions.AddAsync(entity); + await _context.SaveChangesAsync(); } - public Task UpdateAsync(QuestionBase entity) + public async Task UpdateAsync(QuestionBase entity) { - throw new NotImplementedException(); + _context.Questions.Update(entity); + await _context.SaveChangesAsync(); } - public Task DeleteAsync(QuestionBase entity) + public async Task DeleteAsync(QuestionBase entity) { - throw new NotImplementedException(); + _context.Questions.Remove(entity); + await _context.SaveChangesAsync(); } - public Task GetWithAnswersAsync(int questionId) + public async Task GetWithAnswersAsync(int questionId) { - throw new NotImplementedException(); + return await _context.Questions.Include(q => q.Answers) + .FirstOrDefaultAsync(q => q.Id == questionId); } - public Task> GetBySurveyIdAsync(int surveyId) + public async Task> GetBySurveyIdAsync(int surveyId) { - throw new NotImplementedException(); + 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 index c36e455..b655a9e 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs @@ -44,12 +44,14 @@ public class SurveyRepository : ISurveyRepository public async Task GetWithQuestionsAsync(int surveyId) { - return await _context.Surveys.Include(survey => survey.Questions).FirstOrDefaultAsync(s => s.Id == 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); + return await _context.Surveys.Include(survey => survey.Completions) + .FirstOrDefaultAsync(s => s.Id == surveyId); } public async Task> FindByTitleAsync(string title) From 8fc3932df35259f141c66de24626d4aeae368a5f Mon Sep 17 00:00:00 2001 From: shept Date: Fri, 14 Mar 2025 23:22:13 +0500 Subject: [PATCH 6/7] completed CompletionRepository.cs --- .../Repositories/CompletionRepository.cs | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs index e33eba6..bef9e48 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs @@ -1,3 +1,4 @@ +using Microsoft.EntityFrameworkCore; using SurveyLib.Core.Models; using SurveyLib.Core.Repositories; using SurveyLib.Infrastructure.EFCore.Data; @@ -13,33 +14,36 @@ public class CompletionRepository : ICompletionRepository _context = context; } - public Task GetByIdAsync(int id) + public async Task GetByIdAsync(int id) { - throw new NotImplementedException(); + return await _context.Completions.FirstOrDefaultAsync(c => c.Id == id); } - public Task> GetAllAsync() + public async Task> GetAllAsync() { - throw new NotImplementedException(); + return await _context.Completions.ToListAsync(); } - public Task AddAsync(Completion entity) + public async Task AddAsync(Completion entity) { - throw new NotImplementedException(); + await _context.Completions.AddAsync(entity); + await _context.SaveChangesAsync(); } - public Task UpdateAsync(Completion entity) + public async Task UpdateAsync(Completion entity) { - throw new NotImplementedException(); + _context.Completions.Update(entity); + await _context.SaveChangesAsync(); } - public Task DeleteAsync(Completion entity) + public async Task DeleteAsync(Completion entity) { - throw new NotImplementedException(); + _context.Completions.Remove(entity); + await _context.SaveChangesAsync(); } - public Task> GetBySurveyIdAsync(int surveyId) + public async Task> GetBySurveyIdAsync(int surveyId) { - throw new NotImplementedException(); + return await _context.Completions.Where(c => c.SurveyId == surveyId).ToListAsync(); } } \ No newline at end of file From bd3c70913e54e2c9364637caa5aab96c7d2150d8 Mon Sep 17 00:00:00 2001 From: shept Date: Fri, 14 Mar 2025 23:26:32 +0500 Subject: [PATCH 7/7] completed AnswerRepository.cs --- .../Repositories/AnswerRepository.cs | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs index fb77da5..d71e0a7 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs @@ -14,38 +14,41 @@ public class AnswerRepository : IAnswerRepository _context = context; } - public Task GetByIdAsync(int id) + public async Task GetByIdAsync(int id) { throw new NotImplementedException(); } - public Task> GetAllAsync() + public async Task> GetAllAsync() { - throw new NotImplementedException(); + return await _context.Answers.ToListAsync(); } - public Task AddAsync(Answer entity) + public async Task AddAsync(Answer entity) { - throw new NotImplementedException(); + await _context.Answers.AddAsync(entity); + await _context.SaveChangesAsync(); } - public Task UpdateAsync(Answer entity) + public async Task UpdateAsync(Answer entity) { - throw new NotImplementedException(); + _context.Answers.Update(entity); + await _context.SaveChangesAsync(); } - public Task DeleteAsync(Answer entity) + public async Task DeleteAsync(Answer entity) { - throw new NotImplementedException(); + _context.Answers.Remove(entity); + await _context.SaveChangesAsync(); } - public Task> GetByAttemptIdAsync(int attemptId) + public async Task> GetByAttemptIdAsync(int attemptId) { - throw new NotImplementedException(); + return await _context.Answers.Where(a => a.CompletionId == attemptId).ToListAsync(); } - public Task> GetByQuestionIdAsync(int questionId) + public async Task> GetByQuestionIdAsync(int questionId) { - throw new NotImplementedException(); + return await _context.Answers.Where(a => a.QuestionId == questionId).ToListAsync(); } } \ No newline at end of file