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/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/QuestionVariants/MultipleAnswerQuestion.cs b/SurveyLib.Core/Models/QuestionVariants/MultipleAnswerQuestion.cs new file mode 100644 index 0000000..d2eb5b0 --- /dev/null +++ b/SurveyLib.Core/Models/QuestionVariants/MultipleAnswerQuestion.cs @@ -0,0 +1,6 @@ +namespace SurveyLib.Core.Models.QuestionVariants; + +public class MultipleAnswerQuestion : QuestionBase +{ + public ICollection AnswerVariants { get; set; } +} \ No newline at end of file diff --git a/SurveyLib.Core/Models/QuestionVariants/SingleAnswerQuestion.cs b/SurveyLib.Core/Models/QuestionVariants/SingleAnswerQuestion.cs new file mode 100644 index 0000000..de1bb01 --- /dev/null +++ b/SurveyLib.Core/Models/QuestionVariants/SingleAnswerQuestion.cs @@ -0,0 +1,6 @@ +namespace SurveyLib.Core.Models.QuestionVariants; + +public class SingleAnswerQuestion : QuestionBase +{ + public ICollection AnswerVariants { get; set; } +} \ No newline at end of file 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/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/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/IQuestionRepository.cs b/SurveyLib.Core/Repositories/IQuestionRepository.cs index d8ecf6c..3f30cf5 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); + public Task> GetQuestionsBySurveyId(int surveyId); } \ No newline at end of file diff --git a/SurveyLib.Core/Repositories/ISurveyRepository.cs b/SurveyLib.Core/Repositories/ISurveyRepository.cs index 795c26d..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.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..c0417aa --- /dev/null +++ b/SurveyLib.Core/Services/IQuestionService.cs @@ -0,0 +1,12 @@ +using SurveyLib.Core.Models; + +namespace SurveyLib.Core.Services; + +public interface IQuestionService +{ + 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 new file mode 100644 index 0000000..c6d0b29 --- /dev/null +++ b/SurveyLib.Core/Services/ISurveyService.cs @@ -0,0 +1,12 @@ +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 GetSurveyAsync(int id); +} \ No newline at end of file diff --git a/SurveyLib.Infrastructure.EFCore/Data/SurveyDbContext.cs b/SurveyLib.Infrastructure.EFCore/Data/SurveyDbContext.cs new file mode 100644 index 0000000..d77fb6d --- /dev/null +++ b/SurveyLib.Infrastructure.EFCore/Data/SurveyDbContext.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore; +using SurveyLib.Core.Models; +using SurveyLib.Core.Models.QuestionVariants; + +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 diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs new file mode 100644 index 0000000..c57a470 --- /dev/null +++ b/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs @@ -0,0 +1,44 @@ +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 SurveyDbContext _context; + + public AnswerRepository(SurveyDbContext 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(); + } +} \ 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..1ef5ab6 --- /dev/null +++ b/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs @@ -0,0 +1,44 @@ +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 SurveyDbContext _context; + + public CompletionRepository(SurveyDbContext context) + { + _context = context; + } + + public async Task GetByIdAsync(int id) + { + return await _context.Completions.FindAsync(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(); + } +} \ 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..59ca2f7 --- /dev/null +++ b/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.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 QuestionRepository : IQuestionRepository +{ + private readonly SurveyDbContext _context; + + public QuestionRepository(SurveyDbContext context) + { + _context = context; + } + + public async Task GetByIdAsync(int id) + { + return await _context.Questions.FindAsync(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> GetQuestionsBySurveyId(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..b4a214e --- /dev/null +++ b/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs @@ -0,0 +1,44 @@ +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 SurveyDbContext _context; + + public SurveyRepository(SurveyDbContext context) + { + _context = context; + } + + public async Task GetByIdAsync(int id) + { + return await _context.Surveys.FindAsync(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(); + } +} \ 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 diff --git a/SurveyLib.Infrastructure.EFCore/SurveyLib.Infrastructure.EFCore.csproj b/SurveyLib.Infrastructure.EFCore/SurveyLib.Infrastructure.EFCore.csproj new file mode 100644 index 0000000..e154bcc --- /dev/null +++ b/SurveyLib.Infrastructure.EFCore/SurveyLib.Infrastructure.EFCore.csproj @@ -0,0 +1,19 @@ + + + + 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