using BCards.Web.Areas.Tutoriais.Models.ViewModels; using BCards.Web.Areas.Tutoriais.Services; using Microsoft.AspNetCore.Mvc; namespace BCards.Web.Areas.Artigos.Controllers; [Area("Artigos")] public class ArtigosController : Controller { private readonly IMarkdownService _markdownService; private readonly ILogger _logger; public ArtigosController( IMarkdownService markdownService, ILogger logger) { _markdownService = markdownService; _logger = logger; } // GET /artigos public async Task Index() { var artigos = await _markdownService .GetAllArticlesAsync("Artigos", "pt-BR"); return View(artigos); } // GET /artigos/{slug} public async Task Article(string slug) { // Sanitização slug = slug.Replace("..", "").Replace("/", "").Replace("\\", ""); try { var article = await _markdownService.GetArticleAsync( $"Artigos/{slug}", "pt-BR" ); if (article == null) { _logger.LogWarning("Artigo não encontrado: {Slug}", slug); return NotFound(); } // Buscar outros artigos para "Leia também" article.RelatedArticles = await _markdownService .GetAllArticlesAsync("Artigos", "pt-BR"); article.RelatedArticles = article.RelatedArticles .Where(a => a.Slug != slug) .OrderByDescending(a => a.Date) .Take(3) .ToList(); return View(article); } catch (FileNotFoundException) { _logger.LogWarning("Arquivo markdown não encontrado: {Slug}", slug); return NotFound(); } } }