55 lines
3.1 KiB
C#
55 lines
3.1 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using PlotLine.Data;
|
|
using PlotLine.Services;
|
|
|
|
namespace PlotLine.Controllers;
|
|
|
|
[Authorize, Route("Writing/Context/Calendar"), ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
|
|
public sealed class ContextCalendarController(IWordCompanionRepository access, IBookRepository books,
|
|
ICurrentUserService user, ITimelineRepository timeline) : Controller
|
|
{
|
|
private async Task<int?> Project(int bookId)
|
|
{
|
|
if (!user.UserId.HasValue || await access.GetBookStructureAsync(bookId, user.UserId.Value) == null) return null;
|
|
return (await books.GetAsync(bookId))?.ProjectID;
|
|
}
|
|
|
|
[HttpGet("")]
|
|
public async Task<IActionResult> Panel(int bookId, int? projectId, int? chapterId, int? sceneId)
|
|
{
|
|
var project = await Project(bookId);
|
|
if (!project.HasValue) return Forbid();
|
|
if (!ModelState.IsValid || projectId.HasValue && project != projectId) return BadRequest();
|
|
var structure = await access.GetBookStructureAsync(bookId, user.UserId!.Value);
|
|
var chapter = structure!.Chapters.FirstOrDefault(c => sceneId.HasValue
|
|
? c.Scenes.Any(s => s.SceneId == sceneId) : c.ChapterId == chapterId);
|
|
if ((sceneId.HasValue || chapterId.HasValue) && chapter == null || chapterId.HasValue && chapter?.ChapterId != chapterId) return BadRequest();
|
|
return PartialView("~/Views/Writing/_ContextCalendar.cshtml", new ContextCalendarScope(project.Value, bookId, chapter?.ChapterId, sceneId));
|
|
}
|
|
|
|
[HttpGet("State")]
|
|
public async Task<IActionResult> State(int bookId)
|
|
{
|
|
var project = await Project(bookId);
|
|
if (!project.HasValue) return Forbid();
|
|
// Reuse the canonical Story Calendar/Timeline stored-procedure projection.
|
|
// Project access was verified before loading cross-book reference data.
|
|
var data = await timeline.GetByProjectAsync(project.Value, null, false, [], false);
|
|
if (data.Project == null) return NotFound();
|
|
var chapters = data.Chapters.Where(c => !c.IsArchived).ToDictionary(c => c.ChapterID);
|
|
var projectBooks = data.Books.Where(b => !b.IsArchived).ToDictionary(b => b.BookID);
|
|
return Json(new { scenes = data.Scenes.Where(s => !s.IsArchived && chapters.ContainsKey(s.ChapterID))
|
|
.Where(s => projectBooks.ContainsKey(chapters[s.ChapterID].BookID)).Select(s => {
|
|
var chapter = chapters[s.ChapterID];
|
|
var book = projectBooks[chapter.BookID];
|
|
return new { book.BookID, bookLabel = $"Book {book.BookNumber}", bookTitle = book.BookDisplayTitle,
|
|
chapter.ChapterID, chapter.ChapterNumber, chapter.ChapterTitle, s.SceneID, s.SceneNumber, s.Summary,
|
|
title = string.IsNullOrWhiteSpace(s.SceneTitle) ? $"Scene {s.SceneNumber:g}" : s.SceneTitle,
|
|
date = s.StartDateTime?.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture) };
|
|
}) });
|
|
}
|
|
}
|
|
|
|
public sealed record ContextCalendarScope(int ProjectID, int BookID, int? ChapterID, int? SceneID);
|