DeutschLernen/GermanApp/Presentation/Controllers/LessonsController.cs
Lasse Rune Hansen 50f1b8a8dc feat(backend): Implement mandatory authentication and admin module
- Add Role property to User entity with migration
- Create BootstrapController for first admin user creation
- Remove [AllowAnonymous] from all learning content controllers
- Create AdminController with admin-only endpoints
- Create AdminService for user management
- Create UserReportService for progress reports
- Add UserRepository implementation
- Update AuthService with role support

feat(frontend): Implement authentication system
- Add AuthStore with React context for auth state management
- Create Login, Register, Landing, and Home pages
- Add ProtectedRoute and AdminRoute components
- Create Auth API types and client
- Configure Vite with @/ path alias
- Add comprehensive CSS styles for auth and landing pages

BREAKING CHANGE: All learning content now requires authentication.
Users must register and sign in before accessing lessons, quizzes, and stories.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-06-14 12:42:15 +02:00

194 lines
7.1 KiB
C#

using GermanApp.Application.DTOs;
using GermanApp.Application.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace GermanApp.Presentation.Controllers;
/// <summary>
/// API controller for managing lessons.
/// This is part of the Presentation layer.
/// </summary>
[ApiController]
[Route("api/[controller]")]
[Authorize]
public class LessonsController : ControllerBase
{
private readonly LessonService _lessonService;
private readonly ProgressService _progressService;
public LessonsController(LessonService lessonService, ProgressService progressService)
{
_lessonService = lessonService;
_progressService = progressService;
}
/// <summary>
/// Gets all lessons.
/// </summary>
/// <returns>List of all lessons</returns>
[HttpGet]
public async Task<IActionResult> GetAllLessonsAsync(CancellationToken cancellationToken = default)
{
var lessons = await _lessonService.GetAllLessonsAsync(cancellationToken);
return Ok(lessons);
}
/// <summary>
/// Gets all lessons ordered by level and lesson order.
/// </summary>
/// <returns>List of all lessons in order</returns>
[HttpGet("ordered")]
public async Task<IActionResult> GetAllOrderedLessonsAsync(CancellationToken cancellationToken = default)
{
var lessons = await _lessonService.GetAllOrderedLessonsAsync(cancellationToken);
return Ok(lessons);
}
/// <summary>
/// Gets a specific lesson by its ID.
/// </summary>
/// <param name="id">The lesson ID</param>
/// <returns>The lesson with the specified ID</returns>
[HttpGet("{id}")]
public async Task<IActionResult> GetLessonByIdAsync(int id, CancellationToken cancellationToken = default)
{
var lesson = await _lessonService.GetLessonByIdAsync(id, cancellationToken);
if (lesson == null)
return NotFound();
return Ok(lesson);
}
/// <summary>
/// Gets lessons by level ID.
/// </summary>
/// <param name="levelId">The level ID</param>
/// <returns>List of lessons for the specified level</returns>
[HttpGet("by-level/{levelId}")]
public async Task<IActionResult> GetLessonsByLevelAsync(int levelId, CancellationToken cancellationToken = default)
{
var lessons = await _lessonService.GetLessonsByLevelAsync(levelId, cancellationToken);
return Ok(lessons);
}
/// <summary>
/// Gets beginner lessons (A1 and A2 - levels 1 and 2).
/// </summary>
/// <returns>List of beginner lessons</returns>
[HttpGet("beginner")]
public async Task<IActionResult> GetBeginnerLessonsAsync(CancellationToken cancellationToken = default)
{
var lessons = await _lessonService.GetBeginnerLessonsAsync(cancellationToken);
return Ok(lessons);
}
/// <summary>
/// Gets advanced lessons (B2 and C1 - levels 4 and 5).
/// </summary>
/// <returns>List of advanced lessons</returns>
[HttpGet("advanced")]
public async Task<IActionResult> GetAdvancedLessonsAsync(CancellationToken cancellationToken = default)
{
var lessons = await _lessonService.GetAdvancedLessonsAsync(cancellationToken);
return Ok(lessons);
}
/// <summary>
/// Creates a new lesson.
/// </summary>
/// <param name="dto">The lesson data</param>
/// <returns>The created lesson</returns>
[HttpPost]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> CreateLessonAsync(CreateLessonDto dto, CancellationToken cancellationToken = default)
{
var lesson = await _lessonService.CreateLessonAsync(dto, cancellationToken);
return CreatedAtAction(nameof(GetLessonByIdAsync), new { id = lesson.Id }, lesson);
}
/// <summary>
/// Updates an existing lesson.
/// </summary>
/// <param name="id">The lesson ID</param>
/// <param name="dto">The updated lesson data</param>
/// <returns>The updated lesson</returns>
[HttpPut("{id}")]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> UpdateLessonAsync(int id, UpdateLessonDto dto, CancellationToken cancellationToken = default)
{
var lesson = await _lessonService.UpdateLessonAsync(id, dto, cancellationToken);
if (lesson == null)
return NotFound();
return Ok(lesson);
}
/// <summary>
/// Deletes a lesson by its ID.
/// </summary>
/// <param name="id">The lesson ID</param>
/// <returns>No content on success</returns>
[HttpDelete("{id}")]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> DeleteLessonAsync(int id, CancellationToken cancellationToken = default)
{
var result = await _lessonService.DeleteLessonAsync(id, cancellationToken);
if (!result)
return NotFound();
return NoContent();
}
/// <summary>
/// Gets the first lesson in a level.
/// </summary>
/// <param name="levelId">The level ID</param>
/// <returns>The first lesson in the level</returns>
[HttpGet("first/{levelId}")]
public async Task<IActionResult> GetFirstLessonInLevelAsync(int levelId, CancellationToken cancellationToken = default)
{
var lesson = await _lessonService.GetFirstLessonInLevelAsync(levelId, cancellationToken);
if (lesson == null)
return NotFound();
return Ok(lesson);
}
/// <summary>
/// Gets the next lesson after the specified one.
/// </summary>
/// <param name="currentLessonId">The current lesson ID</param>
/// <returns>The next lesson</returns>
[HttpGet("next/{currentLessonId}")]
public async Task<IActionResult> GetNextLessonAsync(int currentLessonId, CancellationToken cancellationToken = default)
{
var lesson = await _lessonService.GetNextLessonAsync(currentLessonId, cancellationToken);
if (lesson == null)
return NotFound();
return Ok(lesson);
}
/// <summary>
/// Gets lessons that the user can access (based on completion of previous lessons).
/// </summary>
/// <param name="userId">The user ID</param>
/// <returns>List of accessible lessons for the user</returns>
[HttpGet("accessible/{userId}")]
[Authorize]
public async Task<IActionResult> GetAccessibleLessonsAsync(int userId, CancellationToken cancellationToken = default)
{
var lessons = await _lessonService.GetAccessibleLessonsAsync(userId, cancellationToken);
return Ok(lessons);
}
/// <summary>
/// Checks if the next lesson is unlocked for a user.
/// </summary>
/// <param name="userId">The user ID</param>
/// <param name="currentLessonId">The current lesson ID</param>
/// <returns>True if the next lesson is unlocked</returns>
[HttpGet("unlocked/{userId}/{currentLessonId}")]
[Authorize]
public async Task<IActionResult> IsNextLessonUnlockedAsync(int userId, int currentLessonId, CancellationToken cancellationToken = default)
{
var isUnlocked = await _progressService.IsNextLessonUnlockedAsync(userId, currentLessonId, cancellationToken);
return Ok(isUnlocked);
}
}