DeutschLernen/GermanApp/Infrastructure/Data/Repositories/StoryProgressRepository.cs
Mistral Vibe 01f6a1fd30 test(backend): add Phase 3 unit tests for Story Integration
- StoryServiceTests.cs: 536 lines, comprehensive tests for StoryService
- StoryGenerationServiceTests.cs: 469 lines, tests for AI story generation
- StoryUnlockServiceTests.cs: 292 lines, tests for story unlock logic
- StoryRepositoryTests.cs: 120 lines, tests for EF Core repository
- StoryProgressRepositoryTests.cs: 74 lines, tests for progress repository
- AppDbContext.cs: Made DbSet properties virtual for Moq compatibility
- Fixed return types in StoryRepository and StoryProgressRepository
- Updated docs/features/story-integration.md for Phase 3 completion
- All 324 unit tests pass

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-06-13 14:23:54 +02:00

127 lines
4.1 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
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 IStoryProgressRepository.
/// This is part of the Infrastructure layer.
/// </summary>
public class StoryProgressRepository : IStoryProgressRepository
{
private readonly AppDbContext _context;
public StoryProgressRepository(AppDbContext context)
{
_context = context;
}
public async Task<IReadOnlyList<StoryProgress>> GetByUserAndLevelAsync(
int userId,
int levelId,
CancellationToken cancellationToken = default)
{
return await _context.StoryProgress
.Where(p => p.UserId == userId && p.LevelId == levelId)
.Include(p => p.StorySegment)
.OrderBy(p => p.StorySegment != null ? p.StorySegment.Order : 0)
.AsNoTracking()
.ToListAsync(cancellationToken);
}
public async Task<StoryProgress?> GetByUserAndSegmentAsync(
int userId,
int storySegmentId,
CancellationToken cancellationToken = default)
{
return await _context.StoryProgress
.FirstOrDefaultAsync(
p => p.UserId == userId && p.StorySegmentId == storySegmentId,
cancellationToken);
}
public async Task<int> GetHighestUnlockedOrderAsync(
int userId,
int levelId,
CancellationToken cancellationToken = default)
{
var highestOrder = await _context.StoryProgress
.Where(p => p.UserId == userId && p.LevelId == levelId)
.Include(p => p.StorySegment)
.Select(p => p.StorySegment != null ? p.StorySegment.Order : 0)
.MaxAsync(cancellationToken);
return highestOrder;
}
public async Task<bool> IsSegmentUnlockedAsync(
int userId,
int storySegmentId,
CancellationToken cancellationToken = default)
{
return await _context.StoryProgress
.AnyAsync(
p => p.UserId == userId && p.StorySegmentId == storySegmentId,
cancellationToken);
}
public async Task<bool> IsSegmentCompletedAsync(
int userId,
int storySegmentId,
CancellationToken cancellationToken = default)
{
return await _context.StoryProgress
.AnyAsync(
p => p.UserId == userId
&& p.StorySegmentId == storySegmentId
&& p.IsCompleted,
cancellationToken);
}
public async Task<StoryProgress> AddAsync(StoryProgress progress, CancellationToken cancellationToken = default)
{
await _context.StoryProgress.AddAsync(progress, cancellationToken);
await _context.SaveChangesAsync(cancellationToken);
return progress;
}
public async Task UpdateAsync(StoryProgress progress, CancellationToken cancellationToken = default)
{
_context.StoryProgress.Update(progress);
await _context.SaveChangesAsync(cancellationToken);
}
public async Task<int> GetTotalSegmentsForLevelAsync(int levelId, CancellationToken cancellationToken = default)
{
return await _context.StorySegments
.CountAsync(s => s.LevelId == levelId && s.IsActive, cancellationToken);
}
public async Task<int> GetUnlockedCountAsync(
int userId,
int levelId,
CancellationToken cancellationToken = default)
{
return await _context.StoryProgress
.CountAsync(
p => p.UserId == userId && p.LevelId == levelId,
cancellationToken);
}
public async Task<int> GetCompletedCountAsync(
int userId,
int levelId,
CancellationToken cancellationToken = default)
{
return await _context.StoryProgress
.CountAsync(
p => p.UserId == userId && p.LevelId == levelId && p.IsCompleted,
cancellationToken);
}
}