- Add QuizQuestion and QuizOption domain entities with QuestionType enum - Add IQuizQuestionRepository and IQuizOptionRepository interfaces - Add QuizQuestionRepository and QuizOptionRepository EF Core implementations - Add QuizQuestionService with CRUD, quiz submission, and statistics - Add QuizQuestionsController with comprehensive REST API endpoints - Add QuizQuestionDto and related DTOs for API communication - Add EF Core migration (20260612050652_AddQuizQuestionTables) for QuizQuestion and QuizOption - Add seed data with sample quiz questions for Greetings, Numbers, Grammar lessons - Update AppDbContext with DbSets and entity configurations - Update Program.cs with migration retry logic for Docker - Update docker-compose.yml with SeedDatabase configuration - Add unit tests for QuizQuestionService Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
142 lines
5.1 KiB
C#
142 lines
5.1 KiB
C#
using GermanApp.Domain.Entities;
|
|
using GermanApp.Domain.Interfaces;
|
|
using GermanApp.Infrastructure.Data.DbContext;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace GermanApp.Infrastructure.Data.Repositories;
|
|
|
|
/// <summary>
|
|
/// Entity Framework Core implementation of IQuizQuestionRepository.
|
|
/// This is part of the Infrastructure layer.
|
|
/// </summary>
|
|
public class QuizQuestionRepository : IQuizQuestionRepository
|
|
{
|
|
private readonly AppDbContext _context;
|
|
|
|
public QuizQuestionRepository(AppDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<QuizQuestion?> GetByIdAsync(int id, CancellationToken cancellationToken = default)
|
|
{
|
|
return await _context.QuizQuestions
|
|
.Include(q => q.Lesson)
|
|
.Include(q => q.Options.OrderBy(o => o.Order))
|
|
.FirstOrDefaultAsync(q => q.Id == id, cancellationToken);
|
|
}
|
|
|
|
public async Task<IReadOnlyList<QuizQuestion>> GetAllAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
return await _context.QuizQuestions
|
|
.Include(q => q.Lesson)
|
|
.Include(q => q.Options)
|
|
.AsNoTracking()
|
|
.ToListAsync(cancellationToken);
|
|
}
|
|
|
|
public async Task<QuizQuestion> AddAsync(QuizQuestion entity, CancellationToken cancellationToken = default)
|
|
{
|
|
await _context.QuizQuestions.AddAsync(entity, cancellationToken);
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
return entity;
|
|
}
|
|
|
|
public async Task UpdateAsync(QuizQuestion entity, CancellationToken cancellationToken = default)
|
|
{
|
|
_context.QuizQuestions.Update(entity);
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
}
|
|
|
|
public async Task DeleteAsync(QuizQuestion entity, CancellationToken cancellationToken = default)
|
|
{
|
|
_context.QuizQuestions.Remove(entity);
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
}
|
|
|
|
public async Task<bool> ExistsAsync(int id, CancellationToken cancellationToken = default)
|
|
{
|
|
return await _context.QuizQuestions
|
|
.AnyAsync(q => q.Id == id, cancellationToken);
|
|
}
|
|
|
|
public async Task<IReadOnlyList<QuizQuestion>> GetByLessonAsync(int lessonId, CancellationToken cancellationToken = default)
|
|
{
|
|
return await _context.QuizQuestions
|
|
.Include(q => q.Lesson)
|
|
.Include(q => q.Options.OrderBy(o => o.Order))
|
|
.Where(q => q.LessonId == lessonId)
|
|
.OrderBy(q => q.Order)
|
|
.AsNoTracking()
|
|
.ToListAsync(cancellationToken);
|
|
}
|
|
|
|
public async Task<IReadOnlyList<QuizQuestion>> GetAllOrderedAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
return await _context.QuizQuestions
|
|
.Include(q => q.Lesson)
|
|
.Include(q => q.Options.OrderBy(o => o.Order))
|
|
.OrderBy(q => q.Lesson.Order)
|
|
.ThenBy(q => q.Order)
|
|
.AsNoTracking()
|
|
.ToListAsync(cancellationToken);
|
|
}
|
|
|
|
public async Task<IReadOnlyList<QuizQuestion>> GetActiveByLessonAsync(int lessonId, CancellationToken cancellationToken = default)
|
|
{
|
|
return await _context.QuizQuestions
|
|
.Include(q => q.Lesson)
|
|
.Include(q => q.Options.OrderBy(o => o.Order))
|
|
.Where(q => q.LessonId == lessonId && q.IsActive)
|
|
.OrderBy(q => q.Order)
|
|
.AsNoTracking()
|
|
.ToListAsync(cancellationToken);
|
|
}
|
|
|
|
public async Task<IReadOnlyList<QuizQuestion>> GetRandomQuestionsForLessonAsync(int lessonId, int count, CancellationToken cancellationToken = default)
|
|
{
|
|
return await _context.QuizQuestions
|
|
.Include(q => q.Lesson)
|
|
.Include(q => q.Options.OrderBy(o => o.Order))
|
|
.Where(q => q.LessonId == lessonId && q.IsActive)
|
|
.OrderBy(q => Guid.NewGuid()) // Random ordering
|
|
.Take(count)
|
|
.AsNoTracking()
|
|
.ToListAsync(cancellationToken);
|
|
}
|
|
|
|
public async Task<IReadOnlyList<QuizQuestion>> GetByDifficultyAsync(int difficulty, CancellationToken cancellationToken = default)
|
|
{
|
|
return await _context.QuizQuestions
|
|
.Include(q => q.Lesson)
|
|
.Include(q => q.Options)
|
|
.Where(q => q.Difficulty == difficulty)
|
|
.OrderBy(q => q.Order)
|
|
.AsNoTracking()
|
|
.ToListAsync(cancellationToken);
|
|
}
|
|
|
|
public async Task<IReadOnlyList<QuizQuestion>> GetByTypeAsync(QuestionType type, CancellationToken cancellationToken = default)
|
|
{
|
|
return await _context.QuizQuestions
|
|
.Include(q => q.Lesson)
|
|
.Include(q => q.Options)
|
|
.Where(q => q.Type == type)
|
|
.OrderBy(q => q.Order)
|
|
.AsNoTracking()
|
|
.ToListAsync(cancellationToken);
|
|
}
|
|
|
|
public async Task<bool> OrderExistsInLessonAsync(int lessonId, int order, int? excludeId = null, CancellationToken cancellationToken = default)
|
|
{
|
|
var query = _context.QuizQuestions
|
|
.Where(q => q.LessonId == lessonId && q.Order == order);
|
|
|
|
if (excludeId.HasValue)
|
|
{
|
|
query = query.Where(q => q.Id != excludeId.Value);
|
|
}
|
|
|
|
return await query.AnyAsync(cancellationToken);
|
|
}
|
|
}
|