38 lines
3.3 KiB
C#
38 lines
3.3 KiB
C#
using System.Security.Claims;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Data.SqlClient;
|
|
using PlotLine.Services.Manuscript;
|
|
using PlotLine.Services.NativeCompanion;
|
|
namespace PlotLine.Controllers;
|
|
[Authorize,Route("manuscript/paragraphs")]
|
|
public sealed class ParagraphManuscriptController(ParagraphManuscriptService manuscripts,NativeManuscriptCapture capture,NativePresenceStore presence):Controller
|
|
{
|
|
private int Author=>int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
|
|
[HttpGet]
|
|
public async Task<IActionResult> Index(int bookId,int page=1,int evidencePage=1)
|
|
{
|
|
if(!manuscripts.Enabled)return NotFound();
|
|
try{var model=await manuscripts.Workspace(Author,bookId,page);model.EvidencePage=Math.Max(1,evidencePage);model.Message=TempData["ParagraphMessage"] as string;return View(model);}catch(UnauthorizedAccessException){return Forbid();}
|
|
}
|
|
[HttpPost("enable"),ValidateAntiForgeryToken]
|
|
public Task<IActionResult> Enable(int bookId,string mode)=>Run(bookId,async()=>{await manuscripts.Enable(Author,bookId,mode);return "Paragraph tracking preference saved. Word prose is never rewritten by PlotDirector.";});
|
|
[HttpPost("snapshot"),ValidateAntiForgeryToken]
|
|
public Task<IActionResult> Snapshot(int bookId)=>Run(bookId,async()=>{var id=await capture.RequestParagraphSnapshot(Author,bookId);return id==Guid.Empty?"A transfer is already pending. Refresh after it finishes.":"Word snapshot requested. Keep Word open until the transfer is acknowledged, then refresh this page.";});
|
|
[HttpPost("cancel"),ValidateAntiForgeryToken]
|
|
public Task<IActionResult> Cancel(int bookId)=>Run(bookId,async()=>{await capture.CancelParagraphSnapshot(Author,bookId);return "Pending snapshot cancelled. Already acknowledged snapshots remain saved.";});
|
|
[HttpPost("change"),ValidateAntiForgeryToken]
|
|
public Task<IActionResult> Change(int bookId,long revision,string action,Guid target,Guid other,string? text,string? eventKey,bool primary,int from,int to,int? startOffset,int? endOffset)=>Run(bookId,async()=>{
|
|
string? kind=null;int eventId=0;if(action=="anchor"){var pieces=eventKey?.Split(':');if(pieces?.Length!=2||pieces[0] is not ("Knowledge" or "Relationship")||!int.TryParse(pieces[1],out eventId))throw new ArgumentException("Choose a saved knowledge or relationship event.");kind=pieces[0];}
|
|
await manuscripts.Change(Author,bookId,revision,action,target,other,text,kind,eventId,primary,from-1,to-1,startOffset,endOffset);presence.BooksChanged(Author);return "Manuscript structure and evidence updated. Word prose is unchanged.";});
|
|
private async Task<IActionResult> Run(int book,Func<Task<string>> work)
|
|
{
|
|
if(!manuscripts.Enabled)return NotFound();
|
|
try{TempData["ParagraphMessage"]=await work();}catch(UnauthorizedAccessException){return Forbid();}
|
|
catch(Exception e)when(e is ArgumentException or InvalidOperationException){TempData["ParagraphMessage"]=e.Message;}
|
|
catch(SqlException e)when(e.Number==51000){TempData["ParagraphMessage"]=e.Message;}
|
|
catch(SqlException){TempData["ParagraphMessage"]="The change was refused. Check your owner permissions, finish any active import, then refresh before retrying.";}
|
|
return RedirectToAction(nameof(Index),new{bookId=book});
|
|
}
|
|
}
|