DeutschLernen/GermanApp/Presentation/Controllers/LevelsController.cs
Lasse Rune Hansen 3caee4c21e
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
feat(backend/presentation): add LevelsController and LessonsController for Lesson Management
- Created LevelsController.cs with full CRUD operations
  - GET /api/levels - all levels
  - GET /api/levels/{id} - level by ID
  - GET /api/levels/by-code/{code} - level by code
  - GET /api/levels/first - first level
  - GET /api/levels/next/{id} - next level
  - POST /api/levels - create level (Admin)
  - PUT /api/levels/{id} - update level (Admin)
  - DELETE /api/levels/{id} - delete level (Admin)

- Created LessonsController.cs with full CRUD and filtering
  - GET /api/lessons - all lessons
  - GET /api/lessons/ordered - ordered lessons
  - GET /api/lessons/{id} - lesson by ID
  - GET /api/lessons/by-level/{levelId} - lessons by level
  - GET /api/lessons/beginner - beginner lessons
  - GET /api/lessons/advanced - advanced lessons
  - GET /api/lessons/first/{levelId} - first lesson in level
  - GET /api/lessons/next/{id} - next lesson
  - GET /api/lessons/accessible/{userId} - accessible lessons
  - GET /api/lessons/unlocked/{userId}/{lessonId} - check unlock
  - POST /api/lessons - create lesson (Admin)
  - PUT /api/lessons/{id} - update lesson (Admin)
  - DELETE /api/lessons/{id} - delete lesson (Admin)

- Added authorization: [Authorize] for most endpoints, [AllowAnonymous] for read-only
- Added Admin role requirement for write operations

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-06-09 07:20:29 +02:00

138 lines
4.7 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 CEFR levels.
/// This is part of the Presentation layer.
/// </summary>
[ApiController]
[Route("api/[controller]")]
[Authorize]
public class LevelsController : ControllerBase
{
private readonly LevelService _levelService;
public LevelsController(LevelService levelService)
{
_levelService = levelService;
}
/// <summary>
/// Gets all CEFR levels ordered by their sort order.
/// </summary>
/// <returns>List of all levels</returns>
[HttpGet]
[AllowAnonymous]
public async Task<ActionResult<IReadOnlyList<LevelDto>>> GetAllLevelsAsync(CancellationToken cancellationToken = default)
{
var levels = await _levelService.GetAllLevelsAsync(cancellationToken);
return Ok(levels);
}
/// <summary>
/// Gets a specific level by its ID.
/// </summary>
/// <param name="id">The level ID</param>
/// <returns>The level with the specified ID</returns>
[HttpGet("{id}")]
[AllowAnonymous]
public async Task<ActionResult<LevelDto>> GetLevelByIdAsync(int id, CancellationToken cancellationToken = default)
{
var level = await _levelService.GetLevelByIdAsync(id, cancellationToken);
if (level == null)
return NotFound();
return Ok(level);
}
/// <summary>
/// Gets a level by its code (e.g., "A1", "B2").
/// </summary>
/// <param name="code">The level code</param>
/// <returns>The level with the specified code</returns>
[HttpGet("by-code/{code}")]
[AllowAnonymous]
public async Task<ActionResult<LevelDto>> GetLevelByCodeAsync(string code, CancellationToken cancellationToken = default)
{
var level = await _levelService.GetLevelByCodeAsync(code, cancellationToken);
if (level == null)
return NotFound();
return Ok(level);
}
/// <summary>
/// Creates a new CEFR level.
/// </summary>
/// <param name="dto">The level data</param>
/// <returns>The created level</returns>
[HttpPost]
[Authorize(Roles = "Admin")]
public async Task<ActionResult<LevelDto>> CreateLevelAsync(CreateLevelDto dto, CancellationToken cancellationToken = default)
{
var level = await _levelService.CreateLevelAsync(dto, cancellationToken);
return CreatedAtAction(nameof(GetLevelByIdAsync), new { id = level.Id }, level);
}
/// <summary>
/// Updates an existing CEFR level.
/// </summary>
/// <param name="id">The level ID</param>
/// <param name="dto">The updated level data</param>
/// <returns>The updated level</returns>
[HttpPut("{id}")]
[Authorize(Roles = "Admin")]
public async Task<ActionResult<LevelDto>> UpdateLevelAsync(int id, UpdateLevelDto dto, CancellationToken cancellationToken = default)
{
var level = await _levelService.UpdateLevelAsync(id, dto, cancellationToken);
if (level == null)
return NotFound();
return Ok(level);
}
/// <summary>
/// Deletes a CEFR level by its ID.
/// </summary>
/// <param name="id">The level ID</param>
/// <returns>No content on success</returns>
[HttpDelete("{id}")]
[Authorize(Roles = "Admin")]
public async Task<ActionResult> DeleteLevelAsync(int id, CancellationToken cancellationToken = default)
{
var result = await _levelService.DeleteLevelAsync(id, cancellationToken);
if (!result)
return NotFound();
return NoContent();
}
/// <summary>
/// Gets the first level (lowest order number) - typically A1.
/// </summary>
/// <returns>The first level</returns>
[HttpGet("first")]
[AllowAnonymous]
public async Task<ActionResult<LevelDto>> GetFirstLevelAsync(CancellationToken cancellationToken = default)
{
var level = await _levelService.GetFirstLevelAsync(cancellationToken);
if (level == null)
return NotFound();
return Ok(level);
}
/// <summary>
/// Gets the next level after the specified one.
/// </summary>
/// <param name="currentLevelId">The current level ID</param>
/// <returns>The next level</returns>
[HttpGet("next/{currentLevelId}")]
[AllowAnonymous]
public async Task<ActionResult<LevelDto>> GetNextLevelAsync(int currentLevelId, CancellationToken cancellationToken = default)
{
var level = await _levelService.GetNextLevelAsync(currentLevelId, cancellationToken);
if (level == null)
return NotFound();
return Ok(level);
}
}