From ca37b03f33d91bff38289c129bb4c6b1b4c278b3 Mon Sep 17 00:00:00 2001 From: shept Date: Fri, 14 Mar 2025 22:39:19 +0500 Subject: [PATCH 01/18] 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 02/18] 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 03/18] 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 04/18] 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 05/18] 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 06/18] 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 07/18] 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 From bac3ed072e388e010d31edcc21073a2b87f3fa9e Mon Sep 17 00:00:00 2001 From: shept Date: Sat, 15 Mar 2025 00:10:12 +0500 Subject: [PATCH 08/18] added integration tools --- .../Integration/DBType.cs | 7 ++++ .../Integration/InfrastructureExtensions.cs | 33 +++++++++++++++++++ .../SurveyLib.Infrastructure.EFCore.csproj | 2 ++ 3 files changed, 42 insertions(+) create mode 100644 SurveyLib.Infrastructure.EFCore/Integration/DBType.cs create mode 100644 SurveyLib.Infrastructure.EFCore/Integration/InfrastructureExtensions.cs diff --git a/SurveyLib.Infrastructure.EFCore/Integration/DBType.cs b/SurveyLib.Infrastructure.EFCore/Integration/DBType.cs new file mode 100644 index 0000000..f498c86 --- /dev/null +++ b/SurveyLib.Infrastructure.EFCore/Integration/DBType.cs @@ -0,0 +1,7 @@ +namespace SurveyLib.Infrastructure.EFCore.Integration; + +public enum DBType +{ + Sqlite, + PostgreSql +} \ No newline at end of file diff --git a/SurveyLib.Infrastructure.EFCore/Integration/InfrastructureExtensions.cs b/SurveyLib.Infrastructure.EFCore/Integration/InfrastructureExtensions.cs new file mode 100644 index 0000000..4735df6 --- /dev/null +++ b/SurveyLib.Infrastructure.EFCore/Integration/InfrastructureExtensions.cs @@ -0,0 +1,33 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using SurveyLib.Core.Repositories; +using SurveyLib.Infrastructure.EFCore.Data; +using SurveyLib.Infrastructure.EFCore.Repositories; + +namespace SurveyLib.Infrastructure.EFCore.Integration; + +public static class InfrastructureExtensions +{ + public static IServiceCollection AddSurveyInfrastructure(this IServiceCollection services, string connectionString, + DBType dbType) + { + switch (dbType) + { + case DBType.Sqlite: + services.AddDbContext(options => options.UseSqlite(connectionString)); + break; + case DBType.PostgreSql: + services.AddDbContext(options => options.UseNpgsql(connectionString)); + break; + default: + throw new ArgumentOutOfRangeException(nameof(dbType), dbType, null); + } + + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + + return services; + } +} \ 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 08f712f..e154bcc 100644 --- a/SurveyLib.Infrastructure.EFCore/SurveyLib.Infrastructure.EFCore.csproj +++ b/SurveyLib.Infrastructure.EFCore/SurveyLib.Infrastructure.EFCore.csproj @@ -8,6 +8,8 @@ + + From cac3cd5ea400e55c504e399f871fb35a963e16c9 Mon Sep 17 00:00:00 2001 From: shept Date: Tue, 25 Mar 2025 22:36:27 +0500 Subject: [PATCH 09/18] deleted some stupid bullshit about integration (i guess datacontext next) --- .../Integration/DBType.cs | 7 ---- .../Integration/InfrastructureExtensions.cs | 33 ------------------- 2 files changed, 40 deletions(-) delete mode 100644 SurveyLib.Infrastructure.EFCore/Integration/DBType.cs delete mode 100644 SurveyLib.Infrastructure.EFCore/Integration/InfrastructureExtensions.cs diff --git a/SurveyLib.Infrastructure.EFCore/Integration/DBType.cs b/SurveyLib.Infrastructure.EFCore/Integration/DBType.cs deleted file mode 100644 index f498c86..0000000 --- a/SurveyLib.Infrastructure.EFCore/Integration/DBType.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace SurveyLib.Infrastructure.EFCore.Integration; - -public enum DBType -{ - Sqlite, - PostgreSql -} \ No newline at end of file diff --git a/SurveyLib.Infrastructure.EFCore/Integration/InfrastructureExtensions.cs b/SurveyLib.Infrastructure.EFCore/Integration/InfrastructureExtensions.cs deleted file mode 100644 index 4735df6..0000000 --- a/SurveyLib.Infrastructure.EFCore/Integration/InfrastructureExtensions.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; -using SurveyLib.Core.Repositories; -using SurveyLib.Infrastructure.EFCore.Data; -using SurveyLib.Infrastructure.EFCore.Repositories; - -namespace SurveyLib.Infrastructure.EFCore.Integration; - -public static class InfrastructureExtensions -{ - public static IServiceCollection AddSurveyInfrastructure(this IServiceCollection services, string connectionString, - DBType dbType) - { - switch (dbType) - { - case DBType.Sqlite: - services.AddDbContext(options => options.UseSqlite(connectionString)); - break; - case DBType.PostgreSql: - services.AddDbContext(options => options.UseNpgsql(connectionString)); - break; - default: - throw new ArgumentOutOfRangeException(nameof(dbType), dbType, null); - } - - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - - return services; - } -} \ No newline at end of file From 7f482cac958d83c9f9bf64589952a657313e3a71 Mon Sep 17 00:00:00 2001 From: shept Date: Tue, 1 Apr 2025 16:29:30 +0500 Subject: [PATCH 10/18] changed GetById --- .../Repositories/CompletionRepository.cs | 2 +- .../Repositories/QuestionRepository.cs | 2 +- .../Repositories/SurveyRepository.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs index bef9e48..9442973 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs @@ -16,7 +16,7 @@ public class CompletionRepository : ICompletionRepository public async Task GetByIdAsync(int id) { - return await _context.Completions.FirstOrDefaultAsync(c => c.Id == id); + return await _context.Completions.FindAsync(id); } public async Task> GetAllAsync() diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs index d124a8e..8145f97 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs @@ -16,7 +16,7 @@ public class QuestionRepository : IQuestionRepository public async Task GetByIdAsync(int id) { - return await _context.Questions.FirstOrDefaultAsync(q => q.Id == id); + return await _context.Questions.FindAsync(id); } public async Task> GetAllAsync() diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs index b655a9e..afe6ca8 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs @@ -16,7 +16,7 @@ public class SurveyRepository : ISurveyRepository public async Task GetByIdAsync(int id) { - return await _context.Surveys.FirstOrDefaultAsync(s => s.Id == id); + return await _context.Surveys.FindAsync(id); } public async Task> GetAllAsync() From 74b650dd8e9ccd63d422a97dcf88d1f947a6db68 Mon Sep 17 00:00:00 2001 From: shept Date: Tue, 1 Apr 2025 16:52:02 +0500 Subject: [PATCH 11/18] added some service interfaces --- SurveyLib.Core/Services/IAnswerService.cs | 12 ++++++++++++ SurveyLib.Core/Services/ICompletionService.cs | 11 +++++++++++ SurveyLib.Core/Services/IQuestionService.cs | 11 +++++++++++ SurveyLib.Core/Services/ISurveyService.cs | 14 ++++++++++++++ 4 files changed, 48 insertions(+) create mode 100644 SurveyLib.Core/Services/IAnswerService.cs create mode 100644 SurveyLib.Core/Services/ICompletionService.cs create mode 100644 SurveyLib.Core/Services/IQuestionService.cs create mode 100644 SurveyLib.Core/Services/ISurveyService.cs diff --git a/SurveyLib.Core/Services/IAnswerService.cs b/SurveyLib.Core/Services/IAnswerService.cs new file mode 100644 index 0000000..d2122dc --- /dev/null +++ b/SurveyLib.Core/Services/IAnswerService.cs @@ -0,0 +1,12 @@ +using SurveyLib.Core.Models; + +namespace SurveyLib.Core.Services; + +public interface IAnswerService +{ + Task AddAnswerAsync(Answer answer); + Task UpdateAnswerAsync(Answer answer); + Task DeleteAnswerAsync(int id); + Task> GetAnswersByCompletionIdAsync(int completionId); + Task> GetAnswersByQuestionIdAsync(int questionId); +} \ No newline at end of file diff --git a/SurveyLib.Core/Services/ICompletionService.cs b/SurveyLib.Core/Services/ICompletionService.cs new file mode 100644 index 0000000..3a9430d --- /dev/null +++ b/SurveyLib.Core/Services/ICompletionService.cs @@ -0,0 +1,11 @@ +using SurveyLib.Core.Models; + +namespace SurveyLib.Core.Services; + +public interface ICompletionService +{ + Task AddCompletionAsync(Completion completion); + Task UpdateCompletionAsync(Completion completion); + Task DeleteCompletionAsync(int id); + Task> GetCompletionsBySurveyIdAsync(int surveyId); +} \ No newline at end of file diff --git a/SurveyLib.Core/Services/IQuestionService.cs b/SurveyLib.Core/Services/IQuestionService.cs new file mode 100644 index 0000000..af0d6d5 --- /dev/null +++ b/SurveyLib.Core/Services/IQuestionService.cs @@ -0,0 +1,11 @@ +using SurveyLib.Core.Models; + +namespace SurveyLib.Core.Services; + +public interface IQuestionService +{ + Task AddQuestionAsync(QuestionBase question); + Task UpdateQuestionAsync(QuestionBase question); + Task DeleteQuestionAsync(int id); + Task> GetQuestionsBySurveyIdAsync(int surveyId); +} \ No newline at end of file diff --git a/SurveyLib.Core/Services/ISurveyService.cs b/SurveyLib.Core/Services/ISurveyService.cs new file mode 100644 index 0000000..6ec6d55 --- /dev/null +++ b/SurveyLib.Core/Services/ISurveyService.cs @@ -0,0 +1,14 @@ +using SurveyLib.Core.Models; + +namespace SurveyLib.Core.Services; + +public interface ISurveyService +{ + Task AddSurveyAsync(Survey survey); + Task UpdateSurveyAsync(Survey survey); + Task DeleteSurveyAsync(int id); + Task> GetSurveysAsync(); + Task GetSurveyByIdAsync(int id); + Task GetSurveyWithQuestionsByIdAsync(int id); + Task GetSurveyWithAnswersByIdAsync(int id); +} \ No newline at end of file From 90f4f9dd51b45ed43932974369a637765a916c0e Mon Sep 17 00:00:00 2001 From: shept Date: Tue, 1 Apr 2025 16:55:04 +0500 Subject: [PATCH 12/18] deleted DataContext.cs --- .../Data/DataContext.cs | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 SurveyLib.Infrastructure.EFCore/Data/DataContext.cs diff --git a/SurveyLib.Infrastructure.EFCore/Data/DataContext.cs b/SurveyLib.Infrastructure.EFCore/Data/DataContext.cs deleted file mode 100644 index dcc2e68..0000000 --- a/SurveyLib.Infrastructure.EFCore/Data/DataContext.cs +++ /dev/null @@ -1,17 +0,0 @@ -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 From 8959c3d984eeccec7b3e3b97f35f072e57430d6a Mon Sep 17 00:00:00 2001 From: shept Date: Tue, 1 Apr 2025 16:59:53 +0500 Subject: [PATCH 13/18] understood my mistake and created DataContext.cs with new name lol --- .../Data/SurveyDbContext.cs | 17 +++++++++++++++++ .../Repositories/AnswerRepository.cs | 4 ++-- .../Repositories/CompletionRepository.cs | 4 ++-- .../Repositories/QuestionRepository.cs | 4 ++-- .../Repositories/SurveyRepository.cs | 4 ++-- 5 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 SurveyLib.Infrastructure.EFCore/Data/SurveyDbContext.cs diff --git a/SurveyLib.Infrastructure.EFCore/Data/SurveyDbContext.cs b/SurveyLib.Infrastructure.EFCore/Data/SurveyDbContext.cs new file mode 100644 index 0000000..5a287c0 --- /dev/null +++ b/SurveyLib.Infrastructure.EFCore/Data/SurveyDbContext.cs @@ -0,0 +1,17 @@ +using Microsoft.EntityFrameworkCore; +using SurveyLib.Core.Models; + +namespace SurveyLib.Infrastructure.EFCore.Data; + +public class SurveyDbContext : DbContext +{ + public DbSet Surveys { get; set; } + public DbSet Questions { get; set; } + public DbSet Completions { get; set; } + public DbSet Answers { get; set; } + + public SurveyDbContext(DbContextOptions options) : base(options) + { + + } +} \ No newline at end of file diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs index d71e0a7..a1d6436 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs @@ -7,9 +7,9 @@ namespace SurveyLib.Infrastructure.EFCore.Repositories; public class AnswerRepository : IAnswerRepository { - private readonly DataContext _context; + private readonly SurveyDbContext _context; - public AnswerRepository(DataContext context) + public AnswerRepository(SurveyDbContext context) { _context = context; } diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs index 9442973..30e6164 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs @@ -7,9 +7,9 @@ namespace SurveyLib.Infrastructure.EFCore.Repositories; public class CompletionRepository : ICompletionRepository { - private readonly DataContext _context; + private readonly SurveyDbContext _context; - public CompletionRepository(DataContext context) + public CompletionRepository(SurveyDbContext context) { _context = context; } diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs index 8145f97..e107973 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs @@ -7,9 +7,9 @@ namespace SurveyLib.Infrastructure.EFCore.Repositories; public class QuestionRepository : IQuestionRepository { - private readonly DataContext _context; + private readonly SurveyDbContext _context; - public QuestionRepository(DataContext context) + public QuestionRepository(SurveyDbContext context) { _context = context; } diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs index afe6ca8..2890053 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs @@ -7,9 +7,9 @@ namespace SurveyLib.Infrastructure.EFCore.Repositories; public class SurveyRepository : ISurveyRepository { - private readonly DataContext _context; + private readonly SurveyDbContext _context; - public SurveyRepository(DataContext context) + public SurveyRepository(SurveyDbContext context) { _context = context; } From cce3ea2645c9d4735925872acce8936e1b81fa75 Mon Sep 17 00:00:00 2001 From: shept Date: Wed, 16 Apr 2025 16:42:56 +0500 Subject: [PATCH 14/18] added more question variants --- SurveyLib.Core/Models/AnswerVariant.cs | 10 ++++++++++ SurveyLib.Core/Models/MultipleAnswerQuestion.cs | 6 ++++++ SurveyLib.Core/Models/QuestionBase.cs | 2 ++ SurveyLib.Core/Models/SingleAnswerQuestion.cs | 6 ++++++ SurveyLib.Core/Models/TextQuestion.cs | 5 +++++ .../Data/SurveyDbContext.cs | 6 +++++- 6 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 SurveyLib.Core/Models/AnswerVariant.cs create mode 100644 SurveyLib.Core/Models/MultipleAnswerQuestion.cs create mode 100644 SurveyLib.Core/Models/SingleAnswerQuestion.cs create mode 100644 SurveyLib.Core/Models/TextQuestion.cs diff --git a/SurveyLib.Core/Models/AnswerVariant.cs b/SurveyLib.Core/Models/AnswerVariant.cs new file mode 100644 index 0000000..9806dd5 --- /dev/null +++ b/SurveyLib.Core/Models/AnswerVariant.cs @@ -0,0 +1,10 @@ +namespace SurveyLib.Core.Models; + +public class AnswerVariant +{ + public int Id { get; set; } + public int QuestionId { get; set; } + public string Text { get; set; } + + public QuestionBase Question { get; set; } +} \ No newline at end of file diff --git a/SurveyLib.Core/Models/MultipleAnswerQuestion.cs b/SurveyLib.Core/Models/MultipleAnswerQuestion.cs new file mode 100644 index 0000000..ec4349c --- /dev/null +++ b/SurveyLib.Core/Models/MultipleAnswerQuestion.cs @@ -0,0 +1,6 @@ +namespace SurveyLib.Core.Models; + +public class MultipleAnswerQuestion : QuestionBase +{ + public ICollection AnswerVariants { get; set; } +} \ No newline at end of file diff --git a/SurveyLib.Core/Models/QuestionBase.cs b/SurveyLib.Core/Models/QuestionBase.cs index 4f2f581..2ca4fa0 100644 --- a/SurveyLib.Core/Models/QuestionBase.cs +++ b/SurveyLib.Core/Models/QuestionBase.cs @@ -6,6 +6,8 @@ public class QuestionBase public int SurveyId { get; set; } public string Title { get; set; } + public string Discriminator { get; set; } + public Survey Survey { get; set; } public ICollection Answers { get; set; } } \ No newline at end of file diff --git a/SurveyLib.Core/Models/SingleAnswerQuestion.cs b/SurveyLib.Core/Models/SingleAnswerQuestion.cs new file mode 100644 index 0000000..d99abe9 --- /dev/null +++ b/SurveyLib.Core/Models/SingleAnswerQuestion.cs @@ -0,0 +1,6 @@ +namespace SurveyLib.Core.Models; + +public class SingleAnswerQuestion : QuestionBase +{ + public ICollection AnswerVariants { get; set; } +} \ No newline at end of file diff --git a/SurveyLib.Core/Models/TextQuestion.cs b/SurveyLib.Core/Models/TextQuestion.cs new file mode 100644 index 0000000..ad05932 --- /dev/null +++ b/SurveyLib.Core/Models/TextQuestion.cs @@ -0,0 +1,5 @@ +namespace SurveyLib.Core.Models; + +public class TextQuestion : QuestionBase +{ +} \ No newline at end of file diff --git a/SurveyLib.Infrastructure.EFCore/Data/SurveyDbContext.cs b/SurveyLib.Infrastructure.EFCore/Data/SurveyDbContext.cs index 5a287c0..f4023de 100644 --- a/SurveyLib.Infrastructure.EFCore/Data/SurveyDbContext.cs +++ b/SurveyLib.Infrastructure.EFCore/Data/SurveyDbContext.cs @@ -6,12 +6,16 @@ namespace SurveyLib.Infrastructure.EFCore.Data; public class SurveyDbContext : DbContext { public DbSet Surveys { get; set; } + public DbSet Questions { get; set; } + public DbSet SingleAnswerQuestions { get; set; } + public DbSet MultipleAnswerQuestions { get; set; } + public DbSet TextQuestions { get; set; } + public DbSet Completions { get; set; } public DbSet Answers { get; set; } public SurveyDbContext(DbContextOptions options) : base(options) { - } } \ No newline at end of file From 58271df9494735e4892de3a0c2fa9db7f09bc54e Mon Sep 17 00:00:00 2001 From: shept Date: Wed, 16 Apr 2025 17:31:27 +0500 Subject: [PATCH 15/18] moved Question Variants to separate namespace --- .../Models/{ => QuestionVariants}/MultipleAnswerQuestion.cs | 2 +- .../Models/{ => QuestionVariants}/SingleAnswerQuestion.cs | 2 +- SurveyLib.Core/Models/QuestionVariants/TextQuestion.cs | 5 +++++ SurveyLib.Core/Models/TextQuestion.cs | 5 ----- SurveyLib.Infrastructure.EFCore/Data/SurveyDbContext.cs | 1 + 5 files changed, 8 insertions(+), 7 deletions(-) rename SurveyLib.Core/Models/{ => QuestionVariants}/MultipleAnswerQuestion.cs (70%) rename SurveyLib.Core/Models/{ => QuestionVariants}/SingleAnswerQuestion.cs (70%) create mode 100644 SurveyLib.Core/Models/QuestionVariants/TextQuestion.cs delete mode 100644 SurveyLib.Core/Models/TextQuestion.cs diff --git a/SurveyLib.Core/Models/MultipleAnswerQuestion.cs b/SurveyLib.Core/Models/QuestionVariants/MultipleAnswerQuestion.cs similarity index 70% rename from SurveyLib.Core/Models/MultipleAnswerQuestion.cs rename to SurveyLib.Core/Models/QuestionVariants/MultipleAnswerQuestion.cs index ec4349c..d2eb5b0 100644 --- a/SurveyLib.Core/Models/MultipleAnswerQuestion.cs +++ b/SurveyLib.Core/Models/QuestionVariants/MultipleAnswerQuestion.cs @@ -1,4 +1,4 @@ -namespace SurveyLib.Core.Models; +namespace SurveyLib.Core.Models.QuestionVariants; public class MultipleAnswerQuestion : QuestionBase { diff --git a/SurveyLib.Core/Models/SingleAnswerQuestion.cs b/SurveyLib.Core/Models/QuestionVariants/SingleAnswerQuestion.cs similarity index 70% rename from SurveyLib.Core/Models/SingleAnswerQuestion.cs rename to SurveyLib.Core/Models/QuestionVariants/SingleAnswerQuestion.cs index d99abe9..de1bb01 100644 --- a/SurveyLib.Core/Models/SingleAnswerQuestion.cs +++ b/SurveyLib.Core/Models/QuestionVariants/SingleAnswerQuestion.cs @@ -1,4 +1,4 @@ -namespace SurveyLib.Core.Models; +namespace SurveyLib.Core.Models.QuestionVariants; public class SingleAnswerQuestion : QuestionBase { diff --git a/SurveyLib.Core/Models/QuestionVariants/TextQuestion.cs b/SurveyLib.Core/Models/QuestionVariants/TextQuestion.cs new file mode 100644 index 0000000..f8912cf --- /dev/null +++ b/SurveyLib.Core/Models/QuestionVariants/TextQuestion.cs @@ -0,0 +1,5 @@ +namespace SurveyLib.Core.Models.QuestionVariants; + +public class TextQuestion : QuestionBase +{ +} \ No newline at end of file diff --git a/SurveyLib.Core/Models/TextQuestion.cs b/SurveyLib.Core/Models/TextQuestion.cs deleted file mode 100644 index ad05932..0000000 --- a/SurveyLib.Core/Models/TextQuestion.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace SurveyLib.Core.Models; - -public class TextQuestion : QuestionBase -{ -} \ No newline at end of file diff --git a/SurveyLib.Infrastructure.EFCore/Data/SurveyDbContext.cs b/SurveyLib.Infrastructure.EFCore/Data/SurveyDbContext.cs index f4023de..d77fb6d 100644 --- a/SurveyLib.Infrastructure.EFCore/Data/SurveyDbContext.cs +++ b/SurveyLib.Infrastructure.EFCore/Data/SurveyDbContext.cs @@ -1,5 +1,6 @@ using Microsoft.EntityFrameworkCore; using SurveyLib.Core.Models; +using SurveyLib.Core.Models.QuestionVariants; namespace SurveyLib.Infrastructure.EFCore.Data; From 845db13c63081ce82eea8e71fd6792a166ecf1d0 Mon Sep 17 00:00:00 2001 From: shept Date: Wed, 16 Apr 2025 17:32:24 +0500 Subject: [PATCH 16/18] overthinking repositories from scratch --- .../Repositories/IAnswerRepository.cs | 3 +-- .../Repositories/ICompletionRepository.cs | 2 +- .../Repositories/IQuestionRepository.cs | 3 +-- .../Repositories/ISurveyRepository.cs | 4 +--- .../Repositories/AnswerRepository.cs | 10 ---------- .../Repositories/CompletionRepository.cs | 5 ----- .../Repositories/QuestionRepository.cs | 11 ----------- .../Repositories/SurveyRepository.cs | 17 ----------------- 8 files changed, 4 insertions(+), 51 deletions(-) diff --git a/SurveyLib.Core/Repositories/IAnswerRepository.cs b/SurveyLib.Core/Repositories/IAnswerRepository.cs index 7766933..65e0e0e 100644 --- a/SurveyLib.Core/Repositories/IAnswerRepository.cs +++ b/SurveyLib.Core/Repositories/IAnswerRepository.cs @@ -4,6 +4,5 @@ namespace SurveyLib.Core.Repositories; public interface IAnswerRepository : IGenericRepository { - Task> GetByAttemptIdAsync(int attemptId); - Task> GetByQuestionIdAsync(int questionId); + } \ No newline at end of file diff --git a/SurveyLib.Core/Repositories/ICompletionRepository.cs b/SurveyLib.Core/Repositories/ICompletionRepository.cs index 8145c1c..9b41e97 100644 --- a/SurveyLib.Core/Repositories/ICompletionRepository.cs +++ b/SurveyLib.Core/Repositories/ICompletionRepository.cs @@ -4,5 +4,5 @@ namespace SurveyLib.Core.Repositories; public interface ICompletionRepository : IGenericRepository { - Task> GetBySurveyIdAsync(int surveyId); + } \ No newline at end of file diff --git a/SurveyLib.Core/Repositories/IQuestionRepository.cs b/SurveyLib.Core/Repositories/IQuestionRepository.cs index d8ecf6c..52c6d39 100644 --- a/SurveyLib.Core/Repositories/IQuestionRepository.cs +++ b/SurveyLib.Core/Repositories/IQuestionRepository.cs @@ -4,6 +4,5 @@ namespace SurveyLib.Core.Repositories; public interface IQuestionRepository : IGenericRepository { - Task GetWithAnswersAsync(int questionId); - Task> GetBySurveyIdAsync(int surveyId); + } \ No newline at end of file diff --git a/SurveyLib.Core/Repositories/ISurveyRepository.cs b/SurveyLib.Core/Repositories/ISurveyRepository.cs index a918cd3..bccec00 100644 --- a/SurveyLib.Core/Repositories/ISurveyRepository.cs +++ b/SurveyLib.Core/Repositories/ISurveyRepository.cs @@ -4,7 +4,5 @@ namespace SurveyLib.Core.Repositories; public interface ISurveyRepository : IGenericRepository { - Task GetWithQuestionsAsync(int surveyId); - Task GetWithCompletionsAsync(int surveyId); - 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 a1d6436..c57a470 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs @@ -41,14 +41,4 @@ public class AnswerRepository : IAnswerRepository _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 index 30e6164..1ef5ab6 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs @@ -41,9 +41,4 @@ public class CompletionRepository : ICompletionRepository _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 index e107973..b4ca354 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs @@ -41,15 +41,4 @@ public class QuestionRepository : IQuestionRepository _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 index 2890053..b4a214e 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs @@ -41,21 +41,4 @@ public class SurveyRepository : ISurveyRepository _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 From 3e570decd723399ce4744ee4cad7f9532adee65f Mon Sep 17 00:00:00 2001 From: shept Date: Wed, 16 Apr 2025 17:57:03 +0500 Subject: [PATCH 17/18] minor repositories changes --- SurveyLib.Core/Repositories/IQuestionRepository.cs | 2 +- .../Repositories/QuestionRepository.cs | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/SurveyLib.Core/Repositories/IQuestionRepository.cs b/SurveyLib.Core/Repositories/IQuestionRepository.cs index 52c6d39..9afe14b 100644 --- a/SurveyLib.Core/Repositories/IQuestionRepository.cs +++ b/SurveyLib.Core/Repositories/IQuestionRepository.cs @@ -4,5 +4,5 @@ namespace SurveyLib.Core.Repositories; public interface IQuestionRepository : IGenericRepository { - + public IEnumerable GetQuestionsBySurveyId(int surveyId); } \ No newline at end of file diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs index b4ca354..425fc27 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs @@ -41,4 +41,9 @@ public class QuestionRepository : IQuestionRepository _context.Questions.Remove(entity); await _context.SaveChangesAsync(); } + + public IEnumerable GetQuestionsBySurveyId(int surveyId) + { + return _context.Questions.Where(q => q.SurveyId == surveyId); + } } \ No newline at end of file From 6e8d6cebdd834a596d3f05e4774cf9ab6abeb06e Mon Sep 17 00:00:00 2001 From: shept Date: Wed, 16 Apr 2025 18:04:55 +0500 Subject: [PATCH 18/18] added basic services --- .../Repositories/IQuestionRepository.cs | 2 +- SurveyLib.Core/Services/IQuestionService.cs | 7 +-- SurveyLib.Core/Services/ISurveyService.cs | 10 ++-- .../Repositories/QuestionRepository.cs | 4 +- .../Services/QuestionService.cs | 43 +++++++++++++++++ .../Services/SurveyService.cs | 46 +++++++++++++++++++ 6 files changed, 100 insertions(+), 12 deletions(-) create mode 100644 SurveyLib.Infrastructure.EFCore/Services/QuestionService.cs create mode 100644 SurveyLib.Infrastructure.EFCore/Services/SurveyService.cs diff --git a/SurveyLib.Core/Repositories/IQuestionRepository.cs b/SurveyLib.Core/Repositories/IQuestionRepository.cs index 9afe14b..3f30cf5 100644 --- a/SurveyLib.Core/Repositories/IQuestionRepository.cs +++ b/SurveyLib.Core/Repositories/IQuestionRepository.cs @@ -4,5 +4,5 @@ namespace SurveyLib.Core.Repositories; public interface IQuestionRepository : IGenericRepository { - public IEnumerable GetQuestionsBySurveyId(int surveyId); + public Task> GetQuestionsBySurveyId(int surveyId); } \ No newline at end of file diff --git a/SurveyLib.Core/Services/IQuestionService.cs b/SurveyLib.Core/Services/IQuestionService.cs index af0d6d5..c0417aa 100644 --- a/SurveyLib.Core/Services/IQuestionService.cs +++ b/SurveyLib.Core/Services/IQuestionService.cs @@ -4,8 +4,9 @@ namespace SurveyLib.Core.Services; public interface IQuestionService { - Task AddQuestionAsync(QuestionBase question); - Task UpdateQuestionAsync(QuestionBase question); - Task DeleteQuestionAsync(int id); + Task AddQuestionAsync(QuestionBase question); + Task UpdateQuestionAsync(QuestionBase question); + Task DeleteQuestionAsync(int id); + Task GetQuestionByIdAsync(int id); Task> GetQuestionsBySurveyIdAsync(int surveyId); } \ No newline at end of file diff --git a/SurveyLib.Core/Services/ISurveyService.cs b/SurveyLib.Core/Services/ISurveyService.cs index 6ec6d55..c6d0b29 100644 --- a/SurveyLib.Core/Services/ISurveyService.cs +++ b/SurveyLib.Core/Services/ISurveyService.cs @@ -4,11 +4,9 @@ namespace SurveyLib.Core.Services; public interface ISurveyService { - Task AddSurveyAsync(Survey survey); - Task UpdateSurveyAsync(Survey survey); - Task DeleteSurveyAsync(int id); + Task AddSurveyAsync(Survey survey); + Task UpdateSurveyAsync(Survey survey); + Task DeleteSurveyAsync(int id); Task> GetSurveysAsync(); - Task GetSurveyByIdAsync(int id); - Task GetSurveyWithQuestionsByIdAsync(int id); - Task GetSurveyWithAnswersByIdAsync(int id); + Task GetSurveyAsync(int id); } \ No newline at end of file diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs index 425fc27..59ca2f7 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs @@ -42,8 +42,8 @@ public class QuestionRepository : IQuestionRepository await _context.SaveChangesAsync(); } - public IEnumerable GetQuestionsBySurveyId(int surveyId) + public async Task> GetQuestionsBySurveyId(int surveyId) { - return _context.Questions.Where(q => q.SurveyId == surveyId); + return await _context.Questions.Where(q => q.SurveyId == surveyId).ToListAsync(); } } \ No newline at end of file diff --git a/SurveyLib.Infrastructure.EFCore/Services/QuestionService.cs b/SurveyLib.Infrastructure.EFCore/Services/QuestionService.cs new file mode 100644 index 0000000..795546d --- /dev/null +++ b/SurveyLib.Infrastructure.EFCore/Services/QuestionService.cs @@ -0,0 +1,43 @@ +using SurveyLib.Core.Models; +using SurveyLib.Core.Repositories; +using SurveyLib.Core.Services; + +namespace SurveyLib.Infrastructure.EFCore.Services; + +public class QuestionService : IQuestionService +{ + private readonly IQuestionRepository _questionRepository; + + public QuestionService(IQuestionRepository questionRepository) + { + _questionRepository = questionRepository; + } + + public async Task AddQuestionAsync(QuestionBase question) + { + await _questionRepository.AddAsync(question); + } + + public async Task UpdateQuestionAsync(QuestionBase question) + { + await _questionRepository.UpdateAsync(question); + } + + public async Task DeleteQuestionAsync(int id) + { + var question = await GetQuestionByIdAsync(id); + + await _questionRepository.DeleteAsync(question); + } + + public async Task GetQuestionByIdAsync(int id) + { + var question = await _questionRepository.GetByIdAsync(id) ?? throw new NullReferenceException(); + return question; + } + + public async Task> GetQuestionsBySurveyIdAsync(int surveyId) + { + return await _questionRepository.GetQuestionsBySurveyId(surveyId); + } +} \ No newline at end of file diff --git a/SurveyLib.Infrastructure.EFCore/Services/SurveyService.cs b/SurveyLib.Infrastructure.EFCore/Services/SurveyService.cs new file mode 100644 index 0000000..52133db --- /dev/null +++ b/SurveyLib.Infrastructure.EFCore/Services/SurveyService.cs @@ -0,0 +1,46 @@ +using SurveyLib.Core.Models; +using SurveyLib.Core.Repositories; +using SurveyLib.Core.Services; + +namespace SurveyLib.Infrastructure.EFCore.Services; + +public class SurveyService : ISurveyService +{ + private readonly ISurveyRepository _surveyRepository; + + public SurveyService(ISurveyRepository surveyRepository) + { + _surveyRepository = surveyRepository; + } + + public async Task AddSurveyAsync(Survey survey) + { + await _surveyRepository.AddAsync(survey); + } + + public async Task UpdateSurveyAsync(Survey survey) + { + await _surveyRepository.UpdateAsync(survey); + } + + public async Task DeleteSurveyAsync(int id) + { + var survey = await _surveyRepository.GetByIdAsync(id); + if (survey == null) + { + throw new NullReferenceException($"Survey with id: {id} was not found"); + } + + await _surveyRepository.DeleteAsync(survey); + } + + public async Task> GetSurveysAsync() + { + return await _surveyRepository.GetAllAsync(); + } + + public async Task GetSurveyAsync(int id) + { + return await _surveyRepository.GetByIdAsync(id); + } +} \ No newline at end of file