Merge branch 'features/add-ef-core-infrastructure' into 'unstable'

Added Infrastructures layer for EF Core

See merge request internship-2025/survey-webapp/surveylib!4
This commit is contained in:
Вячеслав 2025-03-14 18:28:45 +00:00
commit 7c30a3599f
9 changed files with 263 additions and 2 deletions

View file

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

View file

@ -6,5 +6,5 @@ public interface ISurveyRepository : IGenericRepository<Survey>
{
Task<Survey?> GetWithQuestionsAsync(int surveyId);
Task<Survey?> GetWithCompletionsAsync(int surveyId);
Task<Survey?> FindByTitleAsync(string title);
Task<IEnumerable<Survey>> FindByTitleAsync(string title);
}

View file

@ -0,0 +1,17 @@
using Microsoft.EntityFrameworkCore;
using SurveyLib.Core.Models;
namespace SurveyLib.Infrastructure.EFCore.Data;
public class DataContext : DbContext
{
public DbSet<Survey> Surveys { get; set; }
public DbSet<QuestionBase> Questions { get; set; }
public DbSet<Completion> Completions { get; set; }
public DbSet<Answer> Answers { get; set; }
public DataContext(DbContextOptions<DataContext> options) : base(options)
{
Database.EnsureCreated();
}
}

View file

@ -0,0 +1,54 @@
using SurveyLib.Core.Models;
using SurveyLib.Core.Repositories;
using Microsoft.EntityFrameworkCore;
using SurveyLib.Infrastructure.EFCore.Data;
namespace SurveyLib.Infrastructure.EFCore.Repositories;
public class AnswerRepository : IAnswerRepository
{
private readonly DataContext _context;
public AnswerRepository(DataContext context)
{
_context = context;
}
public async Task<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();
}
public async Task<IEnumerable<Answer>> GetByAttemptIdAsync(int attemptId)
{
return await _context.Answers.Where(a => a.CompletionId == attemptId).ToListAsync();
}
public async Task<IEnumerable<Answer>> GetByQuestionIdAsync(int questionId)
{
return await _context.Answers.Where(a => a.QuestionId == questionId).ToListAsync();
}
}

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 CompletionRepository : ICompletionRepository
{
private readonly DataContext _context;
public CompletionRepository(DataContext context)
{
_context = context;
}
public async Task<Completion?> GetByIdAsync(int id)
{
return await _context.Completions.FirstOrDefaultAsync(c => c.Id == 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();
}
public async Task<IEnumerable<Completion>> GetBySurveyIdAsync(int surveyId)
{
return await _context.Completions.Where(c => c.SurveyId == surveyId).ToListAsync();
}
}

View file

@ -0,0 +1,55 @@
using Microsoft.EntityFrameworkCore;
using SurveyLib.Core.Models;
using SurveyLib.Core.Repositories;
using SurveyLib.Infrastructure.EFCore.Data;
namespace SurveyLib.Infrastructure.EFCore.Repositories;
public class QuestionRepository : IQuestionRepository
{
private readonly DataContext _context;
public QuestionRepository(DataContext context)
{
_context = context;
}
public async Task<QuestionBase?> GetByIdAsync(int id)
{
return await _context.Questions.FirstOrDefaultAsync(q => q.Id == 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<QuestionBase?> GetWithAnswersAsync(int questionId)
{
return await _context.Questions.Include(q => q.Answers)
.FirstOrDefaultAsync(q => q.Id == questionId);
}
public async Task<IEnumerable<QuestionBase>> GetBySurveyIdAsync(int surveyId)
{
return await _context.Questions.Where(q => q.SurveyId == surveyId).ToListAsync();
}
}

View file

@ -0,0 +1,61 @@
using Microsoft.EntityFrameworkCore;
using SurveyLib.Core.Models;
using SurveyLib.Core.Repositories;
using SurveyLib.Infrastructure.EFCore.Data;
namespace SurveyLib.Infrastructure.EFCore.Repositories;
public class SurveyRepository : ISurveyRepository
{
private readonly DataContext _context;
public SurveyRepository(DataContext context)
{
_context = context;
}
public async Task<Survey?> GetByIdAsync(int id)
{
return await _context.Surveys.FirstOrDefaultAsync(s => s.Id == 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();
}
public async Task<Survey?> GetWithQuestionsAsync(int surveyId)
{
return await _context.Surveys.Include(survey => survey.Questions)
.FirstOrDefaultAsync(s => s.Id == surveyId);
}
public async Task<Survey?> GetWithCompletionsAsync(int surveyId)
{
return await _context.Surveys.Include(survey => survey.Completions)
.FirstOrDefaultAsync(s => s.Id == surveyId);
}
public async Task<IEnumerable<Survey>> FindByTitleAsync(string title)
{
return await _context.Surveys.Where(s => s.Title.ToLower().Contains(title.ToLower())).ToListAsync();
}
}

View file

@ -0,0 +1,17 @@
<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" />
</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
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