PlotDirector/PlotLine/Controllers/TimelineController.cs
2026-11-30 23:20:12 +00:00

187 lines
7.3 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using PlotLine.Models;
using PlotLine.Services;
using PlotLine.ViewModels;
namespace PlotLine.Controllers;
[Authorize]
public sealed class TimelineController(ITimelineService timeline, ContextSavedData savedData) : Controller
{
public async Task<IActionResult> Index([FromQuery] TimelineFilterViewModel filter)
{
if (filter.ProjectID == 0)
{
return BadRequest("ProjectID is required.");
}
var model = await timeline.GetAsync(filter);
return model is null ? NotFound() : View(model);
}
public async Task<IActionResult> Settings(int projectId)
{
var model = await timeline.GetSettingsAsync(projectId);
return model is null ? NotFound() : View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Settings(TimelineSettingsViewModel model)
{
if (model.ShowMetricShape && !model.SelectedMetricTypeIDs.Any())
{
ModelState.AddModelError(nameof(model.SelectedMetricTypeIDs), "Select at least one metric or disable Metric Shape.");
}
if (!ModelState.IsValid)
{
var current = await timeline.GetSettingsAsync(model.ProjectID);
if (current is null)
{
return NotFound();
}
current.ShowSceneCards = model.ShowSceneCards;
current.ShowMetricShape = model.ShowMetricShape;
current.ShowStoryAssets = model.ShowStoryAssets;
current.ShowWarnings = model.ShowWarnings;
current.SelectedMetricTypeIDs = model.SelectedMetricTypeIDs;
current.MetricOptions = current.MetricOptions
.Select(x => new TimelineMetricSettingOptionViewModel
{
MetricTypeID = x.MetricTypeID,
MetricName = x.MetricName,
Description = x.Description,
SortOrder = x.SortOrder,
IsSelected = model.SelectedMetricTypeIDs.Contains(x.MetricTypeID)
})
.ToList();
return View(current);
}
await timeline.SaveSettingsAsync(model);
TempData["TimelineSettingsMessage"] = "Timeline settings saved.";
return RedirectToAction(nameof(Settings), new { projectId = model.ProjectID });
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> SavePreset(TimelinePresetSaveViewModel model)
{
await timeline.SavePresetAsync(model);
return RedirectToAction(nameof(Index), new { projectId = model.ProjectID, bookId = model.BookID });
}
public async Task<IActionResult> LoadPreset(int id)
{
var preset = await timeline.GetPresetAsync(id);
if (preset is null)
{
return NotFound();
}
var filter = System.Text.Json.JsonSerializer.Deserialize<TimelineFilterViewModel>(preset.FilterJson) ?? new TimelineFilterViewModel();
filter.ProjectID = preset.ProjectID;
filter.BookID = preset.BookID;
filter.FocusType = preset.FocusType;
filter.FocusID = preset.FocusID;
return RedirectToAction(nameof(Index), filter);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeletePreset(int id, int projectId, int? bookId)
{
await timeline.ArchivePresetAsync(id);
return RedirectToAction(nameof(Index), new { projectId, bookId });
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DropMoveScene(TimelineSceneDropMoveViewModel model)
{
var summary = await timeline.MoveSceneByDropAsync(model);
if (summary is not null) await savedData.Scene(model.SceneID);
if (summary is not null)
{
TempData["TimelineDragMessage"] = $"Scene moved. Validation found {summary.WarningCount} warning{(summary.WarningCount == 1 ? "" : "s")}.";
}
return RedirectToAction(nameof(Index), new { projectId = model.ProjectID, bookId = model.BookID, selectedSceneId = model.ReturnSelectedSceneID });
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DropCharacter(TimelineSceneCharacterDropViewModel model)
{
if (Request.Headers.Accept.ToString().Contains("application/json"))
return await SaveSceneDropAsync(() => SaveAndNotify(() => timeline.AddCharacterByDropAsync(model), model.SceneID, "characters"));
await RunDropActionAsync(() => SaveAndNotify(() => timeline.AddCharacterByDropAsync(model), model.SceneID, "characters"), "Character added to scene.");
return RedirectToAction(nameof(Index), new { projectId = model.ReturnProjectID, bookId = model.ReturnBookID, selectedSceneId = model.SceneID });
}
[HttpPost]
[ValidateAntiForgeryToken]
public Task<IActionResult> DropLocation(TimelineSceneLocationDropViewModel model) =>
SaveSceneDropAsync(() => SaveAndNotify(() => timeline.AddLocationByDropAsync(model), model.SceneID, "locations"));
private async Task SaveAndNotify(Func<Task> action, int sceneId, string kind)
{
await action();
await savedData.Scene(sceneId, kind);
}
private async Task<IActionResult> SaveSceneDropAsync(Func<Task> action)
{
if (!ModelState.IsValid)
return BadRequest(new { success = false, message = "Check the selected values and try again." });
try
{
await action();
return Json(new { success = true });
}
catch (InvalidOperationException ex)
{
return BadRequest(new { success = false, message = ex.Message });
}
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DropAssetEvent(TimelineAssetEventDropViewModel model)
{
await RunDropActionAsync(() => SaveAndNotify(() => timeline.AddAssetEventByDropAsync(model), model.SceneID, "assets"), "Asset event added to scene.");
return RedirectToAction(nameof(Index), new { projectId = model.ReturnProjectID, bookId = model.ReturnBookID, selectedSceneId = model.SceneID });
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DropAssetDependency(TimelineAssetDependencyDropViewModel model)
{
await RunDropActionAsync(() => timeline.AddAssetDependencyByDropAsync(model), "Asset dependency saved.");
return RedirectToAction(nameof(Index), new { projectId = model.ReturnProjectID, bookId = model.ReturnBookID });
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DropLocationRelationship(TimelineLocationRelationshipDropViewModel model)
{
await RunDropActionAsync(async () => { await timeline.AddLocationRelationshipByDropAsync(model); await savedData.Project(model.ProjectID, "locations", model.FromLocationID, "Location"); }, "Location relationship saved.");
return RedirectToAction(nameof(Index), new { projectId = model.ProjectID, bookId = model.ReturnBookID });
}
private async Task RunDropActionAsync(Func<Task> action, string successMessage)
{
try
{
await action();
TempData["TimelineDragMessage"] = successMessage;
}
catch (InvalidOperationException ex)
{
TempData["TimelineDragMessage"] = ex.Message;
}
}
}