362 lines
14 KiB
C#
362 lines
14 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using PlotLine.Services;
|
|
using PlotLine.ViewModels;
|
|
using PlotLine.Data;
|
|
using System.Security.Claims;
|
|
using Microsoft.Data.SqlClient;
|
|
|
|
namespace PlotLine.Controllers;
|
|
|
|
[Authorize]
|
|
public sealed class WriterController(
|
|
IWriterWorkspaceService writerWorkspace,
|
|
IWritingScheduleService writingSchedule,
|
|
IBookRepository? bookRepository=null, IProjectAccessService? access=null, WritingActivityRepository? activity=null) : Controller
|
|
{
|
|
public async Task<IActionResult> Index(int? projectId,int? bookId=null,string? workspace=null,bool journey=false)
|
|
{
|
|
if(bookId.HasValue&&!projectId.HasValue&&bookRepository!=null)
|
|
{
|
|
if(access!=null&&!await access.CanAccessEntityAsync("Book",bookId.Value))return NotFound();
|
|
var selected=await bookRepository.GetAsync(bookId.Value);if(selected==null||selected.IsArchived)return NotFound();projectId=selected.ProjectID;
|
|
}
|
|
var model = await writerWorkspace.GetDashboardAsync(projectId);
|
|
if(!projectId.HasValue && int.TryParse(model.ProjectOptions.FirstOrDefault()?.Value,out var firstProject))
|
|
{ projectId=firstProject;model=await writerWorkspace.GetDashboardAsync(projectId); }
|
|
if(bookRepository!=null && projectId.HasValue)
|
|
{
|
|
var available=(await bookRepository.ListByProjectAsync(projectId.Value)).Where(b=>!b.IsArchived).ToArray();
|
|
model.Book=bookId.HasValue?available.SingleOrDefault(b=>b.BookID==bookId):available.FirstOrDefault();
|
|
if(bookId.HasValue&&model.Book==null)return NotFound();
|
|
model.BookID=model.Book?.BookID;
|
|
}
|
|
else model.BookID=bookId;
|
|
if(model.BookID.HasValue)
|
|
{
|
|
if(access!=null&&!await access.CanAccessEntityAsync("Book",model.BookID.Value))return NotFound();
|
|
model.WriteNext=model.WriteNext.Where(x=>x.BookID==model.BookID).ToArray();
|
|
model.ContinueWriting=model.ContinueWriting.Where(x=>x.BookID==model.BookID).ToArray();
|
|
model.RevisionQueue=model.RevisionQueue.Where(x=>x.BookID==model.BookID).ToArray();
|
|
model.PolishingQueue=model.PolishingQueue.Where(x=>x.BookID==model.BookID).ToArray();
|
|
if(activity!=null&&int.TryParse(User.FindFirstValue(ClaimTypes.NameIdentifier),out var user))
|
|
{ try{model.Activity=await activity.Read(user,model.BookID.Value);}catch(SqlException e)when(e.Number==51000){ model.ActivityIsPrivate=true; } }
|
|
if(activity!=null&&int.TryParse(User.FindFirstValue(ClaimTypes.NameIdentifier),out var reader))model.StoryHealthReminders=await activity.Reminders(reader,model.BookID.Value);
|
|
}
|
|
model.Workspace=workspace is "InProgress" or "Revision" or "FinalPass" or "StoryHealth"?workspace:"WriteNext";
|
|
model.Journey=journey;
|
|
model.WritingSchedule = await writingSchedule.GetTodayDashboardAsync(projectId,model.BookID);
|
|
if(model.BookID.HasValue)
|
|
{ model.WritingSchedule.TodayTasks=model.WritingSchedule.TodayTasks.Where(x=>x.BookID==model.BookID).ToArray();model.WritingSchedule.UpcomingTasks=model.WritingSchedule.UpcomingTasks.Where(x=>x.BookID==model.BookID).ToArray(); }
|
|
return View("Index",model);
|
|
}
|
|
public Task<IActionResult> Workspaces(int? projectId,int? bookId,string? workspace=null)=>Index(projectId,bookId,workspace);
|
|
public Task<IActionResult> Journey(int? projectId,int? bookId)=>Index(projectId,bookId,journey:true);
|
|
public IActionResult DisplaySetup(int? projectId,int? bookId)=>Redirect(Url.Action(nameof(Index),new{projectId,bookId})+"#display-setup");
|
|
[HttpGet("Write"),HttpGet("Write/Index")]
|
|
public IActionResult LegacyWrite(int? projectId,int? bookId)=>RedirectToAction(nameof(Index),new{projectId,bookId});
|
|
|
|
public async Task<IActionResult> Chapter(int id)
|
|
{
|
|
var model = await writerWorkspace.GetChapterWorkflowAsync(id);
|
|
return model is null ? NotFound() : View(model);
|
|
}
|
|
|
|
public async Task<IActionResult> SchedulePreview(int? writingPlanId)
|
|
{
|
|
var model = await writingSchedule.GetSchedulePreviewAsync(writingPlanId);
|
|
return View(model);
|
|
}
|
|
|
|
public async Task<IActionResult> WritingPlans()
|
|
{
|
|
var model = await writingSchedule.GetWritingPlansForManagementAsync();
|
|
return View(model);
|
|
}
|
|
|
|
public async Task<IActionResult> EditWritingPlan(int writingPlanId)
|
|
{
|
|
try
|
|
{
|
|
var model = await writingSchedule.GetWritingPlanSettingsAsync(writingPlanId);
|
|
return View(model);
|
|
}
|
|
catch (InvalidOperationException)
|
|
{
|
|
return NotFound();
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> EditWritingPlan(WritingPlanSettingsEditViewModel model)
|
|
{
|
|
if (!model.WritingDayAvailability.Any(x => x.IsAvailable))
|
|
{
|
|
ModelState.AddModelError(string.Empty, "Select at least one writing day.");
|
|
ModelState.AddModelError(nameof(model.WritingDayAvailability), "Select at least one writing day.");
|
|
}
|
|
|
|
if (model.DeadlineDate.Date < model.StartDate.Date)
|
|
{
|
|
ModelState.AddModelError(nameof(model.DeadlineDate), "Deadline date must be on or after the start date.");
|
|
}
|
|
|
|
if (!ModelState.IsValid)
|
|
{
|
|
await PopulatePlanSettingsDisplayAsync(model);
|
|
return View(model);
|
|
}
|
|
|
|
try
|
|
{
|
|
await writingSchedule.UpdateWritingPlanSettingsAsync(model);
|
|
TempData["WritingPlansMessage"] = "Writing plan updated.";
|
|
return RedirectToAction(nameof(WritingPlans));
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
ModelState.AddModelError(string.Empty, ex.Message);
|
|
await PopulatePlanSettingsDisplayAsync(model);
|
|
return View(model);
|
|
}
|
|
}
|
|
|
|
public async Task<IActionResult> PreviewRebalance(int writingPlanId)
|
|
{
|
|
var model = await writingSchedule.GetSchedulePreviewAsync(writingPlanId);
|
|
try
|
|
{
|
|
model.RebalancePreview = await writingSchedule.PreviewRebalanceAsync(writingPlanId);
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
TempData["SchedulePreviewError"] = ex.Message;
|
|
return RedirectToAction(nameof(SchedulePreview), new { writingPlanId });
|
|
}
|
|
|
|
return View(nameof(SchedulePreview), model);
|
|
}
|
|
|
|
public async Task<IActionResult> ScheduleSetup(int? projectId)
|
|
{
|
|
var model = await writingSchedule.GetScheduleSetupAsync(projectId);
|
|
return View(model);
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> ScheduleSetup(WritingScheduleSetupViewModel model)
|
|
{
|
|
if (!model.WritingDayAvailability.Any(x => x.IsAvailable))
|
|
{
|
|
ModelState.AddModelError(string.Empty, "Select at least one writing day.");
|
|
ModelState.AddModelError(nameof(model.WritingDayAvailability), "Select at least one writing day.");
|
|
}
|
|
|
|
if (model.DeadlineDate.Date < model.StartDate.Date)
|
|
{
|
|
ModelState.AddModelError(nameof(model.DeadlineDate), "Deadline date must be on or after the start date.");
|
|
}
|
|
|
|
if (!ModelState.IsValid)
|
|
{
|
|
await writingSchedule.PopulateScheduleSetupOptionsAsync(model);
|
|
return View(model);
|
|
}
|
|
|
|
try
|
|
{
|
|
var result = await writingSchedule.CreateScheduleFromSetupAsync(model);
|
|
if (string.IsNullOrWhiteSpace(result.ValidationMessage))
|
|
{
|
|
TempData["SchedulePreviewMessage"] = $"Writing plan created. {result.GeneratedItemCount} item{(result.GeneratedItemCount == 1 ? "" : "s")} generated.";
|
|
}
|
|
else
|
|
{
|
|
TempData["SchedulePreviewError"] = result.ValidationMessage;
|
|
}
|
|
|
|
return RedirectToAction(nameof(SchedulePreview), new { writingPlanId = result.WritingPlanID });
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
ModelState.AddModelError(string.Empty, ex.Message);
|
|
await writingSchedule.PopulateScheduleSetupOptionsAsync(model);
|
|
return View(model);
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> GenerateSchedule(int writingPlanId)
|
|
{
|
|
try
|
|
{
|
|
var items = await writingSchedule.GenerateSchedule(writingPlanId);
|
|
TempData["SchedulePreviewMessage"] = $"{items.Count} item{(items.Count == 1 ? "" : "s")} generated.";
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
TempData["SchedulePreviewError"] = ex.Message;
|
|
}
|
|
|
|
return RedirectToAction(nameof(SchedulePreview), new { writingPlanId });
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> DeleteGeneratedSchedule(int writingPlanId)
|
|
{
|
|
try
|
|
{
|
|
await writingSchedule.DeleteScheduleAsync(writingPlanId);
|
|
TempData["SchedulePreviewMessage"] = "Generated schedule deleted.";
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
TempData["SchedulePreviewError"] = ex.Message;
|
|
}
|
|
catch
|
|
{
|
|
TempData["SchedulePreviewError"] = "Generated schedule could not be deleted.";
|
|
}
|
|
|
|
return RedirectToAction(nameof(SchedulePreview), new { writingPlanId });
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> ApplyRebalance(int writingPlanId)
|
|
{
|
|
try
|
|
{
|
|
await writingSchedule.ApplyRebalanceAsync(writingPlanId);
|
|
TempData["SchedulePreviewMessage"] = "Schedule rebalanced successfully.";
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
TempData["SchedulePreviewError"] = ex.Message;
|
|
}
|
|
|
|
return RedirectToAction(nameof(SchedulePreview), new { writingPlanId });
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> ActivateWritingPlan(int writingPlanId)
|
|
{
|
|
try
|
|
{
|
|
await writingSchedule.ActivateWritingPlanAsync(writingPlanId);
|
|
TempData["WritingPlansMessage"] = "Writing plan activated.";
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
TempData["WritingPlansError"] = ex.Message;
|
|
}
|
|
|
|
return RedirectToAction(nameof(WritingPlans));
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> DeactivateWritingPlan(int writingPlanId)
|
|
{
|
|
try
|
|
{
|
|
await writingSchedule.DeactivateWritingPlanAsync(writingPlanId);
|
|
TempData["WritingPlansMessage"] = "Writing plan deactivated.";
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
TempData["WritingPlansError"] = ex.Message;
|
|
}
|
|
|
|
return RedirectToAction(nameof(WritingPlans));
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> DeleteWritingPlan(int writingPlanId)
|
|
{
|
|
try
|
|
{
|
|
await writingSchedule.DeleteWritingPlanAsync(writingPlanId);
|
|
TempData["WritingPlansMessage"] = "Writing plan deleted.";
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
TempData["WritingPlansError"] = ex.Message;
|
|
}
|
|
|
|
return RedirectToAction(nameof(WritingPlans));
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> RegenerateWritingPlanSchedule(int writingPlanId)
|
|
{
|
|
try
|
|
{
|
|
var items = await writingSchedule.RegenerateScheduleAsync(writingPlanId);
|
|
TempData["WritingPlansMessage"] = $"{items.Count} item{(items.Count == 1 ? "" : "s")} generated.";
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
TempData["WritingPlansError"] = ex.Message;
|
|
}
|
|
|
|
return RedirectToAction(nameof(WritingPlans));
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> MarkScheduleItemComplete(int writingScheduleItemId, int? projectId)
|
|
{
|
|
var updated = await writingSchedule.MarkScheduleItemCompleteAsync(writingScheduleItemId);
|
|
TempData[updated ? "WriterWorkspaceMessage" : "WriterWorkspaceError"] = updated
|
|
? "Writing task marked complete."
|
|
: "Only planned writing tasks can be marked complete.";
|
|
return RedirectToAction(nameof(Index), new { projectId });
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> SkipScheduleItem(int writingScheduleItemId, int? projectId)
|
|
{
|
|
var updated = await writingSchedule.SkipScheduleItemAsync(writingScheduleItemId);
|
|
TempData[updated ? "WriterWorkspaceMessage" : "WriterWorkspaceError"] = updated
|
|
? "Writing task skipped."
|
|
: "Only planned writing tasks can be skipped.";
|
|
return RedirectToAction(nameof(Index), new { projectId });
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> SaveChapterGoal(ChapterGoalEditViewModel model)
|
|
{
|
|
await writerWorkspace.SaveChapterGoalAsync(model);
|
|
if (string.Equals(Request.Headers["X-Requested-With"], "XMLHttpRequest", StringComparison.OrdinalIgnoreCase))
|
|
return Json(new { ok = true });
|
|
return RedirectToAction(nameof(Chapter), new { id = model.ChapterID });
|
|
}
|
|
|
|
private async Task PopulatePlanSettingsDisplayAsync(WritingPlanSettingsEditViewModel model)
|
|
{
|
|
try
|
|
{
|
|
var existing = await writingSchedule.GetWritingPlanSettingsAsync(model.WritingPlanID);
|
|
model.ProjectName = existing.ProjectName;
|
|
model.BookName = existing.BookName;
|
|
model.GoalType = existing.GoalType;
|
|
model.StartDate = existing.StartDate;
|
|
model.RebalanceModeOptions = existing.RebalanceModeOptions;
|
|
}
|
|
catch (InvalidOperationException)
|
|
{
|
|
// The service will surface the ownership/not-found validation message on submit.
|
|
}
|
|
}
|
|
}
|