using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Data.SqlClient; using PlotLine.Services; using PlotLine.Services.Manuscript; namespace PlotLine.Controllers; [Authorize] [Route("manuscript/reader")] [ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)] public sealed class ManuscriptReaderController(ManuscriptReaderService reader, ICurrentUserService currentUser, ILogger logger) : Controller { [HttpGet] public async Task Index(int bookId, int? chapterId = null) { Response.Headers["X-Robots-Tag"] = "noindex, nofollow, noarchive"; if (currentUser.UserId is not int user) return Challenge(); if (bookId <= 0 || chapterId is <= 0) return NotFound(); try { var model = await reader.Read(user, bookId, chapterId); return model is null ? NotFound() : View(model); } catch (UnauthorizedAccessException) { return NotFound(); } catch (SqlException e) when (e.Number == 51000) { return NotFound(); } catch (Exception e) when (e is SqlException or TimeoutException or System.Text.Json.JsonException) { // Log neither SQL error text nor payloads: manuscripts are private content. logger.LogWarning("Manuscript Reader unavailable ({FailureKind}).", e.GetType().Name); Response.StatusCode = StatusCodes.Status503ServiceUnavailable; return View("Unavailable", new { BookId = bookId, ChapterId = chapterId }); } } }