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