Implement story integration backend services:
- Create StoryService (Application/Services/StoryService.cs) with full CRUD operations
- Create StoryGenerationService (Application/Services/StoryGenerationService.cs) for AI-powered story generation
- Uses IMistralService for text generation
- Uses ITtsService for audio generation
- Splits stories into segments per lesson
- Create StoryUnlockService (Application/Services/StoryUnlockService.cs) for progress management
- Handles lesson completion → story segment unlocking
- Create StoryController (Presentation/Controllers/StoryController.cs) with 12 endpoints:
- GET /api/story/levels - list levels with stories
- GET /api/story/levels/{levelId}/segments - get segments for level
- GET /api/story/segments/{id} - get specific segment
- POST /api/story/segments - create segment (Admin)
- PUT /api/story/segments/{id} - update segment (Admin)
- DELETE /api/story/segments/{id} - delete segment (Admin)
- POST /api/story/levels/{levelId}/generate - generate story with AI (Admin)
- POST /api/story/segments/{segmentId}/audio - generate audio (Admin)
- GET /api/story/levels/{levelId}/progress - get user progress
- POST /api/story/segments/{segmentId}/complete - mark as completed
- GET /api/story/levels/{levelId}/next - get next segment
- GET /api/story/segments/{segmentId}/unlocked - check if unlocked
- GET /api/story/segments/{segmentId}/audio - get audio URL
- GET /api/story/levels/{levelId}/lessons-with-stories - get lessons with story status
- Register all services in Program.cs DI container
- Update feature document to reflect Phase 2 completion
Next: Phase 3 - AI Integration (unit tests for services)
Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
127 lines
4.1 KiB
C#
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 ?? 0;
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|