using System.Security.Claims; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using PlotLine.Services.Writer; namespace PlotLine.Controllers; [Authorize] public sealed class WriterReviewController(IWriterReviewService review):Controller { private int Author=>int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!); [HttpGet] public Task Panel(int bookId,int chapterId,Guid? paragraphId,string? version,int page=1,Guid? endParagraphId=null)=>Run(async()=>PartialView("~/Views/Writer/_ReviewContext.cshtml",await review.Read(Author,bookId,chapterId,paragraphId,version,page,endParagraphId))); [HttpPost,ValidateAntiForgeryToken] public Task Save(ReviewSave request)=>Run(async()=>{if(!ModelState.IsValid)return BadRequest(new{message="Review details are invalid. Your text is retained."});var row=await review.Save(Author,request);return Json(new{success=true,id=row.ID,version=row.Version});}); private async Task Run(Func> work){Response.Headers.CacheControl="no-store";Response.Headers["X-Robots-Tag"]="noindex, nofollow, noarchive";try{return await work();}catch(AuthoringConflictException e){return Conflict(new{message=e.Message});}catch(ArgumentException e){return BadRequest(new{message=e.Message});}catch(Exception e)when(e is UnauthorizedAccessException or KeyNotFoundException){return NotFound();}catch(Microsoft.Data.SqlClient.SqlException e)when(e.Number==51000){return NotFound();}catch(Exception e)when(e is Microsoft.Data.SqlClient.SqlException or TimeoutException){return StatusCode(503,new{message="Review information is temporarily unavailable. Your text is retained."});}} }