QrRapido/Controllers/TutoriaisController.cs
Ricardo Carneiro 162e28ae5a
All checks were successful
Deploy QR Rapido / test (push) Successful in 43s
Deploy QR Rapido / build-and-push (push) Successful in 16m43s
Deploy QR Rapido / deploy-staging (push) Has been skipped
Deploy QR Rapido / deploy-production (push) Successful in 1m55s
fix: PIX + idioma espanhol e SEO
2026-01-25 12:04:24 -03:00

128 lines
5.6 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using QRRapidoApp.Services;
using System.Security.Claims;
namespace QRRapidoApp.Controllers
{
public class TutoriaisController : Controller
{
private readonly IMarkdownService _markdownService;
private readonly AdDisplayService _adDisplayService;
private readonly ILogger<TutoriaisController> _logger;
private readonly IStringLocalizer<QRRapidoApp.Resources.SharedResource> _localizer;
private readonly IConfiguration _config;
public TutoriaisController(
IMarkdownService markdownService,
AdDisplayService adDisplayService,
ILogger<TutoriaisController> logger,
IStringLocalizer<QRRapidoApp.Resources.SharedResource> localizer,
IConfiguration config)
{
_markdownService = markdownService;
_adDisplayService = adDisplayService;
_logger = logger;
_localizer = localizer;
_config = config;
}
// Portuguese: /tutoriais/{slug} (canonical, no prefix)
// Spanish: /es-PY/tutoriais/{slug}
[Route("tutoriais/{slug}")]
[Route("es-PY/tutoriais/{slug}")]
public async Task<IActionResult> Article(string slug, string? culture = null)
{
try
{
// Determine culture from URL path if not provided
culture ??= Request.Path.Value?.StartsWith("/es-PY", StringComparison.OrdinalIgnoreCase) == true
? "es-PY"
: "pt-BR";
var article = await _markdownService.GetArticleAsync(slug, culture);
if (article == null)
{
_logger.LogWarning("Article not found: {Slug} ({Culture})", slug, culture);
return NotFound();
}
// Set ViewBag for ads and user info
var userId = User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
ViewBag.ShowAds = await _adDisplayService.ShouldShowAds(userId);
ViewBag.IsPremium = await _adDisplayService.HasValidPremiumSubscription(userId ?? "");
ViewBag.IsAuthenticated = User.Identity?.IsAuthenticated ?? false;
ViewBag.UserName = User.Identity?.Name ?? "";
_adDisplayService.SetViewBagAds(ViewBag);
// Set SEO metadata from article
ViewBag.Title = article.Metadata.Title;
ViewBag.Description = article.Metadata.Description;
ViewBag.Keywords = article.Metadata.Keywords;
ViewBag.OgImage = article.Metadata.Image;
ViewBag.OgType = "article";
ViewBag.ArticleAuthor = article.Metadata.Author;
ViewBag.ArticlePublishedTime = article.Metadata.Date.ToString("yyyy-MM-ddTHH:mm:ssZ");
ViewBag.ArticleModifiedTime = article.Metadata.LastMod.ToString("yyyy-MM-ddTHH:mm:ssZ");
ViewBag.Culture = culture;
ViewBag.Slug = slug;
// Get related articles (same culture, exclude current)
var allArticles = await _markdownService.GetAllArticlesAsync(culture);
article.RelatedArticles = allArticles
.Where(a => a.Title != article.Metadata.Title)
.Take(3)
.ToList();
_logger.LogInformation("Serving article: {Slug} ({Culture})", slug, culture);
return View(article);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error serving article: {Slug} ({Culture})", slug, culture);
return StatusCode(500, "Internal server error");
}
}
// Portuguese: /tutoriais (canonical, no prefix)
// Spanish: /es-PY/tutoriais
[Route("tutoriais")]
[Route("es-PY/tutoriais")]
public async Task<IActionResult> Index(string? culture = null)
{
try
{
// Determine culture from URL path if not provided
culture ??= Request.Path.Value?.StartsWith("/es-PY", StringComparison.OrdinalIgnoreCase) == true
? "es-PY"
: "pt-BR";
var articles = await _markdownService.GetAllArticlesAsync(culture);
// Set ViewBag
var userId = User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
ViewBag.ShowAds = await _adDisplayService.ShouldShowAds(userId);
ViewBag.IsPremium = await _adDisplayService.HasValidPremiumSubscription(userId ?? "");
ViewBag.IsAuthenticated = User.Identity?.IsAuthenticated ?? false;
ViewBag.UserName = User.Identity?.Name ?? "";
_adDisplayService.SetViewBagAds(ViewBag);
ViewBag.Title = culture == "pt-BR" ? "Tutoriais QR Code" : "Tutoriales Código QR";
ViewBag.Description = culture == "pt-BR"
? "Aprenda a criar e usar QR Codes com nossos tutoriais completos"
: "Aprende a crear y usar códigos QR con nuestros tutoriales completos";
ViewBag.Culture = culture;
_logger.LogInformation("Serving tutorials index ({Culture}): {Count} articles", culture, articles.Count);
return View(articles);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error serving tutorials index ({Culture})", culture);
return StatusCode(500, "Internal server error");
}
}
}
}