PlotDirector/PlotLine/Controllers/WriterPlanningController.cs

48 lines
3.5 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using System.Security.Claims;
using PlotLine.Data;
using PlotLine.Models;
using PlotLine.Services;
using PlotLine.Services.Writer;
namespace PlotLine.Controllers;
[Authorize,Route("Writer/PlanActivities")]
public sealed class WriterPlanningController(WriterPlanningRepository repository,IWritingScheduleService schedule):Controller
{
private int Actor=>int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
[HttpGet]
public async Task<IActionResult> Index(int writingPlanId)
{try{return View(await Load(writingPlanId));}catch(InvalidOperationException){return NotFound();}catch(SqlException e)when(e.Number==51000){return NotFound();}}
[HttpPost,ValidateAntiForgeryToken,RequestFormLimits(ValueCountLimit=20000)]
public async Task<IActionResult> Index(WriterPlanningPage input)
{
WriterPlanningPage model;
try{model=await Load(input.WritingPlanID);}catch(InvalidOperationException){return NotFound();}catch(SqlException e)when(e.Number==51000){return NotFound();}
try
{
if(!input.Confirm)throw new ArgumentException("Review the activity budgets and confirm before saving.");
var chosen=input.Candidates.Where(x=>x.Selected).ToArray();var selected=new List<WriterPlanActivity>();
foreach(var row in chosen){var actual=model.Candidates.SingleOrDefault(x=>x.ChapterID==row.ChapterID&&x.SceneID==row.SceneID);if(actual==null)throw new ArgumentException("A chapter or scene is no longer available. Reload the preview.");if(selected.Any(x=>x.ChapterID==row.ChapterID&&x.SceneID==row.SceneID))throw new ArgumentException("Choose each activity once.");actual.Minutes=row.Minutes;actual.Kind=row.Kind;selected.Add(actual);}
await repository.Save(Actor,input.WritingPlanID,WriterPlanning.Allocate(model.Plan,selected));
TempData["SchedulePreviewMessage"]="Reviewed activities were allocated within your saved availability.";
return RedirectToAction("SchedulePreview","Writer",new{writingPlanId=input.WritingPlanID});
}
catch(ArgumentException e){model.Error=e.Message;}
catch(SqlException e)when(e.Number==51000){model.Error="Activities changed or this plan already contains completed work. Reload, or create a plan for a new pass.";}
return View(model);
}
[HttpPost("Complete"),ValidateAntiForgeryToken]
public async Task<IActionResult> Complete(int writingPlanId,int activityId)
{try{await repository.Complete(Actor,writingPlanId,activityId);return RedirectToAction("SchedulePreview","Writer",new{writingPlanId});}catch(SqlException e)when(e.Number==51000){return NotFound();}}
private async Task<WriterPlanningPage> Load(int id)
{
var plan=await schedule.GetWritingPlanSettingsAsync(id);
var rows=(await repository.Read(Actor,id,"candidates")).ToList();
foreach(var row in rows){row.Minutes=WriterPlanning.StartingBudget(row.Words);row.Kind=plan.GoalType=="FirstDraft"?"Draft":plan.GoalType=="Revision"?"Edit":"Review";}
string? reason=null;
if(rows.Count==0){var context=await repository.Context(Actor,id);reason=context.Chapters==0?"There are no active chapters in this plan's scope. Add a chapter or choose a different book.":$"No unscheduled eligible scene remains: {context.Blocked} scenes are marked blocked and {context.ExistingTasks} already have manual tasks in this plan. Review the existing schedule, or change the blocked-scene setting in Edit availability / deadline.";}
return new(){WritingPlanID=id,Plan=plan,Candidates=rows,Scheduled=await repository.Read(Actor,id),EmptyReason=reason};
}
}