using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Data.SqlClient; using PlotLine.Services.Context; namespace PlotLine.Controllers; [Authorize] [Route("Writing/Context/Intent")] [ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)] public sealed class ContextIntentController(ContextIntentPalette palette) : Controller { // Reusable read endpoint for both Context and the docked Writer component. // It takes an explicit held scope and never follows or changes the Word caret. [HttpGet("Panel")] public IActionResult Panel(int projectId, int bookId, int chapterId, int? sceneId) { if (!ModelState.IsValid || projectId <= 0 || bookId <= 0 || chapterId <= 0 || sceneId <= 0) return BadRequest(); // The fragment contains only caller-supplied IDs. The palette read // below authorises the exact scope before returning saved information. return PartialView("~/Views/Writing/_ContextIntentPanel.cshtml", new IntentPaletteScope(projectId, bookId, chapterId, sceneId)); } [HttpGet("Palette")] public async Task Palette([FromQuery] IntentPaletteQuery query) { if (!ModelState.IsValid) return BadRequest(); try { return Json(await palette.Read(query)); } catch (ArgumentException e) { return BadRequest(new { message = e.Message }); } // This is a JSON data endpoint. Cookie Forbid redirects to an HTML // AccessDenied page; callers need a terminal scope denial instead. catch (UnauthorizedAccessException) { return StatusCode(StatusCodes.Status403Forbidden); } catch (SqlException e) when (e.Number == 51010) { return StatusCode(StatusCodes.Status403Forbidden); } } }