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, ContextIntentChecklist checklist) : 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("Checklist")] public Task Checklist(int bookId, int chapterId, int? sceneId) => ReadChecklist(bookId, chapterId, sceneId); [HttpGet("Passages")] public async Task Passages(int bookId, int chapterId, int? sceneId) { if (!ModelState.IsValid) return BadRequest(); try { return Json(await checklist.Passages(bookId, chapterId, sceneId)); } catch (ArgumentException e) { return BadRequest(new { message = e.Message }); } catch (UnauthorizedAccessException) { return StatusCode(StatusCodes.Status403Forbidden); } catch (SqlException e) when (e.Number == 51010) { return StatusCode(StatusCodes.Status403Forbidden); } } [HttpPost("Checklist")] [ValidateAntiForgeryToken] public Task SaveChecklist([FromBody] IntentChecklistSave? request) => request is null ? Task.FromResult(BadRequest()) : ReadChecklist(request.BookID, request.ChapterID, request.SceneID, request); [HttpPost("Preview")] [ValidateAntiForgeryToken] public IActionResult Preview([FromBody] string? text) { if (!ModelState.IsValid || text is null) return BadRequest(); try { return Json(ContextIntentText.Parse(text)); } catch (ArgumentException e) { return BadRequest(new { message = e.Message }); } } private async Task ReadChecklist(int bookId, int chapterId, int? sceneId, IntentChecklistSave? save = null) { if (!ModelState.IsValid) return BadRequest(); try { return Json(await checklist.Load(bookId, chapterId, sceneId, save)); } catch (ArgumentException e) { return BadRequest(new { message = e.Message }); } catch (UnauthorizedAccessException) { return StatusCode(StatusCodes.Status403Forbidden); } catch (SqlException e) when (e.Number == 51010) { return StatusCode(StatusCodes.Status403Forbidden); } catch (InvalidOperationException e) { return Conflict(new { message = e.Message }); } catch (SqlException e) when (e.Number == 51011) { return Conflict(new { message = "The saved checklist changed. Your draft is still here." }); } catch (SqlException e) when (e.Number == 51012) { return BadRequest(new { message = "Review the checklist items and try again." }); } } [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); } } }