48 lines
3.9 KiB
C#
48 lines
3.9 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using PlotLine.Services.Context;
|
|
namespace PlotLine.Controllers;
|
|
|
|
[Authorize,Route("Writing/Context"),ResponseCache(NoStore=true,Location=ResponseCacheLocation.None)]
|
|
public sealed class ContextToolsController(ContextCharacterEditing editing,ContextRelationshipEditing relationships):Controller
|
|
{
|
|
[HttpGet("EditCharacter")]
|
|
public async Task<IActionResult> EditCharacter(int bookId,int sceneId,int characterId,string field)
|
|
{try{return PartialView("~/Views/Writing/_ContextCharacterEdit.cshtml",await editing.Read(bookId,sceneId,characterId,field));}catch(UnauthorizedAccessException){return Forbid();}catch(ArgumentException){return BadRequest();}}
|
|
[HttpPost("EditCharacter"),ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> SaveCharacter(ContextCharacterEditForm model)
|
|
{
|
|
try{
|
|
if(ModelState.IsValid){await editing.Save(model);return Ok(new{saved=true,bookId=model.BookID});}
|
|
return await Invalid(model,"Check the correction below.");
|
|
}catch(UnauthorizedAccessException){return Forbid();}
|
|
catch(Exception e)when(e is ArgumentException or InvalidOperationException){return await Invalid(model,e.Message);}
|
|
catch(Microsoft.Data.SqlClient.SqlException){return await Invalid(model,"The correction could not be saved. Your draft is retained; retry when the connection recovers.");}
|
|
}
|
|
[HttpGet("EditRelationship")]
|
|
public async Task<IActionResult> EditRelationship(int bookId,int sceneId,int relationshipId=0,string mode="scene",string referenceLens="story")
|
|
{try{return PartialView("~/Views/Writing/_ContextRelationshipEdit.cshtml",await relationships.Read(bookId,sceneId,relationshipId,mode,referenceLens));}catch(UnauthorizedAccessException){return Forbid();}catch(ArgumentException e){return BadRequest(e.Message);}}
|
|
[HttpPost("EditRelationship"),ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> SaveRelationship(ContextRelationshipForm model)
|
|
{
|
|
try{if(ModelState.IsValid){await relationships.Save(model);return Ok(new{saved=true,bookId=model.BookID});}return await InvalidRelationship(model,"Check the correction below.");}
|
|
catch(UnauthorizedAccessException){return Forbid();}
|
|
catch(Exception e)when(e is ArgumentException or InvalidOperationException){return await InvalidRelationship(model,e.Message);}
|
|
catch(Microsoft.Data.SqlClient.SqlException){return await InvalidRelationship(model,"Save unavailable. Your draft is retained; retry when the connection recovers.");}
|
|
}
|
|
private async Task<IActionResult> InvalidRelationship(ContextRelationshipForm input,string error)
|
|
{
|
|
ContextRelationshipForm frame;
|
|
try{frame=await relationships.Read(input.BookID,input.SceneID,input.RelationshipID,input.Mode,input.ReferenceLens);}catch(UnauthorizedAccessException){return Forbid();}catch(ArgumentException e){return BadRequest(e.Message);}
|
|
frame.SourceID=input.SourceID;frame.TargetID=input.TargetID;frame.TypeID=input.TypeID;frame.StateID=input.StateID;frame.Intensity=input.Intensity;frame.Mutual=input.Mutual;frame.Known=input.Known;frame.Notes=input.Notes;frame.Fingerprint=input.Fingerprint;
|
|
ModelState.AddModelError("",error);Response.StatusCode=422;return PartialView("~/Views/Writing/_ContextRelationshipEdit.cshtml",frame);
|
|
}
|
|
private async Task<IActionResult> Invalid(ContextCharacterEditForm input,string error)
|
|
{
|
|
ContextCharacterEditForm frame;
|
|
try{frame=await editing.Read(input.BookID,input.SceneID,input.CharacterID,input.Field);}catch(UnauthorizedAccessException){return Forbid();}catch(ArgumentException){return BadRequest();}
|
|
frame.Value=input.Value;frame.Fingerprint=input.Fingerprint;ModelState.AddModelError(nameof(input.Value),error);Response.StatusCode=422;
|
|
return PartialView("~/Views/Writing/_ContextCharacterEdit.cshtml",frame);
|
|
}
|
|
}
|