Merge branch 'unstable' into 'main'

A lot of things have changed...

See merge request internship-2025/survey-webapp/surveylib!7
This commit is contained in:
Вячеслав 2025-04-16 13:09:44 +00:00
commit 7c8da5995e
23 changed files with 400 additions and 9 deletions

View file

@ -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; }
}

View file

@ -6,6 +6,8 @@ public class QuestionBase
public int SurveyId { get; set; } public int SurveyId { get; set; }
public string Title { get; set; } public string Title { get; set; }
public string Discriminator { get; set; }
public Survey Survey { get; set; } public Survey Survey { get; set; }
public ICollection<Answer> Answers { get; set; } public ICollection<Answer> Answers { get; set; }
} }

View file

@ -0,0 +1,6 @@
namespace SurveyLib.Core.Models.QuestionVariants;
public class MultipleAnswerQuestion : QuestionBase
{
public ICollection<AnswerVariant> AnswerVariants { get; set; }
}

View file

@ -0,0 +1,6 @@
namespace SurveyLib.Core.Models.QuestionVariants;
public class SingleAnswerQuestion : QuestionBase
{
public ICollection<AnswerVariant> AnswerVariants { get; set; }
}

View file

@ -0,0 +1,5 @@
namespace SurveyLib.Core.Models.QuestionVariants;
public class TextQuestion : QuestionBase
{
}

View file

@ -4,6 +4,5 @@ namespace SurveyLib.Core.Repositories;
public interface IAnswerRepository : IGenericRepository<Answer> public interface IAnswerRepository : IGenericRepository<Answer>
{ {
Task<IEnumerable<Answer>> GetByAttemptIdAsync(int attemptId);
Task<IEnumerable<Answer>> GetByQuestionIdAsync(int questionId);
} }

View file

@ -4,5 +4,5 @@ namespace SurveyLib.Core.Repositories;
public interface ICompletionRepository : IGenericRepository<Completion> public interface ICompletionRepository : IGenericRepository<Completion>
{ {
Task<IEnumerable<Completion>> GetBySurveyIdAsync(int surveyId);
} }

View file

@ -1,8 +1,10 @@
using SurveyLib.Core.Models;
namespace SurveyLib.Core.Repositories; namespace SurveyLib.Core.Repositories;
public interface IGenericRepository<T> where T : class public interface IGenericRepository<T> where T : class
{ {
Task<T>? GetByIdAsync(int id); Task<T?> GetByIdAsync(int id);
Task<IEnumerable<T>> GetAllAsync(); Task<IEnumerable<T>> GetAllAsync();
Task AddAsync(T entity); Task AddAsync(T entity);
Task UpdateAsync(T entity); Task UpdateAsync(T entity);

View file

@ -4,6 +4,5 @@ namespace SurveyLib.Core.Repositories;
public interface IQuestionRepository : IGenericRepository<QuestionBase> public interface IQuestionRepository : IGenericRepository<QuestionBase>
{ {
Task<QuestionBase?> GetWithAnswersAsync(int questionId); public Task<IEnumerable<QuestionBase>> GetQuestionsBySurveyId(int surveyId);
Task<IEnumerable<QuestionBase>> GetBySurveyIdAsync(int surveyId);
} }

View file

@ -4,7 +4,5 @@ namespace SurveyLib.Core.Repositories;
public interface ISurveyRepository : IGenericRepository<Survey> public interface ISurveyRepository : IGenericRepository<Survey>
{ {
Task<Survey?> GetWithQuestionsAsync(int surveyId);
Task<Survey?> GetWithCompletionsAsync(int surveyId);
Task<Survey?> FindByTitleAsync(string title);
} }

View file

@ -0,0 +1,12 @@
using SurveyLib.Core.Models;
namespace SurveyLib.Core.Services;
public interface IAnswerService
{
Task<bool> AddAnswerAsync(Answer answer);
Task<bool> UpdateAnswerAsync(Answer answer);
Task<bool> DeleteAnswerAsync(int id);
Task<IEnumerable<Answer>> GetAnswersByCompletionIdAsync(int completionId);
Task<IEnumerable<Answer>> GetAnswersByQuestionIdAsync(int questionId);
}

View file

@ -0,0 +1,11 @@
using SurveyLib.Core.Models;
namespace SurveyLib.Core.Services;
public interface ICompletionService
{
Task<bool> AddCompletionAsync(Completion completion);
Task<bool> UpdateCompletionAsync(Completion completion);
Task<bool> DeleteCompletionAsync(int id);
Task<IEnumerable<Completion>> GetCompletionsBySurveyIdAsync(int surveyId);
}

View file

@ -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<QuestionBase> GetQuestionByIdAsync(int id);
Task<IEnumerable<QuestionBase>> GetQuestionsBySurveyIdAsync(int surveyId);
}

View file

@ -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<IEnumerable<Survey>> GetSurveysAsync();
Task<Survey?> GetSurveyAsync(int id);
}

View file

@ -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<Survey> Surveys { get; set; }
public DbSet<QuestionBase> Questions { get; set; }
public DbSet<SingleAnswerQuestion> SingleAnswerQuestions { get; set; }
public DbSet<MultipleAnswerQuestion> MultipleAnswerQuestions { get; set; }
public DbSet<TextQuestion> TextQuestions { get; set; }
public DbSet<Completion> Completions { get; set; }
public DbSet<Answer> Answers { get; set; }
public SurveyDbContext(DbContextOptions<SurveyDbContext> options) : base(options)
{
}
}

View file

@ -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<Answer?> GetByIdAsync(int id)
{
throw new NotImplementedException();
}
public async Task<IEnumerable<Answer>> 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();
}
}

View file

@ -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<Completion?> GetByIdAsync(int id)
{
return await _context.Completions.FindAsync(id);
}
public async Task<IEnumerable<Completion>> 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();
}
}

View file

@ -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<QuestionBase?> GetByIdAsync(int id)
{
return await _context.Questions.FindAsync(id);
}
public async Task<IEnumerable<QuestionBase>> 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<IEnumerable<QuestionBase>> GetQuestionsBySurveyId(int surveyId)
{
return await _context.Questions.Where(q => q.SurveyId == surveyId).ToListAsync();
}
}

View file

@ -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<Survey?> GetByIdAsync(int id)
{
return await _context.Surveys.FindAsync(id);
}
public async Task<IEnumerable<Survey>> GetAllAsync()
{
return await _context.Surveys.ToListAsync();
}
public async Task AddAsync(Survey entity)
{
await _context.Surveys.AddAsync(entity);
await _context.SaveChangesAsync();
}
public async Task UpdateAsync(Survey entity)
{
_context.Surveys.Update(entity);
await _context.SaveChangesAsync();
}
public async Task DeleteAsync(Survey entity)
{
_context.Surveys.Remove(entity);
await _context.SaveChangesAsync();
}
}

View file

@ -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<QuestionBase> GetQuestionByIdAsync(int id)
{
var question = await _questionRepository.GetByIdAsync(id) ?? throw new NullReferenceException();
return question;
}
public async Task<IEnumerable<QuestionBase>> GetQuestionsBySurveyIdAsync(int surveyId)
{
return await _questionRepository.GetQuestionsBySurveyId(surveyId);
}
}

View file

@ -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<IEnumerable<Survey>> GetSurveysAsync()
{
return await _surveyRepository.GetAllAsync();
}
public async Task<Survey?> GetSurveyAsync(int id)
{
return await _surveyRepository.GetByIdAsync(id);
}
}

View file

@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SurveyLib.Core\SurveyLib.Core.csproj" />
</ItemGroup>
</Project>

View file

@ -2,6 +2,8 @@
Microsoft Visual Studio Solution File, Format Version 12.00 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}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SurveyLib.Core", "SurveyLib.Core\SurveyLib.Core.csproj", "{89972F62-9976-41F2-8259-428F4F196AFF}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SurveyLib.Infrastructure.EFCore", "SurveyLib.Infrastructure.EFCore\SurveyLib.Infrastructure.EFCore.csproj", "{57B8F7D6-53A6-41DF-961A-D5E3E7186DB7}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{89972F62-9976-41F2-8259-428F4F196AFF}.Release|Any CPU.Build.0 = 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 EndGlobalSection
EndGlobal EndGlobal