Merge branch 'unstable' into 'main'

Minor fixes

See merge request internship-2025/survey-webapp/surveylib!14
This commit is contained in:
Вячеслав 2025-05-20 11:03:21 +00:00
commit c477e8cadd
13 changed files with 80 additions and 26 deletions

View file

@ -7,6 +7,7 @@ public class Survey
public string Description { get; set; } public string Description { get; set; }
public int? CreatedBy { get; set; } public int? CreatedBy { get; set; }
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public ICollection<QuestionBase> Questions { get; set; } public ICollection<QuestionBase> Questions { get; set; }
public ICollection<Completion> Completions { get; set; } public ICollection<Completion> Completions { get; set; }

View file

@ -4,5 +4,5 @@ namespace SurveyLib.Core.Repositories;
public interface IAnswerRepository : IGenericRepository<Answer> public interface IAnswerRepository : IGenericRepository<Answer>
{ {
Task DeleteAsync(int questionId, int completionId);
} }

View file

@ -0,0 +1,8 @@
using SurveyLib.Core.Models;
namespace SurveyLib.Core.Repositories;
public interface IAnswerVariantsRepository : IGenericRepository<AnswerVariant>
{
Task<IEnumerable<AnswerVariant>> GetAnswerVariantsByQuestionIdAsync(int questionId);
}

View file

@ -8,5 +8,5 @@ public interface IGenericRepository<T> where T : class
Task<IEnumerable<T>> GetAllAsync(); Task<IEnumerable<T>> GetAllAsync();
Task AddAsync(T entity); Task AddAsync(T entity);
Task UpdateAsync(T entity); Task UpdateAsync(T entity);
Task DeleteAsync(T entity); Task DeleteAsync(int id);
} }

View file

@ -13,6 +13,8 @@ public class SurveyDbContext : DbContext
public DbSet<MultipleAnswerQuestion> MultipleAnswerQuestions { get; set; } public DbSet<MultipleAnswerQuestion> MultipleAnswerQuestions { get; set; }
public DbSet<TextQuestion> TextQuestions { get; set; } public DbSet<TextQuestion> TextQuestions { get; set; }
public DbSet<AnswerVariant> AnswerVariants { get; set; }
public DbSet<Completion> Completions { get; set; } public DbSet<Completion> Completions { get; set; }
public DbSet<Answer> Answers { get; set; } public DbSet<Answer> Answers { get; set; }

View file

@ -36,9 +36,15 @@ public class AnswerRepository : IAnswerRepository
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
} }
public async Task DeleteAsync(Answer entity) public async Task DeleteAsync(int id)
{ {
_context.Answers.Remove(entity); throw new NotImplementedException();
}
public async Task DeleteAsync(int questionId, int completionId)
{
await _context.Answers.Where(a => a.QuestionId == questionId && a.CompletionId == completionId)
.ExecuteDeleteAsync();
await _context.SaveChangesAsync(); 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 AnswerVariantsRepository : IAnswerVariantsRepository
{
private readonly SurveyDbContext _context;
public AnswerVariantsRepository(SurveyDbContext context)
{
_context = context;
}
public async Task<AnswerVariant?> GetByIdAsync(int id)
{
return await _context.AnswerVariants.FindAsync(id);
}
public async Task<IEnumerable<AnswerVariant>> GetAllAsync()
{
return await _context.AnswerVariants.ToListAsync();
}
public async Task AddAsync(AnswerVariant entity)
{
await _context.AnswerVariants.AddAsync(entity);
await _context.SaveChangesAsync();
}
public async Task UpdateAsync(AnswerVariant entity)
{
_context.AnswerVariants.Update(entity);
await _context.SaveChangesAsync();
}
public async Task DeleteAsync(int id)
{
await _context.AnswerVariants.Where(av => av.Id == id).ExecuteDeleteAsync();
await _context.SaveChangesAsync();
}
public async Task<IEnumerable<AnswerVariant>> GetAnswerVariantsByQuestionIdAsync(int questionId)
{
return await _context.AnswerVariants.Where(av => av.QuestionId == questionId).ToListAsync();
}
}

View file

@ -36,9 +36,9 @@ public class CompletionRepository : ICompletionRepository
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
} }
public async Task DeleteAsync(Completion entity) public async Task DeleteAsync(int id)
{ {
_context.Completions.Remove(entity); await _context.Completions.Where(c => c.Id == id).ExecuteDeleteAsync();
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
} }
} }

View file

@ -36,9 +36,9 @@ public class QuestionRepository : IQuestionRepository
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
} }
public async Task DeleteAsync(QuestionBase entity) public async Task DeleteAsync(int id)
{ {
_context.Questions.Remove(entity); await _context.Questions.Where(q => q.Id == id).ExecuteDeleteAsync();
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
} }

View file

@ -36,9 +36,9 @@ public class SurveyRepository : ISurveyRepository
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
} }
public async Task DeleteAsync(Survey entity) public async Task DeleteAsync(int id)
{ {
_context.Surveys.Remove(entity); await _context.Surveys.Where(s => s.Id == id).ExecuteDeleteAsync();
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
} }

View file

@ -16,8 +16,4 @@
<ProjectReference Include="..\SurveyLib.Core\SurveyLib.Core.csproj" /> <ProjectReference Include="..\SurveyLib.Core\SurveyLib.Core.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Services\" />
</ItemGroup>
</Project> </Project>

View file

@ -25,9 +25,7 @@ public class QuestionService : IQuestionService
public async Task DeleteQuestionAsync(int id) public async Task DeleteQuestionAsync(int id)
{ {
var question = await GetQuestionByIdAsync(id); await _questionRepository.DeleteAsync(id);
await _questionRepository.DeleteAsync(question);
} }
public async Task<QuestionBase> GetQuestionByIdAsync(int id) public async Task<QuestionBase> GetQuestionByIdAsync(int id)

View file

@ -25,13 +25,7 @@ public class SurveyService : ISurveyService
public async Task DeleteSurveyAsync(int id) public async Task DeleteSurveyAsync(int id)
{ {
var survey = await _surveyRepository.GetByIdAsync(id); await _surveyRepository.DeleteAsync(id);
if (survey == null)
{
throw new NullReferenceException($"Survey with id: {id} was not found");
}
await _surveyRepository.DeleteAsync(survey);
} }
public async Task<IEnumerable<Survey>> GetSurveysAsync() public async Task<IEnumerable<Survey>> GetSurveysAsync()