DeutschLernen/GermanApp/Presentation/Controllers/LessonsController.cs
Lasse Rune Hansen a6021fb148
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
feat(backend/validation): add FluentValidation for DTOs and update controllers
- Add FluentValidation.AspNetCore package
- Create LevelValidators (CreateLevelValidator, UpdateLevelValidator)
- Create LessonValidators (CreateLessonValidator, UpdateLessonValidator)
- Register FluentValidation in Program.cs with auto-validation
- Update LevelsController and LessonsController to use IActionResult
- Make service methods virtual for Moq testing compatibility
- Add 26 integration tests for LevelsController (13 tests)
- Add 34 integration tests for LessonsController (17 tests)

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-06-09 17:35:14 +02:00

202 lines
7.2 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]
[AllowAnonymous]
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")]
[AllowAnonymous]
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}")]
[AllowAnonymous]
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}")]
[AllowAnonymous]
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")]
[AllowAnonymous]
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")]
[AllowAnonymous]
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}")]
[AllowAnonymous]
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}")]
[AllowAnonymous]
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);
}
}